def get_graph_data(token): email = database_helper.get_user_by_token(token)[0] num_users = database_helper.get_number_of_active() total_users = database_helper.get_number_of_users() user_visits = database_helper.get_user_visits_by_email(email) total_visits = database_helper.get_total_visits() total_messages = database_helper.get_message_count(email) recent_count = [0 for x in range(recent_messages_entries)] for item in database_helper.get_recent_messages( email, recent_messages_timewindow): recent_count[(int(time.time()) - item[2]) // recent_messages_resolution] += 1 user_update = {"current": num_users, "total": total_users} visit_update = {"user": user_visits, "total": total_visits} message_update = {"messagecount": total_messages, "recent": recent_count} data_update = { "users": user_update, "visits": visit_update, "messages": message_update } return json.dumps({ "success": True, "message": "Graph data.", "data": data_update })
def get_user_data_by_token(): hashedData = request.headers.get('hashedData') email = request.json['email'] serverHash = server_hash(email, email) if (serverHash == hashedData): token = database_helper.get_loggedIn_by_email(email)[0][0] user = database_helper.get_user_by_token(token) if (user != None): return json.dumps({ 'success': True, 'message': 'User data received', 'data': user }) else: return json.dumps({ 'success': False, 'message': 'User does not exist or You are not signed in' }) else: return json.dumps({ 'success': False, 'message': 'Authentication failed' })
def sign_out(): token=request.values.get('token') user = database_helper.get_user_by_token(token) if(len(user) == 0): return jsonify(success = False, message = "You are not signed in") else: resp=database_helper.logout(token) return jsonify(success = True, message = "Successfully logged out")
def check_password(token, password): result = database_helper.get_user_by_token(token) salt = result[7] password_hash = hashlib.sha512(password + salt).hexdigest() if result: return password_hash == result[1] else: return False
def send_file(): token = request.values.get("token") filename = request.values.get("filename") user = database_helper.get_user_by_token(token) print "[send_file] user:"******"[send_file] filename:" + filename if user[0][0] != None: return send_from_directory(os.path.join(app.root_path, app.config["UPLOAD_FOLDER"]), filename) else: return " "
def get_user_messages_by_token(): token = request.values.get('token') user = database_helper.get_user_by_token(token) if len(user) > 0: messages = database_helper.get_messages(user[0][0]) if len(messages) > 0: return jsonify(success = True, data = messages, message = "Successfully got user data") else: return jsonify(success = False, message = "User has no messages") else: return jsonify(success = False, message = "You're not signed in")
def get_user_messages_by_token(): token = request.values.get("token") user = database_helper.get_user_by_token(token) if len(user) > 0: messages = database_helper.get_messages(user[0][0]) if len(messages) > 0: return json.dumps({"success": True, "data": messages, "message": "Successfully got user data"}) else: return jsonify(success=False, message="User has no messages") else: return jsonify(success=False, message="You're not signed in")
def get_user_data_by_token(): token = request.values.get('token') user = database_helper.get_user_by_token(token) if len(user) > 0: userdata = database_helper.get_user_by_email(user[0][0]) if len(userdata) > 0: return jsonify(success = True, message = "Data successfully got", data = [userdata[0][0], userdata[0][2], userdata[0][3], userdata[0][4], userdata[0][5], userdata[0][6]]) else: return jsonify(success = False, message = "Something went terribly wrong, this shouldn't happen") else: return jsonify(success = False, message = "You're not logged in")
def get_user_data_by_email(): email = request.values.get('email') token = request.values.get('token') userFromToken = database_helper.get_user_by_token(token) if len(userFromToken) > 0: userdata = database_helper.get_user_by_email(email) if len(userdata) > 0: print "asd" return jsonify(data = [userdata[0][0], userdata[0][2], userdata[0][3], userdata[0][4], userdata[0][5], userdata[0][6]]) else: return jsonify(message = "No such user") else: return jsonify(message = "You're not signed in")
def change_password(): token = request.values.get('token') old_pw = request.values.get('old_password') new_pw = request.values.get('new_password') user = database_helper.get_user_by_token(token) if len(user) <= 0: return jsonify(success = False, message = "You are not logged in") userpw = database_helper.get_user_by_email(user[0][0]) if old_pw == userpw[0][1]: database_helper.change_password(user[0][0], new_pw) return jsonify(success = True, message = "Password changed") else: return jsonify(success = False, message = "Wrong password")
def sign_out(): print('Sign out') token = request.args.get('token', '') logged = database_helper.user_logged_by_token(token=token) if logged: email = database_helper.get_user_by_token(token) del id_socket[str(email)] out = database_helper.sign_out(token=token) if out: send_notification() return json.dumps({'success' : True, 'message': 'User unlogged'}) else: return json.dumps({'success' : False, 'message': 'Failed to unlogged user'}) else: return json.dumps({'success' : True, 'message': 'User already logged out or nonexistent'})
def post_message(): token = request.values.get('token') message = request.values.get('message') reciever = request.values.get('email') sender = database_helper.get_user_by_token(token) if(token != None and message != None and reciever != None): if len(sender) > 0: tmp = database_helper.get_user_by_email(reciever) if len(tmp) > 0: database_helper.post_message(sender[0][0], reciever, message) return jsonify(success = True, message = "Successfully posted message") else: return jsonify(success = False, message = "No such user") else: return jsonify(success = False, message = "You're not signed in") else: return jsonify(success = False, message = "Wrong input data")
def get_graph_data(token): email = database_helper.get_user_by_token(token)[0] num_users = database_helper.get_number_of_active() total_users = database_helper.get_number_of_users() user_visits = database_helper.get_user_visits_by_email(email) total_visits = database_helper.get_total_visits() total_messages = database_helper.get_message_count(email) recent_count = [0 for x in range(recent_messages_entries)] for item in database_helper.get_recent_messages(email, recent_messages_timewindow): recent_count[(int(time.time()) - item[2]) // recent_messages_resolution] += 1 user_update = {"current": num_users, "total": total_users} visit_update = {"user": user_visits, "total":total_visits} message_update = {"messagecount": total_messages, "recent": recent_count} data_update = {"users": user_update, "visits": visit_update, "messages": message_update} return json.dumps({"success": True, "message": "Graph data.", "data": data_update})
def post_message(): token = request.values.get("token") message = request.values.get("message") reciever = request.values.get("email") sender = database_helper.get_user_by_token(token) if token != None and message != None and reciever != None: if len(sender) > 0: tmp = database_helper.get_user_by_email(reciever) if len(tmp) > 0: print "sender: " + sender[0][0] + " reciever: " + reciever + " message: " + message database_helper.post_message(sender[0][0], reciever, message, "no") return jsonify(success=True, message="Successfully posted message") else: return jsonify(success=False, message="No such user") else: return jsonify(success=False, message="You're not signed in") else: return jsonify(success=False, message="Wrong input data")
def sign_out(): token = request.values.get("token") type_ = request.values.get("type") user = database_helper.get_user_by_token(token) print "[sign_out] user: "******"You are not signed in") else: resp = database_helper.logout(token) try: if type_ == "normal": del curr_connections[user[0][0]] print "[sign_out] del:" else: print "[sign_out] autologout:" except: print "[sign_out] except:" return jsonify(success=True, message="Successfully logged out")
def upload_file(): if request.method == "POST": print "[upload_file] entered" token = request.values.get("token") message = request.values.get("message") reciever = request.values.get("reciever") sender = database_helper.get_user_by_token(token) file = request.files["upload_file"] print "[upload_file] data retrieved" if token != None and reciever != None: if len(sender) > 0: tmp = database_helper.get_user_by_email(reciever) if len(tmp) > 0: characters = "abcdefghiklmnopqrstuvwwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" name = "" for i in range(0, 35): name += characters[randint(0, len(characters) - 1)] print "[upload_file] name generated" print "[upload_file] message posted" if file and allowed_file(file.filename): print "[upload_file] inside if before split" print file.filename filetype = file.filename.rsplit(".", 1)[1] print filetype print "[upload_file] filename splitted" message = name + "." + filetype file.save(os.path.join(app.root_path, app.config["UPLOAD_FOLDER"], message)) print "[upload_file] save file" print "sender: " + sender[0][0] + " reciever: " + reciever + " message: " + message database_helper.post_message(sender[0][0], reciever, message, filetype) print "[upload_file] save to database" return jsonify(success=True, message="File uploaded and shared!") else: return jsonify(success=False, message="No such user") else: return jsonify(success=False, message="You're not signed in") else: return jsonify(success=False, message="Wrong input data") return ""
def get_user_data_by_email(): email = request.values.get("email") token = request.values.get("token") userFromToken = database_helper.get_user_by_token(token) if len(userFromToken) > 0: userdata = database_helper.get_user_by_email(email) if len(userdata) > 0: return jsonify( success=True, message="Data successfully retrieved", email=userdata[0][0], fname=userdata[0][2], lname=userdata[0][3], gender=userdata[0][4], city=userdata[0][5], country=userdata[0][6], ) else: return jsonify(success=False, message="No such user") else: return jsonify(success=False, message="You're not signed in")
def get_user_data_by_token(): token = request.values.get("token") user = database_helper.get_user_by_token(token) if len(user) > 0: userdata = database_helper.get_user_by_email(user[0][0]) if len(userdata) > 0: return jsonify( success=True, message="Data successfully retrieved", email=userdata[0][0], fname=userdata[0][2], lname=userdata[0][3], gender=userdata[0][4], city=userdata[0][5], country=userdata[0][6], ) else: return jsonify(success=False, message="Something went terribly wrong, this shouldn't happen") else: return jsonify(success=False, message="You're not logged in")