Ejemplo n.º 1
0
def process_annotations(demo_id):
    annotation = request.get_json()
    if annotation["identifier"] != demo_id:
        LOGGER.error(
            "User %s returned a task id in the demo that wasn't the demo id." %
            current_user.username)
        flash(
            "An internal error occurred, the administrator has been notified.",
            "error",
        )
        return redirect(url_for("main.index"))

    retval = []
    if not annotation["changepoints"] is None:
        retval = [int(cp["x"]) for cp in annotation["changepoints"]]

    # If the user is already introduced, we assume that their demo annotations
    # are already in the database, and thus we don't put them back in (because
    # we want the original ones).
    if current_user.is_introduced:
        return retval

    dataset = Dataset.query.filter_by(
        name=DEMO_DATA[demo_id]["dataset"]["name"]).first()

    # Create a new task
    task = Task(annotator_id=current_user.id, dataset_id=dataset.id)
    task.done = False
    task.annotated_on = None
    db.session.add(task)
    db.session.commit()
    if annotation["changepoints"] is None:
        ann = Annotation(cp_index=None, task_id=task.id)
        db.session.add(ann)
        db.session.commit()
    else:
        for cp in annotation["changepoints"]:
            ann = Annotation(cp_index=cp["x"], task_id=task.id)
            db.session.add(ann)
            db.session.commit()

    # mark task as done
    task.done = True
    task.annotated_on = datetime.datetime.utcnow()
    db.session.commit()

    return retval
Ejemplo n.º 2
0
def create_task():
    """
    This method creates a new task. Validates that the title is not empty.
    :return:
        The list with all the previous tasks plus the new task.
    """

    # Retrieving the current values in the database to show all the tasks
    tasks_db = Task.query.all()
    tasks = []

    for task in tasks_db:
        task_data = {
            'id': task.id,
            'title': task.title,
            'description': task.description,
            'done': task.done
        }
        tasks.append(task_data)

    data_received = request.json

    if not data_received or not 'title' in data_received:
        abort(400)

    # Created the object task
    task_db = Task()
    task_db.title = data_received['title']
    task_db.description = data_received['description']
    task_db.done = False

    db.session.add(task_db)
    db.session.commit()

    # Converting the object task to a dictionary
    task_data = {
        'id': task_db.id,
        'title': task_db.title,
        'description': task_db.description,
        'done': task_db.done
    }
    tasks.append(task_data)

    return jsonify({'tasks': tasks}), 201