aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views/main/jams/retract.py
blob: 277426b5ee263d970cdf1ac0447bb635c56526b9 (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
from werkzeug.exceptions import BadRequest

from pysite.base_route import RouteView
from pysite.decorators import csrf
from pysite.mixins import DBMixin, OAuthMixin

BANNABLE_STATES = ("preparing", "running")


class JamsProfileView(RouteView, DBMixin, OAuthMixin):
    path = "/jams/retract"
    name = "jams.retract"

    table_name = "code_jam_participants"
    infractions_table = "code_jam_infractions"
    jams_table = "code_jams"
    responses_table = "code_jam_responses"

    def get(self):
        if not self.user_data:
            return self.redirect_login()

        user_id = self.user_data["user_id"]
        participant = self.db.get(self.table_name, user_id)

        banned = False

        if participant:
            responses = self.db.run(self.db.query(self.responses_table).filter({"snowflake": user_id}), coerce=list)

            for response in responses:
                jam = response["jam"]
                jam_obj = self.db.get(self.jams_table, jam)

                if jam_obj:
                    if jam_obj["state"] in BANNABLE_STATES:
                        banned = True
                        break

        return self.render(
            "main/jams/retract.html", participant=participant, banned=banned
        )

    @csrf
    def post(self):
        if not self.user_data:
            return self.redirect_login()

        user_id = self.user_data["user_id"]
        participant = self.db.get(self.table_name, user_id)

        if not participant:
            return BadRequest()

        banned = False

        responses = self.db.run(self.db.query(self.responses_table).filter({"snowflake": user_id}), coerce=list)

        for response in responses:
            jam = response["jam"]
            jam_obj = self.db.get(self.jams_table, jam)

            if jam_obj:
                if jam_obj["state"] in BANNABLE_STATES:
                    banned = True

            self.db.delete(self.responses_table, response["id"])

        self.db.delete(self.table_name, participant["id"])

        if banned:
            self.db.insert(
                self.infractions_table, {
                    "participant": user_id,
                    "reason": "Automatic ban: Removed jammer profile in the middle of a code jam",
                    "number": -1,
                    "decremented_for": []
                }
            )

        return self.render(
            "main/jams/retracted.html", participant=participant, banned=banned
        )