aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views/staff/jams/actions.py
blob: 9aa7e79f54e102a57e7f269ebd969d768334544a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from flask import jsonify, request

from pysite.base_route import APIView
from pysite.constants import ALL_STAFF_ROLES, ErrorCodes
from pysite.decorators import csrf, require_roles
from pysite.mixins import DBMixin

GET_ACTIONS = ["questions"]
POST_ACTIONS = ["associate_question", "disassociate_question", "questions", "state"]
KEYS = ["action"]

QUESTION_KEYS = ["optional", "title", "type"]


class ActionView(APIView, DBMixin):
    path = "/jams/action"
    name = "jams.action"

    table_name = "code_jams"
    forms_table = "code_jam_forms"
    questions_table = "code_jam_questions"

    @csrf
    @require_roles(*ALL_STAFF_ROLES)
    def get(self):
        action = request.args.get("action")

        if action not in GET_ACTIONS:
            return self.error(ErrorCodes.incorrect_parameters)

        if action == "questions":
            questions = self.db.get_all(self.questions_table)

            print(questions)
            return jsonify({"questions": questions})

    @csrf
    @require_roles(*ALL_STAFF_ROLES)
    def post(self):
        action = request.args.get("action")

        if action not in POST_ACTIONS:
            return self.error(ErrorCodes.incorrect_parameters)

        if action == "associate_question":
            form = int(request.args.get("form"))
            question = request.args.get("question")

            form_obj = self.db.get(self.forms_table, form)

            if not form_obj:
                return self.error(ErrorCodes.incorrect_parameters, f"Unknown form: {form}")

            question_obj = self.db.get(self.questions_table, question)

            if not question_obj:
                return self.error(ErrorCodes.incorrect_parameters, f"Unknown question: {question}")

            if question_obj["id"] not in form_obj["questions"]:
                form_obj["questions"].append(question_obj["id"])
                self.db.insert(self.forms_table, form_obj, conflict="replace")

                return jsonify({"question": question_obj})
            else:
                return self.error(
                    ErrorCodes.incorrect_parameters,
                    f"Question {question} already associated with form {form}"
                )

        if action == "disassociate_question":
            form = int(request.args.get("form"))
            question = request.args.get("question")

            form_obj = self.db.get(self.forms_table, form)

            if not form_obj:
                return self.error(ErrorCodes.incorrect_parameters, f"Unknown form: {form}")

            question_obj = self.db.get(self.questions_table, question)

            if not question_obj:
                return self.error(ErrorCodes.incorrect_parameters, f"Unknown question: {question}")

            if question_obj["id"] in form_obj["questions"]:
                form_obj["questions"].remove(question_obj["id"])
                self.db.insert(self.forms_table, form_obj, conflict="replace")

                return jsonify({"question": question_obj})
            else:
                return self.error(
                    ErrorCodes.incorrect_parameters,
                    f"Question {question} not already associated with form {form}"
                )

        if action == "state":
            jam = int(request.args.get("jam"))
            state = request.args.get("state")

            if not all((jam, state)):
                return self.error(ErrorCodes.incorrect_parameters)

            jam_obj = self.db.get(self.table_name, jam)
            jam_obj["state"] = state
            self.db.insert(self.table_name, jam_obj, conflict="update")

            return jsonify({})

        if action == "questions":
            data = request.get_json(force=True)

            for key in QUESTION_KEYS:
                if key not in data:
                    return self.error(ErrorCodes.incorrect_parameters, f"Missing key: {key}")

            title = data["title"]
            optional = data["optional"]
            question_type = data["type"]
            question_data = data.get("data", {})

            if question_type in ["number", "range", "slider"]:
                if "max" not in question_data or "min" not in question_data:
                    return self.error(
                        ErrorCodes.incorrect_parameters, f"{question_type} questions must have both max and min values"
                    )

                result = self.db.insert(
                    self.questions_table,
                    {
                        "title": title,
                        "optional": optional,
                        "type": question_type,
                        "data": {
                            "max": question_data["max"],
                            "min": question_data["min"]
                        }
                    },
                    conflict="error"
                )
            elif question_type == "radio":
                if "options" not in question_data:
                    return self.error(
                        ErrorCodes.incorrect_parameters, f"{question_type} questions must have both options"
                    )

                result = self.db.insert(
                    self.questions_table,
                    {
                        "title": title,
                        "optional": optional,
                        "type": question_type,
                        "data": {
                            "options": question_data["options"]
                        }
                    },
                    conflict="error"
                )
            else:
                result = self.db.insert(
                    self.questions_table,
                    {  # No extra data for other types of question
                        "title": title,
                        "optional": optional,
                        "type": question_type
                    },
                    conflict="error"
                )

            return jsonify({"id": result["generated_keys"][0]})