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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
from email.utils import parseaddr
from flask import redirect, request, url_for
from werkzeug.exceptions import BadRequest, NotFound
from pysite.base_route import RouteView
from pysite.decorators import csrf
from pysite.mixins import DBMixin, OauthMixin
class JamsJoinView(RouteView, DBMixin, OauthMixin):
path = "/jams/join/<int:jam>"
name = "jams.join"
table_name = "code_jams"
forms_table = "code_jam_forms"
questions_table = "code_jam_questions"
responses_table = "code_jam_responses"
participants_table = "code_jam_participants"
infractions_table = "code_jam_infractions"
def get(self, jam):
jam_obj = self.db.get(self.table_name, jam)
if not jam_obj:
return NotFound()
if not self.user_data:
return redirect(url_for("discord.login"))
infractions = self.get_infractions(self.user_data["user_id"])
for infraction in infractions:
if infraction["number"] == -1: # Indefinite ban
return self.render("main/jams/banned.html", infraction=infraction, jam=jam_obj)
if infraction["number"]: # Got some jams left
if jam not in infraction["decremented_for"]:
# Make sure they haven't already tried to apply for this jam
infraction["number"] -= 1
infraction["decremented_for"].append(jam)
self.db.insert(self.infractions_table, infraction, conflict="replace")
return self.render("main/jams/banned.html", infraction=infraction, jam=jam_obj)
if jam in infraction["decremented_for"]:
# They already tried to apply for this jam
return self.render("main/jams/banned.html", infraction=infraction, jam=jam_obj)
participant = self.db.get(self.participants_table, self.user_data["user_id"])
if not participant:
return redirect(url_for("main.jams.profile", form=jam))
if self.get_response(jam, self.user_data["user_id"]):
return self.render("main/jams/already.html", jam=jam_obj)
form_obj = self.db.get(self.forms_table, jam)
questions = []
if form_obj:
for question in form_obj["questions"]:
questions.append(self.db.get(self.questions_table, question))
return self.render(
"main/jams/join.html", jam=jam_obj, form=form_obj,
questions=questions, question_ids=[q["id"] for q in questions]
)
@csrf
def post(self, jam):
jam_obj = self.db.get(self.table_name, jam)
if not jam_obj:
return NotFound()
if not self.user_data:
return redirect(url_for("discord.login"))
infractions = self.get_infractions(self.user_data["user_id"])
for infraction in infractions:
if infraction["number"] == -1: # Indefinite ban
return self.render("main/jams/banned.html", infraction=infraction)
if infraction["number"]: # Got some jams left
if jam not in infraction["decremented_for"]:
# Make sure they haven't already tried to apply for this jam
infraction["number"] -= 1
infraction["decremented_for"].append(jam)
self.db.insert(self.infractions_table, infraction, conflict="replace")
return self.render("main/jams/banned.html", infraction=infraction, jam=jam_obj)
if jam in infraction["decremented_for"]:
# They already tried to apply for this jam
return self.render("main/jams/banned.html", infraction=infraction, jam=jam_obj)
participant = self.db.get(self.participants_table, self.user_data["user_id"])
if not participant:
return redirect(url_for("main.jams.profile"))
if self.get_response(jam, self.user_data["user_id"]):
return self.render("main/jams/already.html", jam=jam_obj)
form_obj = self.db.get(self.forms_table, jam)
if not form_obj:
return NotFound()
questions = []
for question in form_obj["questions"]:
questions.append(self.db.get(self.questions_table, question))
answers = []
for question in questions:
value = request.form.get(question["id"])
answer = {"question": question["id"]}
if not question["optional"] and value is None:
return BadRequest()
if question["type"] == "checkbox":
if value == "on":
answer["value"] = True
elif not question["optional"]:
return BadRequest()
else:
answer["value"] = False
elif question["type"] == "email":
if value:
address = parseaddr(value)
if address == ("", ""):
return BadRequest()
answer["value"] = value
elif question["type"] in ["number", "range", "slider"]:
if value is not None:
value = int(value)
if value > int(question["data"]["max"]) or value < int(question["data"]["min"]):
return BadRequest()
answer["value"] = value
elif question["type"] == "radio":
if value:
if value not in question["data"]["options"]:
return BadRequest()
answer["value"] = value
elif question["type"] in ["text", "textarea"]:
answer["value"] = value
answers.append(answer)
user_id = self.user_data["user_id"]
response = {
"snowflake": user_id,
"jam": jam,
"approved": False,
"answers": answers
}
self.db.insert(self.responses_table, response)
return self.render("main/jams/thanks.html", jam=jam_obj)
def get_response(self, jam, user_id):
query = self.db.query(self.responses_table).filter({"jam": jam, "snowflake": user_id})
result = self.db.run(query, coerce=list)
if result:
return result[0]
return None
def get_infractions(self, user_id):
query = self.db.query(self.infractions_table).filter({"participant": user_id})
return self.db.run(query, coerce=list)
|