generated from spseol/flask-start-onefile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebface.py
192 lines (151 loc) · 5.75 KB
/
webface.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
from flask import Flask, render_template, request, redirect, url_for, session, flash
import functools
from werkzeug.security import generate_password_hash, check_password_hash
import sqlite3
from mysqlite import SQLite
import random
import string
import re
# from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
app.secret_key = b"totoj e zceLa n@@@hodny retezec nejlep os.urandom(24)"
app.secret_key = b"x6\x87j@\xd3\x88\x0e8\xe8pM\x13\r\xafa\x8b\xdbp\x8a\x1f\xd41\xb8"
slova = ("Super", "Perfekt", "Úža category", "Flask")
def prihlasit(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
if "user" in session:
return function(*args, **kwargs)
else:
return redirect(url_for("login", url=request.path))
return wrapper
@app.route("/", methods=["GET"])
def index():
return render_template("base.html")
@app.route("/info/")
def info():
return render_template("info.html")
@app.route("/Zkracovac/")
def zkracovac():
new = request.args.get('new')
if 'uživatel' in session:
with SQLite("data.db") as cur:
res = cur.execute('SELECT zkratka,adresa FROM adresy WHERE user=?',
[session['uživatel']]
)
zkratky = res.fetchall()
if not zkratky: #situace kdy je uživatel přihlášen ale nemá nic uložené v databázi
zkratky = []
else:
zkratky = []
return render_template("zkracovac.html", new=new, zkratky = zkratky)
@app.route("/Zkracovac/", methods = ["POST"])
def zkracovac_post():
url = request.form.get('url_adresa')
if url and re.match('https?://.+',url):
zkratka = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
with SQLite("data.db") as cur:
if 'uživatel' in session:
cur.execute(
"INSERT INTO adresy (zkratka,adresa,user) VALUES (?,?,?)",
[zkratka, url, session['uživatel']]
)
else:
cur.execute(
"INSERT INTO adresy (zkratka,adresa) VALUES (?,?)",
[zkratka, url]
)
flash('Adresa uložena.')
return redirect(url_for('zkracovac', new=zkratka))
else:
flash('To co jsi zadal není adresa webové stránky')
return redirect(url_for('zkracovac'))
@app.route("/Zkracovac/<zkratka>", methods = ["GET"])
def dezkracovac(zkratka):
print(zkratka)
with SQLite("data.db") as cur:
res = cur.execute('SELECT adresa FROM adresy WHERE zkratka=?',[zkratka])
odpoved = res.fetchone()
if odpoved:
print(odpoved[0])
return redirect(odpoved[0])
else:
flash('To co jsi zadal není adresa webové stránky')
return redirect(url_for('zkracovac'))
@app.route("/abc/")
def abc():
return render_template("abc.html", slova=slova)
@app.route("/malina/", methods=["GET", "POST"])
def malina():
if "uživatel" not in session:
flash("Nejsi přihlášen. Tato stránka vyžaduje přihlášení.", "error")
return redirect(url_for("login", page=request.full_path))
hmotnost = request.args.get("hmotnost")
vyska = request.args.get("vyska")
print(hmotnost, vyska)
if hmotnost and vyska:
try:
hmotnost = float(hmotnost)
vyska = float(vyska)
bmi = hmotnost / (0.01 * vyska) ** 2
except (ZeroDivisionError, ValueError):
bmi = None
# err = 'Je třeba zadat dvě nenulová čísla!'
else:
bmi = None
return render_template("malina.html", bmi=bmi)
@app.route("/login/", methods=["GET"])
def login():
jmeno = request.args.get("jmeno")
heslo = request.args.get("heslo")
print(jmeno, heslo)
return render_template("login.html")
@app.route("/login/", methods=["POST"])
def login_post():
jmeno = request.form.get("jmeno")
heslo = request.form.get("heslo")
page = request.args.get("page")
with SQLite("data.db") as cur:
cur.execute("SELECT passwd FROM user WHERE login = ?", [jmeno])
ans = cur.fetchall()
if ans and check_password_hash(ans[0][0], heslo):
flash("Jsi přihlášen!", "message")
session["uživatel"] = jmeno
if page:
return redirect(page)
else:
flash("Nesprávné přihlašovací údaje", "error")
if page:
return redirect(url_for("login", page=page))
return redirect(url_for("login"))
@app.route("/logout/", methods=["GET", "POST"])
def logout():
session.pop("uživatel", None)
return redirect(url_for("index"))
@app.route("/registrate/", methods=["GET"])
def registrate():
return render_template("registrate.html")
@app.route("/registrate/", methods=["POST"])
def registrate_post():
jmeno = request.form.get("jmeno")
heslo = request.form.get("heslo")
heslo2 = request.form.get("heslo2")
if not (jmeno and heslo and heslo2):
flash("Je nutné vyplnit všechna políčka!", "error")
return redirect(url_for("registrate"))
if heslo != heslo2:
flash("Obě hesla musí být stejná!", "error")
return redirect(url_for("registrate"))
heslo_hash = generate_password_hash(heslo)
try:
with SQLite("data.db") as cur:
cur.execute(
"INSERT INTO user (login,passwd) VALUES (?,?)", [jmeno, heslo_hash]
)
flash("Právě jsi se zaregistroval.", "message")
flash("Jsi přihlášen....", "message")
session["uživatel"] = jmeno
return redirect(url_for("index"))
except sqlite3.IntegrityError:
flash(f"Jméno {jmeno} již existuje. Vyberte jiné.", "error")
return redirect(url_for("registrate"))