diff --git a/main.py b/main.py index f16419eeec636b9f754a70fcead5b89734b5cf85..be7b1811b2cda7f3c052da0e1752b76f269c73fa 100644 --- a/main.py +++ b/main.py @@ -71,7 +71,7 @@ try: app.config['GITHUB_CLIENT_ID'] = os.environ['GITHUB_CLIENT_ID'] app.config['GITHUB_CLIENT_SECRET'] = os.environ['GITHUB_CLIENT_SECRET'] except: - print("github client id sor client secret is not set, please set these and run again.") + print("github client id or client secret is not set, please set these and run again.") exit() github = GitHub(app) @@ -210,69 +210,68 @@ def redirect_short_url(short_url): @app.route('/user/login') def login(): - return github.authorize(scope="user") + return github.authorize(scope="user") #redirect the user to the github login page and ask for access to user data (name, email, ...) -@app.route('/user/github-callback') +@app.route('/user/github-callback') #Github redirects to this link after the user authenticated. Then we use the Token we get from github and request via the github api the username and the userid @github.authorized_handler def authorized(oauth_token): if oauth_token is None: - return "oauth failed, please try again" + return "oauth failed, please try again" #If you call this page manual you get this error headers = {'Authorization': 'token ' + oauth_token,} #Useragent doesn't matters, but is set here githubResponse = get("https://api.github.com/user", headers=headers).text userID = str(json.loads(githubResponse)['id']) username = str(json.loads(githubResponse)['login']) - resp = make_response(redirect('/')) - resp.set_cookie('userID', userID) + resp = make_response(redirect('/')) #redirect the user at the end back to the main page + resp.set_cookie('userID', userID) #set the cookies with username and userid resp.set_cookie('username', username) return resp @app.route('/user/logout') def logout(): resp = make_response("logout successful") - resp.set_cookie('userID', "", max_age=0) + resp.set_cookie('userID', "", max_age=0) #Set the max age of the cookies to 0, this means delete the cookies. resp.set_cookie('username', "", max_age=0) return resp -@app.route('/user/links') +@app.route('/user/links')#This function gives the user the posibility to see and delete his links def ownLinks(): try: - userID = request.cookies.get('userID') - loginbar = "Hello " + request.cookies.get('username') + ' (<a href="/user/logout" style="color:white">logout</a>)' + userID = request.cookies.get('userID') #Get the userid from the cookie + loginbar = "Hello " + request.cookies.get('username') + ' (<a href="/user/logout" style="color:white">logout</a>)' #This is the loginbar except: - return redirect("/user/login") + return redirect("/user/login") #If user is not logged in redirect him to the login page - with sqlite3.connect('db/urls.db') as conn: #Get the original URL from the database + 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]) - response = '<table id="t01">\n<tr>\n<th>Long URL</th>\n<th>Short URL</th>\n<th>Action</th>\n</tr>\n' + 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>Action</th>\n</tr>\n' #This is the layout of the table try: entriesList = res.fetchall() - for entries in entriesList: + for entries in entriesList: #for every entrie in the database add a line to the table 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' - - if(len(entriesList) == 0): response = 'you have no shorten links. <a href="/">back</a>' + response = response + "</table>" #Close the table + if(len(entriesList) == 0): response = 'you have no shorten links. <a href="/">back</a>' #If user has no shorten links make this message with a back button except: - abort(500) - response = response + "</table>" - return render_template('editEntries.html', content=response, loginbar=loginbar) + abort(500) #Shouldn't happen, 500 means internal server error + return render_template('editEntries.html', content=response, loginbar=loginbar) #Put the table and the login div inside the template and server it to the user -@app.route('/user/delete') +@app.route('/user/delete') #This function is called if a user deletes an entrie def delete(): try: - userID = request.cookies.get('userID') - loginbar = "Hello " + request.cookies.get('username') + ' (<a href="/user/logout" style="color:white">logout</a>)' + userID = request.cookies.get('userID') #get the userid from the cookie + loginbar = "Hello " + request.cookies.get('username') + ' (<a href="/user/logout" style="color:white">logout</a>)' # generate the login form except: - return redirect("/user/login") - linkToDelete = request.args.get('link') + 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 sqlite3.connect('db/urls.db') as conn: #Get the original URL from the database + with sqlite3.connect('db/urls.db') as conn: cursor = conn.cursor() try: - cursor.execute('DELETE FROM WEB_URL WHERE SHORT_URL=? AND USERNAME=?', [linkToDelete, userID]) - return redirect('/user/links') + 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)