diff --git a/.env b/.env index d89cb66..9046943 100644 --- a/.env +++ b/.env @@ -1,4 +1,3 @@ -FLASK_APP=webface +FLASK_APP=webface.py FLASK_RUN_PORT=54321 -FLASK_ENV=development -#FLASK_DEBUG=1 +FLASK_DEBUG=1 diff --git a/.flaskenv b/.flaskenv index eb74e69..67e402e 100644 --- a/.flaskenv +++ b/.flaskenv @@ -1,2 +1,2 @@ -FLASK_APP=webface +FLASK_APP=webface.py FLASK_ENV=production diff --git a/.vscode/settings.json b/.vscode/settings.json index 2f97bb1..42aa686 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,5 +5,6 @@ "emmet.includeLanguages": { "jinja-html": "html", "vue-html": "html" - } + }, + "python.formatting.provider": "black" } \ No newline at end of file diff --git a/README.md b/README.md index 7244e12..e3d3995 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Open in Visual Studio Code](https://classroom.github.com/assets/open-in-vscode-c66648af7eb3fe8bc4f294546bfd86ef473780cde1dea487d3c4ff354943c9ae.svg)](https://classroom.github.com/online_ide?assignment_repo_id=8703051&assignment_repo_type=AssignmentRepo) +[![Open in Visual Studio Code](https://classroom.github.com/assets/open-in-vscode-c66648af7eb3fe8bc4f294546bfd86ef473780cde1dea487d3c4ff354943c9ae.svg)](https://classroom.github.com/online_ide?assignment_repo_id=8703049&assignment_repo_type=AssignmentRepo) Flask Start OneFile ========================= diff --git a/SQLlite.db b/SQLlite.db new file mode 100644 index 0000000..316cf04 Binary files /dev/null and b/SQLlite.db differ diff --git a/data.db b/data.db new file mode 100644 index 0000000..72ae65d Binary files /dev/null and b/data.db differ diff --git a/mysqlite.py b/mysqlite.py new file mode 100644 index 0000000..f0ea0a0 --- /dev/null +++ b/mysqlite.py @@ -0,0 +1,37 @@ +import sqlite3 + + +class SQLite(): + def __init__(self, file='db.sqlite3'): + self.file=file + def __enter__(self): + self.conn = sqlite3.connect(self.file) + self.conn.row_factory = sqlite3.Row + return self.conn.cursor() + def __exit__(self, type, value, traceback): + self.conn.commit() + self.conn.close() + + +if __name__ == '__main__': + with SQLite("SQLlite.db") as cur: + sql = """ +CREATE TABLE "adresy"( + "zkratka" TEXT NOT NULL, + "adresa" TEXT NOT NULL, + "user: TEXT, + PRIMARY KEY("zkratka") +); + """ + cur.execute(sql) + + sql = """ +CREATE TABLE "user"( + "login" TEXT, + "passwd" TEXT, + PRIMARY KEY("login") +); + + + """ + cur.execute(sql) \ No newline at end of file diff --git "a/static/ban\303\241n.jpeg" "b/static/ban\303\241n.jpeg" new file mode 100644 index 0000000..eeb9f93 Binary files /dev/null and "b/static/ban\303\241n.jpeg" differ diff --git a/static/pomeranc.jpeg b/static/pomeranc.jpeg new file mode 100644 index 0000000..e4f2fec Binary files /dev/null and b/static/pomeranc.jpeg differ diff --git a/static/styles.css b/static/styles.css index e4b4043..21added 100644 --- a/static/styles.css +++ b/static/styles.css @@ -1,5 +1,5 @@ body { - background-color: #eeeeee; + background-color: #ea0079; padding-left: 1em; padding-right: 1em; margin-left: 90px; @@ -8,10 +8,11 @@ body { h1, h2, h3 { clear: both; margin-top: 3em; + color: #0aec0a; } h1 a { - color: #bb8800; + color: #0aec0a; } nav ul { @@ -26,12 +27,12 @@ nav ul { } nav ul li a { display: block; - color: white; - background-color: #324b79; + color: rgb(0, 30, 255); + background-color: #7bff00; border-radius: 25px 50px 0px; padding: 1ex 2ex; } nav ul li a:hover { - color: #324b72; + color: #5e12a6; background-color: white; } nav img { @@ -45,7 +46,7 @@ footer { background-color: #ecc8f0; } .login { - display: inline-block; + display: block; float: right; width: 30%; border: orange solid 2px; @@ -59,5 +60,28 @@ footer { background-color: lightblue; } - +.flash{ + display: block; + position: absolute; + left: 30%; + top: 1ex; + width: 40%; + border: 2px orange solid; + padding: 2ex; + border-radius: 20px; + font-weight: bold; +} +.error{ + background-color: white; + color: red; +} /*# sourceMappingURL=styles.css.map */ + +.url{ + font-size: 150%; +} + +.url input{ + width: 75%; + font-size: 170%; +} \ No newline at end of file diff --git a/templates/abc.html b/templates/abc.html index 590d55d..241887c 100644 --- a/templates/abc.html +++ b/templates/abc.html @@ -1,4 +1,4 @@ -{% extends 'base.html.j2' %} +{% extends 'base.html' %} {% block obsah %} diff --git a/templates/banan.html b/templates/banan.html new file mode 100644 index 0000000..38ec8a9 --- /dev/null +++ b/templates/banan.html @@ -0,0 +1,28 @@ + +{% extends 'base.html' %} + +{%block title%}Informace{%endblock%} + +{% block obsah %} + +

Miluju banany, BMI

+ +

+

formulář

+
+ Hmotnost:
kg
+ Výška:
cm
+ + + +
+

BMI={{bmi}}

+ + + + + + + + +{% endblock obsah %} diff --git a/templates/base.html b/templates/base.html index 925dc41..a9b1f57 100644 --- a/templates/base.html +++ b/templates/base.html @@ -16,14 +16,33 @@ Fork me on GitHub - - +

+ +{% if 'uživatel' in session %} +jsi přihlášen jako Najvetší lupič +{% else %} +Jaaaaj!!! Nejsi přihlášen Přihlas se! +{% endif %} + +

+ +{% for category,message in get_flashed_messages(with_categories=true) %} +

+ +{{message}} +

+{% endfor %} @@ -58,7 +77,18 @@ + diff --git a/templates/info.html b/templates/info.html index 2f1ab41..8bb0d22 100644 --- a/templates/info.html +++ b/templates/info.html @@ -1,4 +1,4 @@ -{% extends 'base.html.j2' %} +{% extends 'base.html' %} {%block title%}Informace{%endblock%} diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..76d3de7 --- /dev/null +++ b/templates/login.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} + +{%block title%}Login{%endblock%} + +{% block obsah %} + +

Login

+ +

zde se mužeš přihlásit

+
+ jméno:

+ heslo:
+
+
+ + + +
+{% endblock obsah %} diff --git a/templates/pomerance.html b/templates/pomerance.html new file mode 100644 index 0000000..0bb62dc --- /dev/null +++ b/templates/pomerance.html @@ -0,0 +1,32 @@ +{% extends 'base.html' %} + +{%block title%}Pomerance{%endblock%} + +{% block obsah %} + +

BMI

+ +

+ +

{%if bmi != None%} +"BMI" = {{bmi}} +{%else %} +

je treba zadat 2 nenulova cisla

+{%endif%} +

+ + +
+

Hmotnost:
+ kg

+

Výska:
+ + cm

+ +

+ +
+ +

By Karel Z.

+ +{% endblock obsah %} diff --git a/templates/registr.html b/templates/registr.html new file mode 100644 index 0000000..32ae318 --- /dev/null +++ b/templates/registr.html @@ -0,0 +1,25 @@ +{% extends 'base.html' %} + +{%block title%}rgistrace{%endblock%} + +{% block obsah %} + +

registrace

+ +

zde se muzes registrovat

+ + +
+ jmeno


+ heslo


+ heslo2


+ + + + + +
+ + + +{% endblock obsah %} diff --git a/templates/zkracovac.html b/templates/zkracovac.html new file mode 100644 index 0000000..7202e63 --- /dev/null +++ b/templates/zkracovac.html @@ -0,0 +1,38 @@ +{% extends 'base.html' %} + +{%block title%}zkracovac{%endblock%} + +{% block obsah %} + +

zkracovac URL

+ + +{% if new %} +

zkratka: {{new}}

+ +{% endif %} + +
+ +

+ Zadej URL: + +

+ + +
+ +

Seznam ulozenych zkratek

+ + + + + + +{% endblock obsah %} \ No newline at end of file diff --git a/webface.py b/webface.py index 857ea93..3b98a91 100644 --- a/webface.py +++ b/webface.py @@ -1,5 +1,9 @@ -from flask import Flask, render_template, request, redirect, url_for, session +from pickle import GET +from flask import Flask, render_template, request, redirect, url_for, session, flash import functools +import string +import random +import re # from werkzeug.security import generate_password_hash, check_password_hash @@ -10,6 +14,8 @@ slova = ("Super", "Perfekt", "Úža", "Flask") +from mysqlite import SQLite + def prihlasit(function): @functools.wraps(function) @@ -34,9 +40,30 @@ def info(): @app.route("/abc/") def abc(): + if "uživatel" not in session: + flash("Nejsi příhlášen, tato stránka vyžaduje přihlášení.", "error") + return redirect(url_for("login", page=request.full_path)) return render_template("abc.html", slova=slova) +@app.route("/banan/", methods=["GET", "POST"]) +def banan(): + if "uživatel" not in session: + flash("Nejsi příhlášen, tato stránka vyžaduje přihlášení.", "error") + return redirect(url_for("login", page=request.full_path)) + + hmotnost = request.args.get("hmotnost") + + výška = request.args.get("výška") + + print(hmotnost, výška) + if hmotnost and výška != None: + bmi = int(hmotnost) / ((int(výška) / 100) ** 2) + else: + bmi = 0 + return render_template("banan.html", bmi=bmi) + + @app.route("/text/") def text(): return """ @@ -46,3 +73,134 @@ def text():

toto je text

""" + + +@app.route("/login/", methods=["GET"]) +def login(): + jmeno = request.args.get("jmeno") + heslo = request.args.get("heslo") + print(jmeno, heslo) + if request.method == "GET": + 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("SQLlite.db") as cur: + cur.execute("select passwd FROM user WHERE login = ?", [jmeno]) + ans = cur.fetchall() + + if ans and ans[0][0] == heslo: + flash("Jsi přihlášen!", "message") + session["uživatel"] = jmeno + if page: + return redirect(page) + else: + flash("Nespávné přihlašovací udaje", "error") + if page: + return redirect(url_for("login", page=page)) + return redirect(url_for("login")) + # stejne jako funkce get, jen jiný zápis + + +@app.route("/logout/", methods=["GET"]) +def logout(): + session.pop("uživatel", None) + return redirect(url_for("login")) + + +@app.route("/registr/", methods=["GET"]) +def registr(): + jmeno = request.args.get("jmeno") + heslo = request.args.get("heslo") + heslo2 = request.form.get("heslo2") + + print(jmeno, heslo, heslo2) + if request.method == "GET": + return render_template("registr.html") + + +@app.route("/registr/", methods=["POST"]) +def registr_post(): + jmeno = request.form.get("jmeno") + heslo = request.form.get("heslo") + heslo2 = request.form.get("heslo2") + page = request.args.get("page") + + with SQLite("SQLlite.db") as cur: + cur.execute("INSERT INTO user = (?, ?)", [jmeno, heslo]) + ans = cur.fetchall() + if heslo == heslo2: + + if ans and ans[0][0] == heslo: + flash("Jsi přihlášen!", "message") + session["uživatel"] = jmeno + if page: + return redirect(page) + else: + flash("Nespávné přihlašovací udaje", "error") + if page: + return redirect(url_for("login", page=page)) + return redirect(url_for("pomerance")) + + +@app.route("/zkracovac/") +def zkracovac(): + new = request.args.get("new") + if "uživatel" in session: + with SQLite("SQLlite.db") as cur: + res = cur.execute( + "SELECT zkratka, adresa FROM adresy WHERE user =?", [session["uživatel"]] + ) + zkratky = res.fetchall() + if zkratky == None: + 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") + if url and re.match("https?://.+", url): + zkratka = "".join(random.choices(string.ascii_uppercase + string.digits, k=5)) + flash("adresa ulozena") + with SQLite("SQLlite.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] + ) + + return redirect(url_for("zkracovac", new=zkratka)) + else: + flash("ADRESA NEN9 ADRESA") + + return redirect(url_for("zkracovac")) + + +@app.route("/zkracovac/", methods=["GET"]) +def dezkracovac(zkratka): + print(zkratka) + with SQLite("SQLlite.db") as cur: + if "uživatel" in session: + res = cur.execute("SELECT adresa FROM adresy WHERE zkratka=?", [zkratka]) + odpoved = res.fetchall()[0][0] + if odpoved: + print(odpoved) + return redirect(odpoved) + else: + flash("({})neni adresa".format(zkratka)) + return redirect(odpoved)