diff options
| author | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2019-03-26 13:35:17 +0000 |
|---|---|---|
| committer | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2019-03-26 13:35:17 +0000 |
| commit | 5e711f079121e0b9e958261df4aafcf568cb62cf (patch) | |
| tree | 99a1336b2406411d81a2a05199362f6f1e362e0a /app/main | |
| parent | first working version of pan + zoom + click (diff) | |
| download | AnnotateChange-5e711f079121e0b9e958261df4aafcf568cb62cf.tar.gz AnnotateChange-5e711f079121e0b9e958261df4aafcf568cb62cf.zip | |
First working version of complete annotation functionality
Diffstat (limited to 'app/main')
| -rw-r--r-- | app/main/routes.py | 68 |
1 files changed, 64 insertions, 4 deletions
diff --git a/app/main/routes.py b/app/main/routes.py index a1d1a63..f1f7a15 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -1,12 +1,29 @@ # -*- coding: utf-8 -*- -from flask import render_template, flash, url_for, redirect +import datetime + +from flask import render_template, flash, url_for, redirect, request from flask_login import current_user, login_required +from app import db from app.main import bp -from app.models import Task +from app.models import Annotation, Task from app.main.datasets import load_data_for_chart +RUBRIC = """ +<i>Please mark all the points in the time series where an abrupt change in + the behaviour of the series occurs.</i> +<br> +<br> +If there are no such points, please click the "no changepoints" button. +<br> +When you're ready, please click the submit button. +<br> +<b>Note:</b> You can zoom and pan the graph if needed. +<br> +Thank you! +""" + @bp.route("/") @bp.route("/index") @@ -28,10 +45,53 @@ def index(): @bp.route("/annotate/<int:task_id>", methods=("GET", "POST")) @login_required def task(task_id): + if request.method == "POST": + # record post time + now = datetime.datetime.utcnow() + + # get the json from the client + annotation = request.get_json() + if annotation["task"] != task_id: + flash("Internal error: task id doesn't match.") + return redirect(url_for(task_id=task_id)) + + task = Task.query.filter_by(id=task_id).first() + + # remove all previous annotations for this task + for ann in Annotation.query.filter_by(task_id=task_id).all(): + db.session.delete(ann) + task.done = False + task.annotated_on = None + db.session.commit() + + # record the annotation + 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 the task as done + task.done = True + task.annotated_on = now + db.session.commit() + + flash("Your annotation has been recorded, thank you!") + return url_for("main.index") + task = Task.query.filter_by(id=task_id).first() if task is None: flash("No task with id %r has been assigned to you." % task_id) return redirect(url_for("main.index")) data = load_data_for_chart(task.dataset.name) - return render_template("annotate/index.html", title="Annotate %s" % - task.dataset.name, task=task, data=data) + return render_template( + "annotate/index.html", + title="Annotate %s" % task.dataset.name, + task=task, + data=data, + rubric=RUBRIC, + ) |
