summaryrefslogtreecommitdiff
path: root/routes/tools.py
blob: 65aff626306dfe4196c7a0eea2cca16683ad5989 (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
from random import randint
from flask import session, request
from app import D

def rows2dicts( rows, names ):
    dlist=[]
    for i in range(len(rows)):
        row={}
        for j in range(len(names)):
            row[names[j]]=rows[i][j]
        dlist.append(row)
    return dlist

def get_alert():
    if "alert" in session:
        alert = session["alert"]
        del session["alert"]
        return f"{alert}"
    return ""
    
def get_nick():
    while "id" in session.keys():
        nick = D.user.get_nick(session["id"])
        if not nick:
            del session['id']
            if "quiz_id" in session.keys():
                del session['quiz_id']
            break
        return nick
    return "(ei nimimerkkiä)"

def generate_link():
    konso="rtpshjklvnm"
    vocal="eyuioa"
    str=""
    for i in range(4):
        str+=konso[randint(0,len(konso)-1)]
        str+=vocal[randint(0,len(vocal)-1)]
    return str

def csrf_check():
    if "csrf" not in session \
            or "csrf" not in request.form \
            or session["csrf"] != request.form["csrf"]:
        session["alert"]="Istuntosi katkesi tai pyyntö on toiselta sivulta!"
        return True
    return False