From a0c81e455a3ad8c6bab7a8ad82ff6b5594416534 Mon Sep 17 00:00:00 2001 From: Jonas Leder <jonas@jonasled.de> Date: Sat, 9 Nov 2019 18:06:08 +0100 Subject: [PATCH] added table preview of own urls (delete missing) --- main.py | 25 ++++++++++++++++++++++--- static/style.css | 23 ++++++++++++++++++++++- templates/editEntries.html | 16 ++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 templates/editEntries.html diff --git a/main.py b/main.py index 0b44c25..e1dbb96 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,7 @@ from io import BytesIO #Needed for base64 encoding of the image from PIL import Image #Needed for QR generation from flask_github import GitHub #github oauth library import json #used for github oauth - +from html import escape #This is used to escape characters, if they are send in the url app = Flask(__name__) domain_to_index = {} @@ -135,10 +135,8 @@ def grecaptcha_verify(request): #This function is used to verify the google reca @app.route('/', methods=['GET']) def home_get(): try: - userID = request.cookies.get('userID') loginbar = "Hello " + request.cookies.get('username') + ' (<a href="/user/logout" style="color:white">logout</a>)' except: - userID = "null" loginbar = '<a href="/user/login" style="color:white">login</a>' return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect, loginbar=loginbar) #return the default site to create a new shorten link @@ -237,6 +235,27 @@ def logout(): resp.set_cookie('username', "", max_age=0) return resp +@app.route('/user/links') +def ownLinks(): + try: + userID = request.cookies.get('userID') + loginbar = "Hello " + request.cookies.get('username') + ' (<a href="/user/logout" style="color:white">logout</a>)' + except: + return redirect("/user/login") + + with sqlite3.connect('db/urls.db') as conn: #Get the original URL from the database + cursor = conn.cursor() + res = cursor.execute('SELECT LONG_URL, SHORT_URL FROM WEB_URL WHERE USERNAME=?', [userID]) + response = '<table id="t01">\n<tr>\n<th>Long URL</th>\n<th>Short URL</th>\n<th>Action</th>\n</tr>\n' + try: + for entries in res.fetchall(): + response = response + "<tr>\n<td>" + entries[0] + "</td>\n<td>" + entries[1] + '</td>\n<td><a id="red" href="/user/delete?link=' + escape(entries[1]) + '">delete</a></tr>\n' + except: + abort(500) + response = response + "</table>" + return render_template('editEntries.html', content=response, loginbar=loginbar) + + if __name__ == '__main__': table_check()# This code checks whether database table is created or not if production: #Check if production variable is set to true use the waitress webserver, else use the buildin flask webserver, with more debug output diff --git a/static/style.css b/static/style.css index e2b3d42..bedcc8f 100644 --- a/static/style.css +++ b/static/style.css @@ -64,11 +64,11 @@ margin: auto; position: relative; z-index: 1; background: #FFFFFF; -max-width: 360px; margin: 0 auto 100px; padding: 45px; text-align: center; box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24); +display: table; } .form input { font-family: "Roboto", sans-serif; @@ -190,6 +190,27 @@ animation: fadein 0.5s, fadeout 0.5s 2.5s; font-size: 17px; } +table, th, td { + border: 1px solid black; + border-collapse: collapse; +} +th, td { + padding: 15px; + text-align: left; +} +table#t01 tr:nth-child(even) { + background-color: #eee; +} +table#t01 tr:nth-child(odd) { + background-color: #fff; +} +table#t01 th { + background-color: black; + color: white; +} +a#red { + color: red; +} @-webkit-keyframes fadein { from {bottom: 0; opacity: 0;} to {bottom: 30px; opacity: 1;} diff --git a/templates/editEntries.html b/templates/editEntries.html new file mode 100644 index 0000000..c460bdf --- /dev/null +++ b/templates/editEntries.html @@ -0,0 +1,16 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <link href="{{ url_for('static', filename='style.css') }}" rel="stylesheet"> + <title>URL shorter</title> + </head> + + <body> + <div id="loginbar">{{loginbar | safe}}</div> + <div class="login-page"> + <div class="form"> + {{content | safe}} + </div> + </div> + </body> +</html> \ No newline at end of file -- GitLab