summaryrefslogtreecommitdiff
path: root/routes/answer.py
blob: 2c2be82b8074caabc75a5bd01a973362fe04d521 (plain)
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
from app import app, D
from flask import render_template, session, request, redirect
from routes.tools import rows2dicts, get_alert, get_nick, csrf_check


@app.route("/kys/<link>")
def kys_link(link):
    if aid := D.quiz.find_by_link( link ):
        session["answer_id"] = aid
        return redirect("/#answer")
    return redirect("/")

@app.route("/set/answer_id",methods=["POST"])
def answer_id():
    next = "/#"+request.form["caller"] if "caller" in request.form else "/"
    if csrf_check():
        return redirect(next)
    if "id" not in session:
        session["alert"] = "Nimimerkkiä ei ole asetettu."
        return redirect(next)
    else:
        sid = session["id"]

    if "link" not in request.form or request.form["link"]=="":
        session["alert"] = "Kyselmän nimeä ei ole annettu."
        return redirect(next)
        
    if aid := D.quiz.find_by_link( request.form["link"] ):
        session["answer_id"] = aid
    else:
        session["alert"] = "Koodilla ei löytynyt kyselmää"
        return redirect(next)
        
    if next == "/#analyse" and not D.quiz.user( aid, sid ):
        session["alert"] = "Et ole vielä vastannut tähän kyselmään. \
                            Voit tutkia vastaksia vastattuasi."
        return redirect("/#answer")

    if next == "/#answer" and D.quiz.user( aid, sid ):
        session["alert"] = "Olet jo vastannut valitsemaasi kyselyyn."
        return redirect("/#analyse")

    return redirect( next )

@app.route("/pages/answer.html")
def answer():
    if "id" in session:
        sid = session["id"]
    else:
        return render_template(
                "answer.html",
                caller = "answer",
                alert = get_alert()
            )
        
    if "answer_id" in session:
        aid = session["answer_id"]
    else:
        return render_template(
                "answer.html",
                caller = "answer",
                alert = get_alert(),
                nick = get_nick()
            )
        
    if D.quiz.user(aid, sid):
        return render_template(
                "answer.html",
                caller = "answer",
                alert = get_alert(),
                nick = get_nick()
            )
        
    return render_template(
            "answer.html",
            caller = "answer",
            alert = get_alert(),
            nick = get_nick(),
            questions = rows2dicts( D.quiz.questions(aid), ['i','q','n','p'] ),
            link = D.quiz.get_link( aid )
        )

@app.route("/set/answers",methods=["POST"])
def set_answers():
    if csrf_check():
        return redirect("/#answer")
    if "id" not in session:
        session["alert"]="Nimimerkkiä ei ole vielä valittu!"
        return redirect( "/#answer" )
    if "answer_id" not in session:
        session["alert"]="Kyselyä ei ole valittu vastaamista varten!"
        return redirect( "/#answer" )

    sid = session["id"]
    for question, answer in request.form.items():
        if question=="csrf":
            continue
        try: 
            if int(answer) < 0 or int(answer) > 999:
                session["alert"]="Luvattoman pieniä tai suuria lukuja!"
                return redirect( "/#answer" )
            elif D.answer.get(int(sid), int(question)) != -1:
                session["alert"]="Kyselyyn olikin jo saatu vastauksia."
                return redirect( "/#answer" )
        except ValueError:
            session["alert"] = "Vastaukset ei ole lukuja!"
            return redirect( "/#answer" )

    for question, answer in request.form.items():
        if question=="csrf":
            continue
        D.answer.new(int(sid), int(question), int(answer))

    return redirect("/#analyse")