Skip to content
Snippets Groups Projects
userprofile.py 2.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jonas Leder's avatar
    Jonas Leder committed
    from sqlite3 import connect
    
    from flask import redirect, abort, render_template
    from html import escape #This is used to escape characters, if they are send in the url
    
    
    Jonas Leder's avatar
    Jonas Leder committed
    def userProfile(request, cookieNotice, s, pageNumber, url_scheme):
    
        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" >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)
            offset = pageNumber * 25
        except:
            abort(404)
    
    
    Jonas Leder's avatar
    Jonas Leder committed
        with 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()
    
                lenEntries = len(entriesList)
    
                idCounter = 0
    
                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])
                    except:
                        calls = "0"
    
                    response = response + "<tr id=tr_" + str(idCounter) + ">\n<td>" + entries[0] + "</td>\n<td><a href=\"" + url_scheme + "://" + entries[1] + '">' + entries[1] + '</a></td>\n<td>' + calls + '</td>\n<td><a id="red" href="javascript:deleteLink(\'/user/delete?link=' + escape(entries[1].replace("'", "\\'")) + '\',\'tr_' + str(idCounter) + '\')">delete</a> <a href="#" id="dialog-link" onclick="buttonListener(\'' + entries[1] + '\', this)">QR</a></tr>\n'
    
                    idCounter=idCounter+1
    
                response = response + "</table>" #Close the table
    
                if(len(entriesList) == 0): response = '<h2>you have no shorten links.</h2>' #If user has no shorten links make this message
    
                elif(pageNumber > 0): 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, backButton=backButton, nextButton=nextButton) #Put the table and the login div inside the template and server it to the user
    
    
    if (__name__ == "__main__"):
        print("This file is not made fore direct call, please run the main.py")
        exit()