Skip to content
Snippets Groups Projects
deletelink.py 1.08 KiB
from sqlite3 import connect
from flask import redirect, abort

def deleteLink(request, s):
    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>)' # generate the login form
        loginbar = loginbar + "" #to hide the unused variable message
    except:
        return redirect("/user/login") # if user is not logged in redirect him to the login page
    linkToDelete = request.args.get('link') #get the link, which the user want's to delete from the parameter in the url.

    with connect('db/urls.db') as conn:
        cursor = conn.cursor()
        try:
            cursor.execute('DELETE FROM WEB_URL WHERE SHORT_URL=? AND USERNAME=?', [linkToDelete, userID]) #Delete the entrie
            return redirect('/user/links') #redirect the user back to the table.
        except:
            abort(500)
            
if (__name__ == "__main__"):
    print("This file is not made fore direct call, please run the main.py")
    exit()