def chart_data(): data = request.get_json() email = data['email'] #if 'Authorization' in request.headers: # a_token = request.headers.get('Authorization') #email = database_helper.token_to_email(a_token) result = database_helper.get_user_data(email) print(result) if (result != {}): pageviews = result["pageviews"] loggedin = database_helper.number_of_logged_in() messages = database_helper.number_of_messages() data = json.dumps({ "pageviews": pageviews, "loggedin": loggedin, "messages": messages, "msg": "updatechart" }) print("this is a cry for help") #print(data) print(users) for emails in users.keys(): ws = users[emails] print("users", ws) #if (email in users): print("i am dying") ws.send(data) else: print("Det blev fel här") return create_response(True, "chart data retrieved")
def post_message(): token = request.json['token'] toemail = request.json['email'] message = request.json['message'] if toemail == "": toemail = database_helper.get_email_by_token(token) user = database_helper.get_user(toemail) if len(user) == 0: return json.dumps({ 'success': False, 'message': "Recipient does not exist (postmessage)", 'data': "" }) try: fromemail = database_helper.get_email_by_token(token) except: return json.dumps({ 'success': False, 'message': "Token does not exist", 'data': "" }) result = database_helper.store_message(toemail, fromemail, message) if result: try: ws_dic[toemail].send( str(len(ws_dic.keys())) + ":" + str(database_helper.number_of_messages(toemail)) + ":" + str(database_helper.registered_users())) except: pass return json.dumps({ 'success': result, 'message': "You succesfully posted a message", 'data': message }) else: return json.dumps({ 'success': result, 'message': "Your message was not posted", 'data': "" })
def echo_sockets(): if request.environ.get('wsgi.websocket'): ws = request.environ['wsgi.websocket'] while True: try: message = ws.receive() if message != None: ws_dic[message] = ws print ws_dic for user in ws_dic.keys(): print "logging in " + str(len(ws_dic.keys())) ws_dic[user].send( str(len(ws_dic.keys())) + ":" + str(database_helper.number_of_messages(user)) + ":" + str(database_helper.registered_users())) except WebSocketError: return 'ERROR'
def sign_up(): email = request.json['email'] password = request.json['password'] firstname = request.json['firstname'] familyname = request.json['familyname'] gender = request.json['gender'] city = request.json['city'] country = request.json['country'] if len(email) == 0 or len(firstname) == 0 or len(familyname) == 0 or len( gender) == 0 or len(city) == 0 or len(country) == 0 or len( password) == 0: return json.dumps({ 'success': False, 'message': "No field can be left empty", 'data': "" }) elif len(password) < 8: return json.dumps({ 'success': False, 'message': "Password must be at least 8 characters long", 'data': "" }) result = database_helper.insert_user(email, password, firstname, familyname, gender, city, country) if result == True: for user in ws_dic.keys(): ws_dic[user].send( str(len(ws_dic.keys())) + ":" + str(database_helper.number_of_messages(user)) + ":" + str(database_helper.registered_users())) return json.dumps({ 'success': True, 'message': "You successfully signed up", 'data': "" }) else: return json.dumps({ 'success': False, 'message': "Sign up failed", 'data': "" })
def sign_out(): token = request.json['token'] email = database_helper.get_email_by_token(token) res = database_helper.delete_token(token) if res: del ws_dic[email] for user in ws_dic.keys(): print user print len(ws_dic.keys()) ws_dic[user].send( str(len(ws_dic.keys())) + ":" + str(database_helper.number_of_messages(user)) + ":" + str(database_helper.registered_users())) return json.dumps({ 'success': True, 'message': "You successfully signed out", 'data': "" }) else: return json.dumps({ 'success': False, 'message': "Your are not signed in", 'data': "" })