def log_user_in(un): # record that user is logged in ip = request.remote_addr # user's current ip dbconnect.write( "INSERT INTO stepify.logins (username, ip) VALUES (%s, %s);", ( un, ip, ))
def set_user_sp_and_tasks(study_program): un = check_login() # record user's study program dbconnect.write( "UPDATE stepify.users SET study_program = %s WHERE username = %s;", ( study_program, un, )) # set user's tasks based on study program # first get a list of tasks (as ids) relevant to this study program # Psycopg2 automatically wraps things with %s in single quotes, so I am forced to directly insert a string here # First I will check that the string is one of the 4 options we expect it to be if study_program == 's_e' or study_program == 'i_d' or study_program == 'p_m' or study_program == 'undecided': sql_string = 'SELECT id FROM stepify.tasks WHERE ' + study_program + ' = TRUE;' task_ids = dbconnect.query(sql_string, (), "all") # then get user's id (in users table) user_id = find_user_from_uname(un) # then link the tasks to the user in the users_tasks table for task_id in task_ids: dbconnect.write( "INSERT INTO stepify.users_tasks (user_id, task_id, completion) VALUES (%s, %s, FALSE)", ( str(user_id), str(task_id[0]), ))
def process_signup(u): un = u['username'] if find_user_from_uname(un) is None: # Sign user up dbconnect.write( "INSERT INTO stepify.users (username, password) VALUES (%s, %s);", ( un, u['password'], )) # Log user in log_user_in(un) # Move on to choose your study program return redirect("/choose-your-study-program") else: return 'Sorry, ' + un + ' is already taken. <a href="/">Go back</a>'
def task_undone(): error = None if request.method == 'POST': task_id = str(request.json) # mark task completion as yes in db # get user id uname = check_login() uid = str(find_user_from_uname(uname)) dbconnect.write( "UPDATE stepify.users_tasks SET completion = FALSE WHERE user_id = %s AND task_id = %s;", ( uid, task_id, )) # the code below is executed if the request method was GET return render_template('404.html', error=error)
def task_done_id(): error = None if request.method == 'POST': task_data = request.json # mark task completion as yes in db # get user id uname = check_login() uid = str(find_user_from_uname(uname)) dbconnect.write( "UPDATE stepify.users_tasks SET completion = TRUE, completion_date = TIMESTAMP %s WHERE user_id = %s AND task_id = %s;", ( task_data['compdate'], uid, task_data['taskid'], )) # the code below is executed if the request method was GET return render_template('404.html', error=error)
def log_user_out(un): ip = request.remote_addr # user's current ip # Check if user is logged in logged_in_uname = dbconnect.query( "SELECT username FROM stepify.logins WHERE ip = %s;", (ip, ), "one") # if someone is logged in, fix the uname if logged_in_uname: logged_in_uname = logged_in_uname[0] # if logged in, log out if logged_in_uname == un: dbconnect.write( "DELETE FROM stepify.logins WHERE username = %s AND ip = %s;", ( un, ip, )) else: return redirect("/")