From a3645384764573d682d4b5602f33fd97eeb6c698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Vyb=C3=ADhal?= Date: Thu, 19 Jan 2023 13:54:46 +0100 Subject: [PATCH] =?UTF-8?q?dopln=C4=9Bn=C3=AD=C4=8Dko?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.db | Bin 20480 -> 20480 bytes mysqlite.py | 4 +- templates/base.html | 2 +- templates/zkracovac.html | 15 ------- "templates/zkracova\304\215.html" | 28 +++++++++++++ webface.py | 64 ++++++++++++++++++++++++------ 6 files changed, 83 insertions(+), 30 deletions(-) delete mode 100644 templates/zkracovac.html create mode 100644 "templates/zkracova\304\215.html" diff --git a/data.db b/data.db index 39be85425491ea75b5df1520b2febb583c8fedb5..b6891e358455c784d78b7af750f3983d384b16cf 100644 GIT binary patch delta 425 zcmZozz}T>Wae_3X;6xc`M!}59pT%FcSx_L2zg~%jMci62%FjD2 zD5IpLpx8=Zzr4I$FE6zuF*z|sFF8LqDM^7DsD{DZ$uzGcq+XO-o8nN;OI{H8e9dFtRYS zG*3x3HngxbGD|ZtGBr&$Hcd`TGD%G`u(V7wNM~meZ}dz|$<53|wP>=p{Z0-J{;dqa Ykl(tQ%V8xyI~%hgV^R{wWae_1>&qNt#MxKocOZb@>1U3sQoZz20!HA8E0SMq+F*~tD98$16V2L3&pc^p>qPYmFkyvN@K0L!-;q5uE@ diff --git a/mysqlite.py b/mysqlite.py index c98f408..8efea6f 100644 --- a/mysqlite.py +++ b/mysqlite.py @@ -10,4 +10,6 @@ def __enter__(self): return self.conn.cursor() def __exit__(self, type, value, traceback): self.conn.commit() - self.conn.close() \ No newline at end of file + self.conn.close() + + \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index a8a7a78..c7cffe1 100644 --- a/templates/base.html +++ b/templates/base.html @@ -39,7 +39,7 @@
  • Info
  • ABC
  • Malina
  • -
  • Zkracovač
  • +
  • Zkracovač
  • Login
  • logout
  • registrace
  • diff --git a/templates/zkracovac.html b/templates/zkracovac.html deleted file mode 100644 index 5c26bc5..0000000 --- a/templates/zkracovac.html +++ /dev/null @@ -1,15 +0,0 @@ -{% extends 'base.html' %} - -{%block title%}Zkracovač{%endblock%} - -{% block obsah %} - -

    Zkracovač URL

    - -
    -

    - Zadej URL: - - -

    -{% endblock obsah %} \ No newline at end of file diff --git "a/templates/zkracova\304\215.html" "b/templates/zkracova\304\215.html" new file mode 100644 index 0000000..b2c1d0d --- /dev/null +++ "b/templates/zkracova\304\215.html" @@ -0,0 +1,28 @@ +{% extends 'base.html' %} + +{%block title%}Zkracovač{%endblock%} + +{% block obsah %} + +

    Zkracovač URL

    + +{% if new %} +

    Vytvořena nová zkratka:{{ new }}

    + +{% endif %} + +
    +

    + Zadejte URL:
    + + +

    +
    +

    Seznam uložených zkratek

    + +{%endfor%} +{% endblock obsah %} \ No newline at end of file diff --git a/webface.py b/webface.py index a23d50c..185efe1 100644 --- a/webface.py +++ b/webface.py @@ -6,6 +6,7 @@ import sqlite3 import random import string +import re app = Flask(__name__) app.secret_key = b"totoj e zceLa n@@@hodny retezec nejlep os.urandom(24)" @@ -15,7 +16,9 @@ slova = ("Super", "Perfekt", "Úža", "Flask") - +@app.route("/", methods=["GET"]) +def index(): + return render_template("base.html") def prihlasit(function): @functools.wraps(function) @@ -28,22 +31,57 @@ def wrapper(*args, **kwargs): return wrapper -@app.route("/", methods=["GET"]) -def index(): - return render_template("base.html") +@app.route("/zkracovač/", methods=["GET"]) +def zkracovač(): + new = request.args.get("new") + if "uzivatel" in session: + with SQLite("data.db") as cur: + res = cur.execute("SELECT zkratka, adresa FROM adresy Where user=?", [session["uzivatel"]]) + zkratky = res.fetchall() + if not zkratky: + zkratky = [] + else: + zkratky = [] + return render_template("zkracovač.html", new=new, zkratky=zkratky) + -@app.route("/zkracovac/") -def zkracovac(): - return render_template("zkracovac.html") -@app.route("/zkracovac/", methods=["POST"]) -def zkracovac_post(): +@app.route("/zkracovač/", methods=["POST"]) +def zkracovač_post(): url = request.form.get("url") - zkratka = "".join(random.choices(string.ascii_uppercase +string.digits, k=5)) + 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 "uzivatel" in session: + cur.execute("INSERT INTO adresy (zkratka, adresa, user) VALUES (?, ?, ?)", [zkratka, url, session["uzivatel"]]) + flash("Adresa uložena") + else: + cur.execute("INSERT INTO adresy (zkratka, adresa) VALUES (?, ?)", [zkratka, url]) + return redirect(url_for("zkracovač", new=zkratka)) + else: + flash("To, co jsi zadal není adresa webové stránky!") + + + return redirect(url_for("zkracovač",)) + +@app.route("/zkracovač/", methods=["GET"]) +def dezkracovac(zkratka): + print(zkratka) with SQLite("data.db") as cur: - cur.execute( " INSERT INTO adresy (zkratka,adresa) VALUES (?,?) ", [zkratka,url] ) - - return redirect(url_for("zkracovac")) + cur.execute("SELECT adresa FROM adresy WHERE zkratka=? ;", [zkratka]) + odpoved = cur.fetchone() + print(type(odpoved)) + if odpoved: + print(odpoved[0]) + return redirect(odpoved[0]) + + else: + flash("Toto ({}) není korektní ZKRATKA".format(zkratka)) + + + return redirect(url_for("zkracovač"))