diff --git a/main.py b/main.py index a8b951eb42b87466cfb882564f92cdcc62361a75..e23c34b45e082f7f0c75e89c2660031d3205cedf 100644 --- a/main.py +++ b/main.py @@ -155,8 +155,12 @@ def logout(): return resp @app.route('/user/links')#This function gives the user the posibility to see and delete his links -def ownLinks(): - return userProfile(request, cookieNotice, s) +def redirectOwnLinks(): + return redirect("/user/links0") + +@app.route('/user/links<pageNumber>')#This function gives the user the posibility to see and delete his links +def ownLinks(pageNumber): + return userProfile(request, cookieNotice, s, pageNumber) @app.route('/user/delete') #This function is called if a user deletes an entrie def delete(): diff --git a/templates/editEntries.html b/templates/editEntries.html index f7075a85e338f70ddb3b86aae4d6767a47cd5f31..f56f993e867270265904f6e647bc2c81dab5a975 100644 --- a/templates/editEntries.html +++ b/templates/editEntries.html @@ -14,6 +14,7 @@ <div class="login-page"> <div class="form"> {{content | safe}} + <p>{% if backButton %}<a href="{{backButton}}"><button id="small">last page</button></a>{% endif %}	{% if nextButton %}<a href="{{nextButton}}"><button id="small">next page</button></a>{% endif %}</p> <p><a href="/user/api"><button id="small">API</button></a>	<a href="/"><button id="small">back</button></a></p> </div> </div> diff --git a/userprofile.py b/userprofile.py index f71f865b9b2243172e7a4c18601900e4b94f38f3..48bbe786b15aa2c8e2b7155979641f5fd6c1e2dc 100644 --- a/userprofile.py +++ b/userprofile.py @@ -2,20 +2,31 @@ import sqlite3 from flask import redirect, abort, render_template from html import escape #This is used to escape characters, if they are send in the url -def userProfile(request, cookieNotice, s): +def userProfile(request, cookieNotice, s, pageNumber): + backButton = 0 + nextButton = 0 + lenEntries = 0 try: userID = s.loads(request.cookies.get('userID')) #Get the userid from the cookie loginbar = "Hello " + s.loads(request.cookies.get('username')) + ' (<a href="/user/logout" style="color:white">logout</a>)' #This is the loginbar except: return redirect("/user/login") #If user is not logged in redirect him to the login page + try: + pageNumber = int(pageNumber) + print(pageNumber - 1) + offset = pageNumber * 25 + except: + abort(404) + with sqlite3.connect('db/urls.db') as conn: cursor = conn.cursor() res = cursor.execute('SELECT LONG_URL, SHORT_URL FROM WEB_URL WHERE USERNAME=?', [userID]) #Get all entries from the database, that are created by this user response = '<table id="t01">\n<tr>\n<th>Long URL</th>\n<th>Short URL</th>\n<th>Views</th>\n<th>Action</th>\n</tr>\n' #This is the layout of the table try: entriesList = res.fetchall() - for entries in entriesList: #for every entrie in the database add a line to the table + lenEntries = len(entriesList) + for entries in entriesList[offset:][:25]: #for every entrie in the database add a line to the table cursor2 = conn.cursor() try: calls = str(cursor2.execute('SELECT CALLS FROM ANALYTICS WHERE SHORT_URL=?', [entries[1]]).fetchone()[0]) @@ -23,8 +34,11 @@ def userProfile(request, cookieNotice, s): calls = "0" response = response + "<tr>\n<td>" + entries[0] + "</td>\n<td><a href=\"" + entries[1] + '">' + entries[1] + '</a></td>\n<td>' + calls + '</td>\n<td><a id="red" href="/user/delete?link=' + escape(entries[1]) + '">delete</a> <a href="#" id="dialog-link" onclick="buttonListener(\'' + entries[1] + '\', this)">QR</a></tr>\n' response = response + "</table>" #Close the table + if(len(entriesList) == 0): response = 'you have no shorten links.' #If user has no shorten links make this message + elif(pageNumber > 1): backButton = "/user/links" + str(pageNumber - 1) + if(offset + 25 < lenEntries): nextButton = "/user/links" + str(pageNumber + 1) except: print(Exception) abort(500) #Shouldn't happen, 500 means internal server error - return render_template('editEntries.html', content=response, loginbar=loginbar, cookieNotice=cookieNotice) #Put the table and the login div inside the template and server it to the user + return render_template('editEntries.html', content=response, loginbar=loginbar, cookieNotice=cookieNotice, backButton=backButton, nextButton=nextButton) #Put the table and the login div inside the template and server it to the user