diff --git a/backend/db.py b/backend/db.py index 592a5c638574..3a0a3307e49c 100644 --- a/backend/db.py +++ b/backend/db.py @@ -162,7 +162,7 @@ def get_completed_at(task_ids): return times -def get_completion(task_id): +def get_completions(task_id): """ Get completed time for list of task ids :param task_id: task ids @@ -175,7 +175,6 @@ def get_completion(task_id): filename = os.path.join(c['output_dir'], str(task_id) + '.json') if os.path.exists(filename): data = json.load(open(filename)) - data['completions'][0]['id'] = task_id else: data = None @@ -191,13 +190,30 @@ def save_completion(task_id, completion): global c task_id = int(task_id) - task = get_tasks()[task_id] - completion['id'] = task_id - task['completions'] = [completion] + task = get_completions(task_id) + + # init completions if it's empty + if 'completions' not in task: + task['completions'] = [] + + # update old completion + updated = False + if 'id' in completion: + for i, item in enumerate(task['completions']): + if item['id'] == completion['id']: + task['completions'][i] = completion + updated = True + + # write new completion + if not updated: + completion['id'] = task_id * 1000 + len(task['completions']) + 1 + task['completions'].append(completion) + + # write task + completions to file filename = os.path.join(c['output_dir'], str(task_id) + '.json') os.mkdir(c['output_dir']) if not os.path.exists(c['output_dir']) else () json.dump(task, open(filename, 'w'), indent=4, sort_keys=True) - return task_id * 10000 + random.randint(0, 1000) # in simple case completion id == task id + return completion['id'] def delete_completion(task_id): diff --git a/backend/server.py b/backend/server.py index 898b903417a9..218818cbdb1c 100644 --- a/backend/server.py +++ b/backend/server.py @@ -79,7 +79,7 @@ def index(): task_id = request.args.get('task_id', None) if task_id is not None: - task_data = db.get_completion(task_id) + task_data = db.get_completions(task_id) if task_data is None: task_data = db.get_task(task_id) @@ -137,7 +137,7 @@ def api_tasks(task_id): """ Get task by id """ # try to get task with completions first - task_data = db.get_completion(task_id) + task_data = db.get_completions(task_id) task_data = db.get_task(task_id) if task_data is None else task_data return make_response(jsonify(task_data), 200) @@ -187,19 +187,17 @@ def api_completion_by_id(task_id, completion_id): return make_response('Incorrect request method', 500) -@app.route('/api/tasks//completions/', methods=['PATCH']) +@app.route('/api/tasks//completions//', methods=['PATCH']) @exception_treatment def api_completion_update(task_id, completion_id): """ Rewrite existing completion with patch. This is technical api call for editor testing only. It's used for Rewrite button in editor. """ global c - completion = request.json - assert task_id == completion_id, \ - 'Task ID != Completion ID' completion.pop('state', None) # remove editor state + completion['id'] = int(completion_id) db.save_completion(task_id, completion) log.info(msg='Completion saved', extra={'task_id': task_id, 'output': request.json}) return make_response('ok', 201)