def sign_up(): user = { 'email': request.headers.get('email'), 'password': request.headers.get('password'), 'firstname': request.headers.get('firstname'), 'familyname': request.headers.get('familyname'), 'gender': request.headers.get('gender'), 'city': request.headers.get('city'), 'country': request.headers.get('country'), } # Validate sign up form if not db.validate_user(user): return json.dumps({ 'success': False, 'message': "Form data missing or incorrect type." }) # Check if email is taken if db.get_user(user['email']): return json.dumps({ 'success': False, 'message': "User already exists." }) encrypted_password = sha256_crypt.encrypt(unicode(user['password'])) db.add_user(user['email'], encrypted_password, user['firstname'], user['familyname'], user['gender'], user['city'], user['country']) return json.dumps({ 'success': True, 'message': "Successfully created a new user." })
def websocket(): if request.environ.get('wsgi.websocket'): ws = request.environ['wsgi.websocket'] token = ws.receive() email = database_helper.validate_logged_in(token) #print('got email from validate_logged_in in websocket server.py', email) email = email[0] #print(database_helper.validate_user(token)) if database_helper.validate_user(token): if email in sockets: # oldSocket is the existing entry oldSocket = sockets[email] try: message = {'data': 'sign_out'} oldSocket.send(json.dumps(message)) except WebSocketError as e: print('ERROR (server.py websocket): ', e) del sockets[email] sockets[email] = ws push_websocket_active_users() push_websocket_total_posts() while True: try: ws.receive() except WebSocketError as e: print('ERROR (server.py websocket)', e) return '' return ''
def change_password(): data = request.get_json() token = get_token() if not database_helper.validate_user(None, token): return jsonify({"success": False, "msg": "Unauthorized"}) if database_helper.change_pw(data, token): return jsonify({"success": True, "msg": "password changed"}) else: return jsonify({"success": False, "msg": "password did not change"})
def post_message(): data = request.get_json() token = get_token() if database_helper.find_user(data["email"]) != data["email"]: return jsonify({"success": False, "msg": "could not find user"}) if not database_helper.validate_user(data["email"], token): return jsonify({"success": False, "msg": "Unauthorized"}) if not database_helper.create_post(data, token): return jsonify({"success": False, "msg": "could not send msg"}) return jsonify({"success": True, "msg": "message sent"})
def get_user_messages_by_email(): #data = request.get_json() email = request.args.get('email') token = get_token() if not database_helper.validate_user(email,token): return jsonify({"success":False,"msg":"Unauthorized"}) all_msg = database_helper.get_msg(email,token) if(all_msg == ""): return jsonify({"success":False,"msg":"no messages found"}) return jsonify({"success": True,"msg":"found user messages","data" : all_msg})
def get_user_messages_by_token(): token = get_token() if not database_helper.validate_user(None,token): return jsonify({"success":False,"msg":"Unauthorized"}) #retrieve all your own messages all_msg = database_helper.get_msg(None,token) if all_msg == "": return jsonify({"success":False,"msg":"no messages found"}) #return string as a json object return jsonify({"success": True,"msg":"found user messages","data" : all_msg})
def get_user_data_by_email(): #recieve email and token token = get_token() email = request.args.get('email') if not database_helper.validate_user(None, token): return jsonify({"success": False, "msg": "Unauthorized"}) #check if the user is active result = database_helper.get_data(email, token) if result == None: return jsonify({"success": False, "msg": "no data found"}) #get the result from user table return jsonify({"success": True, "msg": "found user data", "data": result})
def user_sign_out(): token = get_token() email = database_helper.get_data(None,token)[0] try: active_sessions[email][0].send("close") del active_sessions[email] except: print("log out") if not database_helper.validate_user(None,token): return jsonify({"success":False,"msg" : "Unauthorized"}) database_helper.remove_user(token) return jsonify({"success":True,"msg" : "user logged out"})
def get_user_data_by_token(): #recieve token token = get_token() #display all user for the user with the correct token #find the active user with correlating token if not database_helper.validate_user(None, token): return jsonify({"success": False, "msg": "Unauthorized"}) #select data with the correct user result = database_helper.get_data(None, token) if result == None: return jsonify({"success": False, "msg": "Unable to find user"}) return jsonify({"success": True, "msg": "found user data", "data": result})
def sign_up(): email = request.form['email'] password = request.form['password'] firstname = request.form['firstname'] familyname = request.form['familyname'] gender = request.form['gender'] city = request.form['city'] country = request.form['country'] if not dh.validate_user(email): dh.add_user(email, password, firstname, familyname, gender, city, country) return json.dumps({ "success": True, "message": "Successfully created a new user" }) return json.dumps({"success": False, "message": "User already exists"})
def post_message(): token = request.form['token'] message = request.form['message'] reciever_email = request.form['email'] day = request.form['day'] if token in logged_in_users: sender_email = logged_in_users[token] if dh.validate_user(reciever_email): print "sending messageupdate with ws" dh.add_message(reciever_email, sender_email, message, day) if reciever_email in websockets: ws = websockets[reciever_email] data = dh.get_user_messages(reciever_email) ws.send( json.dumps({ "action": "updatemessages", "message": "Messages stats updated", "data": data })) return json.dumps({"success": True, "message": "Message posted"}) else: return json.dumps({"success": False, "message": "No such user"}) return json.dumps({"success": False, "message": "You are not signed in"})
def user_sign_out(): token = get_token() if not database_helper.validate_user(None, token): return jsonify({"success": False, "msg": "Unauthorized"}) database_helper.remove_user(token) return jsonify({"success": True, "msg": "user logged out"})