diff options
author | 2018-05-19 13:03:01 +0100 | |
---|---|---|
committer | 2018-05-19 13:03:01 +0100 | |
commit | 75b640709ca186c137fde6291eedf806ba474a13 (patch) | |
tree | 23078753c654bab291d5cee5329f28a79981a3d8 /pysite | |
parent | [Jams] Add non-task info text to be displayed at jam announcement (diff) |
[Jams] Complete question listing/editing/etc
Diffstat (limited to 'pysite')
-rw-r--r-- | pysite/constants.py | 1 | ||||
-rw-r--r-- | pysite/views/staff/jams/actions.py | 29 | ||||
-rw-r--r-- | pysite/views/staff/jams/forms/__init__.py | 0 | ||||
-rw-r--r-- | pysite/views/staff/jams/forms/questions_edit.py | 75 | ||||
-rw-r--r-- | pysite/views/staff/jams/forms/questions_view.py | 24 | ||||
-rw-r--r-- | pysite/views/staff/jams/forms/view.py | 44 |
6 files changed, 173 insertions, 0 deletions
diff --git a/pysite/constants.py b/pysite/constants.py index e30ed10b..f4ea8449 100644 --- a/pysite/constants.py +++ b/pysite/constants.py @@ -76,6 +76,7 @@ JAM_STATES = [ ] JAM_QUESTION_TYPES = [ + "checkbox", "email", "number", "radio", diff --git a/pysite/views/staff/jams/actions.py b/pysite/views/staff/jams/actions.py index 9aa7e79f..3683db39 100644 --- a/pysite/views/staff/jams/actions.py +++ b/pysite/views/staff/jams/actions.py @@ -7,6 +7,7 @@ from pysite.mixins import DBMixin GET_ACTIONS = ["questions"] POST_ACTIONS = ["associate_question", "disassociate_question", "questions", "state"] +DELETE_ACTIONS = ["question"] KEYS = ["action"] QUESTION_KEYS = ["optional", "title", "type"] @@ -166,3 +167,31 @@ class ActionView(APIView, DBMixin): ) return jsonify({"id": result["generated_keys"][0]}) + + @csrf + @require_roles(*ALL_STAFF_ROLES) + def delete(self): + action = request.args.get("action") + + if action not in DELETE_ACTIONS: + return self.error(ErrorCodes.incorrect_parameters) + + if action == "question": + question = request.args.get("id") + + if not question: + return self.error(ErrorCodes.incorrect_parameters, f"Missing key: id") + + question_obj = self.db.get(self.questions_table, question) + + if not question_obj: + return self.error(ErrorCodes.incorrect_parameters, f"Unknown question: {question}") + + self.db.delete(self.questions_table, question) + + for form_obj in self.db.get_all(self.forms_table): + if question in form_obj["questions"]: + form_obj["questions"].remove(question) + self.db.insert(self.forms_table, form_obj, conflict="replace") + + return jsonify({"id": question}) diff --git a/pysite/views/staff/jams/forms/__init__.py b/pysite/views/staff/jams/forms/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/pysite/views/staff/jams/forms/__init__.py diff --git a/pysite/views/staff/jams/forms/questions_edit.py b/pysite/views/staff/jams/forms/questions_edit.py new file mode 100644 index 00000000..94132e0d --- /dev/null +++ b/pysite/views/staff/jams/forms/questions_edit.py @@ -0,0 +1,75 @@ +import json + +from flask import request, redirect, url_for +from werkzeug.exceptions import NotFound, BadRequest + +from pysite.base_route import RouteView +from pysite.constants import ALL_STAFF_ROLES +from pysite.decorators import require_roles, csrf +from pysite.mixins import DBMixin + +REQUIRED_KEYS = ["title", "date_start", "date_end"] + + +class StaffView(RouteView, DBMixin): + path = "/jams/forms/questions/<question>" + name = "jams.forms.questions.edit" + + questions_table = "code_jam_questions" + + @require_roles(*ALL_STAFF_ROLES) + def get(self, question): + question_obj = self.db.get(self.questions_table, question) + + if not question_obj: + return NotFound() + + return self.render( + "staff/jams/forms/questions_edit.html", question=question_obj + ) + + @require_roles(*ALL_STAFF_ROLES) + @csrf + def post(self, question): + question_obj = self.db.get(self.questions_table, question) + + if not question_obj: + return NotFound() + + title = request.form.get("title") + optional = request.form.get("optional") + question_type = request.form.get("type") + + print(question_type) + + if not title or not optional or not question_type: + return BadRequest() + + question_obj["title"] = title + question_obj["optional"] = optional == "optional" + question_obj["type"] = question_type + + if question_type == "radio": + options = request.form.get("options") + + if not options: + return BadRequest() + + options = json.loads(options)["options"] # No choice this time + question_obj["data"] = {"options": options} + + elif question_type in ("number", "range", "slider"): + question_min = request.form.get("min") + question_max = request.form.get("max") + + if question_min is None or question_max is None: + return BadRequest() + + question_obj["data"] = { + "min": question_min, + "max": question_max + } + + self.db.insert(self.questions_table, question_obj, conflict="replace") + + return redirect(url_for("staff.jams.forms.questions")) diff --git a/pysite/views/staff/jams/forms/questions_view.py b/pysite/views/staff/jams/forms/questions_view.py new file mode 100644 index 00000000..00331810 --- /dev/null +++ b/pysite/views/staff/jams/forms/questions_view.py @@ -0,0 +1,24 @@ +from werkzeug.exceptions import NotFound + +from pysite.base_route import RouteView +from pysite.constants import ALL_STAFF_ROLES +from pysite.decorators import require_roles +from pysite.mixins import DBMixin + +REQUIRED_KEYS = ["title", "date_start", "date_end"] + + +class StaffView(RouteView, DBMixin): + path = "/jams/forms/questions" + name = "jams.forms.questions" + + questions_table = "code_jam_questions" + + @require_roles(*ALL_STAFF_ROLES) + def get(self): + questions = self.db.get_all(self.questions_table) + + return self.render( + "staff/jams/forms/questions_view.html", questions=questions, + question_ids=[q["id"] for q in questions] + ) diff --git a/pysite/views/staff/jams/forms/view.py b/pysite/views/staff/jams/forms/view.py new file mode 100644 index 00000000..eaa910f2 --- /dev/null +++ b/pysite/views/staff/jams/forms/view.py @@ -0,0 +1,44 @@ +from werkzeug.exceptions import NotFound + +from pysite.base_route import RouteView +from pysite.constants import ALL_STAFF_ROLES +from pysite.decorators import require_roles +from pysite.mixins import DBMixin + +REQUIRED_KEYS = ["title", "date_start", "date_end"] + + +class StaffView(RouteView, DBMixin): + path = "/jams/forms/<int:jam>" + name = "jams.forms.view" + + table_name = "code_jams" + forms_table = "code_jam_forms" + questions_table = "code_jam_questions" + + @require_roles(*ALL_STAFF_ROLES) + def get(self, jam): + jam_obj = self.db.get(self.table_name, jam) + + if not jam_obj: + return NotFound() + + form_obj = self.db.get(self.forms_table, jam) + + if not form_obj: + form_obj = { + "number": jam, + "questions": [] + } + + self.db.insert(self.forms_table, form_obj) + + if form_obj["questions"]: + questions = self.db.get_all(self.questions_table, *[q for q in form_obj["questions"]]) + else: + questions = [] + + return self.render( + "staff/jams/forms/view.html", jam=jam_obj, form=form_obj, + questions=questions, question_ids=[q["id"] for q in questions] + ) |