|
7 | 7 | from app.lti_session_passback.auth_checkers import check_auth, is_admin |
8 | 8 | from app.mongo_odm import TaskAttemptsDBManager, TasksDBManager |
9 | 9 | from app.utils import check_arguments_are_convertible_to_object_id, check_argument_is_convertible_to_object_id |
| 10 | +from app.localisation import t |
| 11 | +from app.status import PassBackStatus |
| 12 | + |
| 13 | +from app.mongo_models import TaskAttempts |
10 | 14 |
|
11 | 15 | api_task_attempts = Blueprint('api_task_attempts', __name__) |
12 | 16 | logger = get_root_logger() |
|
15 | 19 |
|
16 | 20 | @check_arguments_are_convertible_to_object_id |
17 | 21 | @api_task_attempts.route('/api/task-attempts/', methods=['GET']) |
18 | | -def get_current_task_attempt() -> (dict, int): |
| 22 | +def get_current_task_attempt() -> tuple[dict, int]: |
19 | 23 | """ |
20 | 24 | Endpoint to get current task attempt information. |
21 | 25 |
|
@@ -56,10 +60,49 @@ def get_current_task_attempt() -> (dict, int): |
56 | 60 | 'attempt_count': task_db.attempt_count, |
57 | 61 | }, 200 |
58 | 62 |
|
| 63 | +def get_task_attempt_information(task_attempt_db: TaskAttempts) -> dict: |
| 64 | + try: |
| 65 | + is_passed_back = dict(task_attempt_db.is_passed_back) |
| 66 | + |
| 67 | + for training_id, training_status in is_passed_back.items(): |
| 68 | + is_passed_back[training_id] = t(PassBackStatus.russian.get(training_status)) |
| 69 | + |
| 70 | + return { |
| 71 | + 'message': 'OK', |
| 72 | + 'task_id': str(task_attempt_db.task_id), |
| 73 | + 'username': str(task_attempt_db.username), |
| 74 | + 'training_count': task_attempt_db.training_count, |
| 75 | + 'training_scores': dict(task_attempt_db.training_scores), |
| 76 | + 'is_passed_back': is_passed_back, |
| 77 | + 'params_for_passback': dict(task_attempt_db.params_for_passback) |
| 78 | + } |
| 79 | + except Exception as e: |
| 80 | + return {'message': '{}: {}'.format(e.__class__, e)} |
| 81 | + |
| 82 | + |
| 83 | +@check_arguments_are_convertible_to_object_id |
| 84 | +@api_task_attempts.route('/api/task-attempts/<task_attempt_id>/', methods=['GET']) |
| 85 | +def get_task_attempt(task_attempt_id) -> tuple[dict, int]: |
| 86 | + """ |
| 87 | + Endpoint to get information about a task attempt by its identifier. |
| 88 | +
|
| 89 | + :param task_attempt_id: Task attempt identifier |
| 90 | + :return: Dictionary with task attempt information and 'OK' message, or |
| 91 | + a dictionary with an explanation and 404 HTTP return code if a task attempt was not found, or |
| 92 | + an empty dictionary with 404 HTTP return code if access was denied. |
| 93 | + """ |
| 94 | + if not check_access({'_id': ObjectId(task_attempt_id)}): |
| 95 | + return {}, 404 |
| 96 | + |
| 97 | + task_attempt_db = TaskAttemptsDBManager().get_task_attempt(task_attempt_id) |
| 98 | + |
| 99 | + if task_attempt_db is None: |
| 100 | + return {'message': 'No task attempt with task_attempt_id = {}.'.format(task_attempt_id)}, 404 |
| 101 | + return get_task_attempt_information(task_attempt_db), 200 |
59 | 102 |
|
60 | 103 | @check_arguments_are_convertible_to_object_id |
61 | 104 | @api_task_attempts.route('/api/task_attempts/<task_attempt_id>/', methods=['DELETE']) |
62 | | -def delete_task_attempt_by_task_attempt_id(task_attempt_id: str) -> (dict, int): |
| 105 | +def delete_task_attempt_by_task_attempt_id(task_attempt_id: str) -> tuple[dict, int]: |
63 | 106 | """ |
64 | 107 | Endpoint to delete a task attempt by its identifier. |
65 | 108 |
|
|
0 commit comments