aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views/staff/jams/edit_info.py
blob: 3264a9af09b34f5cf51c9c319de51bf702390d5a (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
from flask import redirect, request, url_for
from werkzeug.exceptions import BadRequest, NotFound

from pysite.base_route import RouteView
from pysite.constants import ALL_STAFF_ROLES
from pysite.decorators import csrf, require_roles
from pysite.mixins import DBMixin
from pysite.rst import render

REQUIRED_KEYS = ["info_rst", "repo", "task_rst", "theme"]
ALLOWED_STATES = ["planning", "info"]


class StaffView(RouteView, DBMixin):
    path = "/jams/<int:jam>/edit/info"
    name = "jams.edit.info"
    table_name = "code_jams"

    @require_roles(*ALL_STAFF_ROLES)
    def get(self, jam):
        jam_obj = self.db.get(self.table_name, jam)

        if not jam_obj:
            return NotFound()

        if not jam_obj["state"] in ALLOWED_STATES:
            return BadRequest()
        return self.render("staff/jams/edit_info.html", jam=jam_obj)

    @require_roles(*ALL_STAFF_ROLES)
    @csrf
    def post(self, jam):
        jam_obj = self.db.get(self.table_name, jam)

        if not jam_obj:
            return NotFound()

        if not jam_obj["state"] in ALLOWED_STATES:
            return BadRequest()

        print(request.form)
        for key in REQUIRED_KEYS:
            arg = request.form.get(key)

            if not arg:
                return BadRequest()

            jam_obj[key] = arg

        jam_obj["task_html"] = render(jam_obj["task_rst"], link_headers=False)["html"]
        jam_obj["info_html"] = render(jam_obj["info_rst"], link_headers=False)["html"]

        self.db.insert(self.table_name, jam_obj, conflict="replace")

        return redirect(url_for("staff.jams.index"))