-
Notifications
You must be signed in to change notification settings - Fork 1
/
puzzlehunts_csv_import.py
57 lines (46 loc) · 2.7 KB
/
puzzlehunts_csv_import.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
import csv
from flask import request, redirect, Blueprint, flash
from db_model import Puzzlehunt, db, PuzzlehuntSettings, Puzzle, ArrivalCode, SolutionCode, Hint, PuzzlePrerequisite
from helpers import render, admin_required
puzzlehunts_csv_import = Blueprint('puzzlehunts_csv_import', __name__, template_folder='templates', static_folder='static')
@puzzlehunts_csv_import.route('/puzzlehunts/csv_import', methods=("GET", "POST"))
@admin_required
def puzzlehunts_import():
if request.method == "POST":
try:
puzzlehunt = Puzzlehunt(request.form["puzzlehunt"])
db.session.add(puzzlehunt)
db.session.flush()
minutes_to_hint_setting = PuzzlehuntSettings(puzzlehunt.id_puzzlehunt, "minutes_to_hint")
minutes_to_hint_setting.value = request.form["minutes_to_hint"]
minutes_to_hint = int(minutes_to_hint_setting.value)
db.session.add(minutes_to_hint_setting)
hints_are_ordered = PuzzlehuntSettings(puzzlehunt.id_puzzlehunt, "hints_are_ordered")
hints_are_ordered.value = "True"
db.session.add(hints_are_ordered)
puzzles = {}
# order,puzzle,arrival_code,solution_code,solution_message,hint_1,hint_2,prerequisites
reader = csv.DictReader(request.form['puzzlehunt_csv'].splitlines())
for row in reader:
order = int(row['order'])
puzzle = Puzzle(puzzlehunt.id_puzzlehunt, row['puzzle'], "", order)
db.session.add(puzzle)
db.session.flush()
puzzles[order] = puzzle.id_puzzle
db.session.add(ArrivalCode(puzzle.id_puzzle, row['arrival_code'], ""))
db.session.add(SolutionCode(puzzle.id_puzzle, row['solution_code'], row['solution_message']))
db.session.add(Hint(puzzle.id_puzzle, 1, minutes_to_hint, row['hint_1']))
db.session.add(Hint(puzzle.id_puzzle, 2, minutes_to_hint, row['hint_2']))
prerequisites = [int(x) for x in row['prerequisites'].split(',') if x]
for prerequisite in prerequisites:
if prerequisite not in puzzles:
flash(f'Chyba u šifry "{puzzle.puzzle}". Prerekvizity mohou být jen předchozí šifry.', "warning")
else:
db.session.add(PuzzlePrerequisite(puzzles[prerequisite], puzzle.id_puzzle))
db.session.commit()
except KeyError as e:
flash(f'Chyba při importu: chybí sloupec {e}.', "warning")
except ValueError as e:
flash(f'Chyba při importu: {e}', "warning")
return redirect(f"/puzzlehunts")
return render("puzzlehunts_csv_import.html")