-
Notifications
You must be signed in to change notification settings - Fork 1
/
codes.py
195 lines (145 loc) · 7.06 KB
/
codes.py
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
189
190
191
192
193
194
from flask import request, redirect, flash, Blueprint
from db_model import Puzzle, db, Code, ArrivalCode, SolutionCode
from helpers import render, admin_required
codes = Blueprint('codes', __name__, template_folder='templates', static_folder='static')
# Code normalization
LETTERS_NODIA = "acdeeinorstuuyz"
LETTERS_DIA = "áčďéěíňóřšťúůýž"
# A translation table usable with `str.translate` to rewrite characters with dia to the ones without them.
REMOVE_DIA = str.maketrans(LETTERS_DIA + LETTERS_DIA.upper(), LETTERS_NODIA + LETTERS_NODIA.upper())
def remove_diacritics(text: str):
return text.translate(REMOVE_DIA)
def compare_codes(user_code: str, correct_code: str):
user_code = remove_diacritics(user_code).lower().strip()
correct_code = remove_diacritics(correct_code).lower().strip()
return user_code == correct_code
# Puzzlehunt codes
def get_codes(id_puzzlehunt):
return Code.query.filter_by(id_puzzlehunt=id_puzzlehunt).all()
@codes.route('/puzzlehunts/<id_puzzlehunt>/codes/new', methods=("GET", "POST"))
@admin_required
def codes_new(id_puzzlehunt):
if request.method == "POST":
code = Code(id_puzzlehunt, request.form["code"], request.form["message"])
db.session.add(code)
db.session.commit()
return redirect(f"/puzzlehunts/{id_puzzlehunt}")
return render("code_edit.html", heading="Přidat kód do šifrovačky", back_url=f"/puzzlehunts/{id_puzzlehunt}")
@codes.route('/puzzlehunts/<id_puzzlehunt>/codes/<id_code>', methods=("GET", "POST"))
@admin_required
def codes_edit(id_puzzlehunt, id_code):
code = Code.query.get(id_code)
if code is None:
flash(f"Kód s id_code={id_code} neexistuje.", "warning")
return redirect(f"/puzzlehunts/{id_puzzlehunt}")
if request.method == "POST":
code.code = request.form["code"]
code.message = request.form["message"]
db.session.add(code)
db.session.commit()
return redirect(f"/puzzlehunts/{id_puzzlehunt}")
else:
return render("code_edit.html", heading="Upravit kód", back_url=f"/puzzlehunts/{id_puzzlehunt}", code=code)
@codes.route('/puzzlehunts/<id_puzzlehunt>/codes/<id_code>/delete', methods=("POST",))
@admin_required
def codes_delete(id_puzzlehunt, id_code):
code = Code.query.get(id_code)
if code is None:
flash(f"Kód s id_code={id_code} neexistuje.", "warning")
return redirect(f"/puzzlehunts/{id_puzzlehunt}")
db.session.delete(code)
db.session.commit()
flash(f'Kód "{code.code}" smazán.', "success")
return redirect(f"/puzzlehunts/{id_puzzlehunt}")
# Arrival codes
def get_arrival_codes(id_puzzle):
return ArrivalCode.query.filter_by(id_puzzle=id_puzzle).all()
@codes.route('/puzzles/<id_puzzle>/arrival_codes/new', methods=("GET", "POST"))
@admin_required
def arrival_codes_new(id_puzzle):
puzzle = Puzzle.query.get(id_puzzle)
if puzzle is None:
flash(f"Šifra s id_puzzle={id_puzzle} neexistuje.", "warning")
return redirect("/puzzles")
if request.method == "POST":
code = ArrivalCode(id_puzzle, request.form["code"], request.form["message"])
db.session.add(code)
db.session.commit()
return redirect(f"/puzzles/{id_puzzle}")
return render("code_edit.html", heading="Přidat kód na otevření šifry", back_url=f"/puzzles/{id_puzzle}", puzzle=puzzle)
@codes.route('/puzzles/<id_puzzle>/arrival_codes/<id_arrival_code>', methods=("GET", "POST"))
@admin_required
def arrival_codes_edit(id_puzzle, id_arrival_code):
puzzle = Puzzle.query.get(id_puzzle)
if puzzle is None:
flash(f"Šifra s id_puzzle={id_puzzle} neexistuje.", "warning")
return redirect("/puzzles")
arrival_code = ArrivalCode.query.get(id_arrival_code)
if arrival_code is None:
flash(f"Kód s id_arrival_code={id_arrival_code} neexistuje.", "warning")
return redirect(f"/puzzles/{id_puzzle}")
if request.method == "POST":
arrival_code.code = request.form["code"]
arrival_code.message = request.form["message"]
db.session.add(arrival_code)
db.session.commit()
return redirect(f"/puzzles/{id_puzzle}")
else:
return render("code_edit.html", heading="Upravit kód", back_url=f"/puzzles/{id_puzzle}", code=arrival_code, puzzle=puzzle)
@codes.route('/puzzles/<id_puzzle>/arrival_codes/<id_arrival_code>/delete', methods=("POST",))
@admin_required
def arrival_codes_delete(id_puzzle, id_arrival_code):
arrival_code = ArrivalCode.query.get(id_arrival_code)
if arrival_code is None:
flash(f"Kód s id_arrival_code={id_arrival_code} neexistuje.", "warning")
return redirect(f"/puzzles/{id_puzzle}")
db.session.delete(arrival_code)
db.session.commit()
flash(f'Kód "{arrival_code.code}" smazán.', "success")
return redirect(f"/puzzles/{id_puzzle}")
# Solution codes
def get_solution_codes(id_puzzle):
return SolutionCode.query.filter_by(id_puzzle=id_puzzle).all()
@codes.route('/puzzles/<id_puzzle>/solution_codes/new', methods=("GET", "POST"))
@admin_required
def solution_codes_new(id_puzzle):
puzzle = Puzzle.query.get(id_puzzle)
if puzzle is None:
flash(f"Šifra s id_puzzle={id_puzzle} neexistuje.", "warning")
return redirect("/puzzles")
if request.method == "POST":
code = SolutionCode(id_puzzle, request.form["code"], request.form["message"])
db.session.add(code)
db.session.commit()
return redirect(f"/puzzles/{id_puzzle}")
return render("code_edit.html", heading="Přidat řešení šifry", back_url=f"/puzzles/{id_puzzle}", puzzle=puzzle)
@codes.route('/puzzles/<id_puzzle>/solution_codes/<id_solution_code>', methods=("GET", "POST"))
@admin_required
def solution_codes_edit(id_puzzle, id_solution_code):
puzzle = Puzzle.query.get(id_puzzle)
if puzzle is None:
flash(f"Šifra s id_puzzle={id_puzzle} neexistuje.", "warning")
return redirect("/puzzles")
solution_code = SolutionCode.query.get(id_solution_code)
if solution_code is None:
flash(f"Řešení s id_solution_code={id_solution_code} neexistuje.", "warning")
return redirect(f"/puzzles/{id_puzzle}")
if request.method == "POST":
solution_code.code = request.form["code"]
solution_code.message = request.form["message"]
db.session.add(solution_code)
db.session.commit()
return redirect(f"/puzzles/{id_puzzle}")
else:
return render("code_edit.html", heading="Upravit řešení", back_url=f"/puzzles/{id_puzzle}", code=solution_code, puzzle=puzzle)
@codes.route('/puzzles/<id_puzzle>/solution_codes/<id_solution_code>/delete', methods=("POST",))
@admin_required
def solution_codes_delete(id_puzzle, id_solution_code):
solution_code = SolutionCode.query.get(id_solution_code)
if solution_code is None:
flash(f"Řešení s id_solution_code={id_solution_code} neexistuje.", "warning")
return redirect(f"/puzzles/{id_puzzle}")
db.session.delete(solution_code)
db.session.commit()
flash(f'Řešení "{solution_code.code}" smazáno.', "success")
return redirect(f"/puzzles/{id_puzzle}")