Skip to content

Commit

Permalink
Multiple completions support. (HumanSignal#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
makseq authored Aug 30, 2019
1 parent a66897f commit 9030d51
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
28 changes: 22 additions & 6 deletions backend/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down
10 changes: 4 additions & 6 deletions backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -187,19 +187,17 @@ def api_completion_by_id(task_id, completion_id):
return make_response('Incorrect request method', 500)


@app.route('/api/tasks/<task_id>/completions/<completion_id>', methods=['PATCH'])
@app.route('/api/tasks/<task_id>/completions/<completion_id>/', 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)
Expand Down

0 comments on commit 9030d51

Please sign in to comment.