-
Notifications
You must be signed in to change notification settings - Fork 1
/
puzzles.py
92 lines (76 loc) · 3.6 KB
/
puzzles.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
from flask import request, redirect, flash, Blueprint
from codes import get_arrival_codes, get_solution_codes
from db_model import Puzzle, db, PuzzlePrerequisite, Puzzlehunt
from helpers import render, admin_required
from hints import get_hints
puzzles = Blueprint('puzzles', __name__, template_folder='templates', static_folder='static')
@puzzles.route('/puzzles')
@admin_required
def puzzles_list():
puzzles = Puzzle.query.filter_by(id_puzzlehunt=Puzzlehunt.get_current_id()).order_by(Puzzle.order).all()
return render("puzzles.html", puzzles=puzzles)
@puzzles.route('/puzzles/new', methods=("GET", "POST"))
@admin_required
def puzzles_new():
if request.method == "POST":
puzzle = Puzzle(Puzzlehunt.get_current_id(), request.form["puzzle"], request.form["assignment"], request.form["order"])
db.session.add(puzzle)
db.session.commit()
return redirect(f"/puzzles/{puzzle.id_puzzle}")
order = Puzzle.query.filter_by(id_puzzlehunt=Puzzlehunt.get_current_id()).count() + 1
return render("puzzle_edit.html", order=order)
@puzzles.route('/puzzles/<id_puzzle>', methods=("GET", "POST"))
@admin_required
def puzzles_edit(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":
puzzle.puzzle = request.form["puzzle"]
puzzle.assignment = request.form["assignment"]
puzzle.order = request.form["order"]
db.session.add(puzzle)
db.session.commit()
return redirect("/puzzles")
else:
arrival_codes = get_arrival_codes(id_puzzle)
solution_codes = get_solution_codes(id_puzzle)
hints = get_hints(id_puzzle)
return render("puzzle_edit.html", puzzle=puzzle, arrival_codes=arrival_codes, solution_codes=solution_codes, hints=hints)
@puzzles.route('/puzzles/<id_puzzle>/delete', methods=("POST",))
@admin_required
def puzzles_delete(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")
db.session.delete(puzzle)
db.session.commit()
flash(f'Šifra "{puzzle.puzzle}" smazána.', "success")
return redirect("/puzzles")
@puzzles.route('/puzzles/<id_new_puzzle>/prerequisites/new', methods=("GET", "POST"))
@admin_required
def prerequisites_new(id_new_puzzle):
puzzle = Puzzle.query.get(id_new_puzzle)
if request.method == "POST":
prerequisite = PuzzlePrerequisite(request.form["prerequisite"], id_new_puzzle)
db.session.add(prerequisite)
db.session.commit()
return redirect(f"/puzzles/{id_new_puzzle}")
else:
prerequisites_ids = [p.id_puzzle for p in puzzle.get_prerequisites()]
other_puzzles = Puzzle.query.filter_by(id_puzzlehunt=Puzzlehunt.get_current_id())\
.filter(Puzzle.id_puzzle.not_in(prerequisites_ids + [puzzle.id_puzzle])).all()
return render("prerequisite_edit.html", puzzle=puzzle, other_puzzles=other_puzzles)
@puzzles.route('/puzzles/<id_new_puzzle>/prerequisites/<id_previous_puzzle>/delete', methods=("POST",))
@admin_required
def prerequisites_delete(id_new_puzzle, id_previous_puzzle):
prerequisite = PuzzlePrerequisite.query.get((id_previous_puzzle, id_new_puzzle))
if prerequisite is not None:
db.session.delete(prerequisite)
db.session.commit()
flash(f'Závislost úspěšně smazána.', "success")
else:
flash(f'Závislost nenalezena.', "warning")
return redirect(f"/puzzles/{id_new_puzzle}")