aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views/staff/jams/actions.py
blob: 93a7f2c7b678842891739404e8c58e20655d2e1b (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
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

ACTIONS = ["state"]
KEYS = ["action"]


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

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

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

        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({})