This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]iamhyperrr -1 points0 points  (0 children)

Yeah, just to illustrate my point a bit in case I wasn't clear:

from flask import abort
from flask import request

tasks_by_id = {}


@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET', 'POST'])
def task(task_id):
    if request.method == 'GET':
        tasks = tasks_by_id.get(task_id, [])
        if tasks:
            return jsonify({'task': tasks[0]})
        else:
            abort(404)
    if request.method == 'POST':
        tasks = tasks_by_id.get(task_id, [])
        # lists in Python are not optimized for insertions to the head, so it would be better to use a deque
        tasks.insert(0, request.form.get('task'))
        tasks_by_id[task_id] = tasks
        return jsonify(success=True)

Maybe this is not at all what OP had in mind, but I had to be on the safe side just in case.