|
17 | 17 | # along with PYBOSSA. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
|
19 | 19 | import json |
20 | | -from datetime import timedelta |
| 20 | +from datetime import timedelta, datetime |
21 | 21 | from time import time |
22 | 22 |
|
23 | 23 | from pybossa.contributions_guard import ContributionsGuard |
24 | 24 | from pybossa.core import sentinel |
25 | 25 | from werkzeug.exceptions import BadRequest |
| 26 | +import os |
26 | 27 |
|
27 | 28 | TASK_USERS_KEY_PREFIX = 'pybossa:project:task_requested:timestamps:{0}' |
28 | 29 | USER_TASKS_KEY_PREFIX = 'pybossa:user:task_acquired:timestamps:{0}' |
29 | 30 | TASK_ID_PROJECT_ID_KEY_PREFIX = 'pybossa:task_id:project_id:{0}' |
30 | 31 | ACTIVE_USER_KEY = 'pybossa:active_users_in_project:{}' |
31 | 32 | EXPIRE_LOCK_DELAY = 5 |
32 | 33 | EXPIRE_RESERVE_TASK_LOCK_DELAY = 30*60 |
33 | | - |
| 34 | +USER_EXPORTED_REPORTS_KEY = 'pybossa:user:exported:reports:{}' |
34 | 35 |
|
35 | 36 | def get_active_user_key(project_id): |
36 | 37 | return ACTIVE_USER_KEY.format(project_id) |
@@ -127,6 +128,35 @@ def get_locked_tasks_project(project_id): |
127 | 128 | }) |
128 | 129 | return tasks |
129 | 130 |
|
| 131 | +def get_user_exported_reports_key(user_id): |
| 132 | + # redis key to store exported reports for user_id |
| 133 | + return USER_EXPORTED_REPORTS_KEY.format(user_id) |
| 134 | + |
| 135 | +def register_user_exported_report(user_id, path, conn, ttl=60*60): |
| 136 | + # register report path for user_id |
| 137 | + # reports are stored as hset with key as user_id and field as timestamp:path |
| 138 | + now = time() |
| 139 | + key = get_user_exported_reports_key(user_id) |
| 140 | + filename = os.path.basename(path) |
| 141 | + value = json.dumps({"filename": filename, "path": path}) |
| 142 | + conn.hset(key, now, value) |
| 143 | + conn.expire(key, ttl) |
| 144 | + cache_info = f"Registered exported report for user_id {user_id} at {now} with value {value}" |
| 145 | + return cache_info |
| 146 | + |
| 147 | +def get_user_exported_reports(user_id, conn): |
| 148 | + # obtain all reports for user_id |
| 149 | + # reports are stored as hset with key as user_id and field as timestamp:path |
| 150 | + # return list of (timestamp, path) tuples |
| 151 | + key = get_user_exported_reports_key(user_id) |
| 152 | + reports_data = conn.hgetall(key).items() |
| 153 | + result = [] |
| 154 | + for k, v in reports_data: |
| 155 | + decoded_value = json.loads(v.decode()) |
| 156 | + formatted_time = datetime.fromtimestamp(float(k.decode())).strftime('%Y-%m-%d %H:%M:%S:%f')[:-3] |
| 157 | + result.append((formatted_time, decoded_value['filename'], decoded_value['path'])) |
| 158 | + return result |
| 159 | + |
130 | 160 |
|
131 | 161 | class LockManager(object): |
132 | 162 | """ |
|
0 commit comments