def searching(): user = User() logs = json.loads(session['log']) if user.is_complete(logs['id']): return render_template('search.html') else: return render_template('print.html', title="Profile incomplete", values=["Please complete your profile"])
def confirm_email(token): us = User() try: us.confirm_token(token) except: return render_template('print.html', title='Wrong token', values=['Your email could not be confirmed']) return render_template('print.html', title='Account confirmed', values=['Your account has been confirmed'])
def delete_profile(): user_id = request.args.get('id') log = json.loads(session['log']) if log['id'] == user_id or log['admin'] == 1: us = User() us.delete(user_id) return jsonify()
def set_connected(): user = User() if session['log'] and session['log'] != '': status = request.args.get('status') user.set_connected(json.loads(session['log'])['id'], int(status)) return jsonify(result=[True]) return jsonify(result=[False])
def report_user(): us = User() user_id = request.args.get('id') info = json.loads(session['log']) ct = us.count_query('''DELETE FROM reports WHERE to_id = ? AND from_id = ?''', (user_id, info['id'])) if ct == 0: us.query('''INSERT INTO reports(to_id, from_id) VALUES(?, ?)''', (user_id, info['id'])) return jsonify(result=[])
def login_validation(): user = User() validity = user.auth(request.form['login'], request.form['password']) if validity: session['log'] = user.to_json() return redirect(url_for('chat')) else: return render_template('print.html', title="Login error", values=["An error occurred during your login attempt."])
def block_user(): us = User() user_id = request.args.get('id') info = json.loads(session['log']) res = False ct = us.count_query('''DELETE FROM blocks WHERE to_id = ? AND from_id = ?''', (user_id, info['id'])) if ct == 0: res = True us.query('''INSERT INTO blocks(to_id, from_id) VALUES(?, ?)''', (user_id, info['id'])) return jsonify(result=res)
def handle_mess(msg): send_user_id = json.loads(session['log'])['id'] user = User() if not user.is_matched(send_user_id, msg['rec_user_id']): return chat_socket = Chat(send_user_id, msg['rec_user_id']) chat_socket.send(msg['message']) private_room = chat_socket.get_room() socketio.emit('mess_received', msg, room=private_room) notif_inst = Notif(send_user_id) notif_inst.send(msg['rec_user_id'], 'message')
def matches(): user = User() user_matches = user.matched(json.loads(session['log'])['id']) user_likes = user.likes(json.loads(session['log'])['id']) my_likes = user.my_likes(json.loads(session['log'])['id']) arr_match = np.array(user_matches) if len(arr_match) > 0: user_likes = [likes for likes in user_likes if str(likes[2]) not in arr_match[:, 2]] my_likes = [likes for likes in my_likes if str(likes[2]) not in arr_match[:, 2]] vis = Visit(json.loads(session['log'])['id']) visits = vis.get_all() return render_template('matches.html', matches=user_matches, likes=user_likes, my_likes=my_likes, visits=visits)
def get_messages(): current_id = json.loads(session['log'])['id'] chat_user_id = request.args.get('target_user_id') chat_socket = Chat(current_id, chat_user_id) messages = chat_socket.get() notif_inst = Notif(current_id) notif_inst.flush(chat_user_id, 'message') messages = format_timestamp(messages, current_id) user = User() if len(messages) > 0 and user.is_matched(current_id, chat_user_id): return jsonify(result=messages) return jsonify(result=[])
def populate_db(): nb = 550 result = populate(nb).json() user = User() ret = [] for res in result['results']: ret.append(user.populate(res)) vis = Visit(0) vis.random(6 * nb) lik = Like() lik.random(6 * nb) hash = Hashtag() hash.random(3 * nb) return jsonify(result=ret)
def register_check_input(): user = User() user.dict_to_obj(request.form) error = user.check_password() error.update(user.check_username()) error.update(user.check_email()) liste = list() if True in error.values(): for key, value in error.items(): if value is True: liste.append(key) else: user.register() liste.append("You were successfully registered.") liste.append("An email was sent for account confirmation.") return render_template('print.html', title="User Creation", values=liste)
def treatlocation(): user = User() info = json.loads(session['log']) user.dict_to_obj(info) longitude = request.form['longitude'] latitude = request.form['latitude'] response = requests.get("https://nominatim.openstreetmap.org/reverse?format=json&zoom=10&" + "lat=" + latitude + "&lon=" + longitude) result = response.json() city = result["address"]["city"] user.updatelocation(longitude, latitude) user.update_city(city) return ""
def create_user(first_name, last_name, email, password): """ Creates a new user with the provided credentials, and returns a token. """ if User.by_email(email): raise exceptions.Conflict(description='User already exists.') user = UserFactory.instance.create(first_name, last_name, email, password).save() token = generate_token_for_user(user) return jsonify(snake_to_camel_case_dict({'token': token.decode("utf-8")}))
def get_hashtags(): user = User() if 'login' not in request.args: info = json.loads(session['log']) user.dict_to_obj(info) else: user.dict_to_obj({'id': request.args.get('login')}) val = user.get_hashtags() val = json.dumps(val) return val
def delete_hashtag(): user = User() info = json.loads(session['log']) user.dict_to_obj(info) my_hash = request.form['hashtag'] user.delete_hashtag(my_hash) return jsonify(result=[])
def search_user(): lat = float(json.loads(session['log'])['latitude']) lng = float(json.loads(session['log'])['longitude']) dist = float(request.args.get('distance')) minlat = lat - degrees(dist / 6371) maxlat = lat + degrees(dist / 6371) minlng = lng - degrees(dist / 6371 / cos(radians(lat))) maxlng = lng + degrees(dist / 6371 / cos(radians(lat))) data = { 'login': request.args.get('login'), 'popularity': [request.args.get('min_popularity'), request.args.get('max_popularity')], 'age': age_to_date(request.args.get('min_age'), request.args.get('max_age')), 'distance': [minlat, maxlat, minlng, maxlng], 'radius': dist, 'sort': request.args.get('sort').lower(), 'location': [lat, lng], 'matching': request.args.get('matching'), 'hashtag': request.args.getlist('hashtag[]') } user = User() res = user.search(data, json.loads(session['log'])['id']) return jsonify(result=res, debug=data)
def profile(): user = User() info = json.loads(session['log']) user.dict_to_obj(info) session['log'] = user.to_json() info = json.loads(session['log']) profile_pic = user.query('''SELECT * FROM pics WHERE id = ?''', (info['pic_id'],)) pics = user.query('''SELECT * FROM pics WHERE user_id = ? AND id != ?''', (info['id'], info['pic_id'])) info['pics'] = pics if len(profile_pic) > 0: info['profile_pic'] = profile_pic[0] return render_template('profil.html', info=info)
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, first_name=form.first_name.data, last_name=form.last_name.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash('Your account has been created! You are now able to log in', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('home')) user = User.verify_reset_token(token) if user is None: flash('That is an invalid or expired token', 'warning') return redirect(url_for('reset_request')) form = RequestResetForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Your Password has been updated! You are now able to log in', 'success') return redirect(url_for('login')) return render_template('reset_token.html', title='Reset Password', form=form)
def create_user(form, hashed_password): new_user = False username_check = User.query.filter_by(username=form.username.data).first() email_check = User.query.filter_by(email=form.email.data).first() if username_check or email_check: if username_check: flash( f'A user with that username already exists, please choose another', 'danger') if email_check: flash( f'A user with that email already exists, please choose another', 'danger') else: new_user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(new_user) db.session.commit() new_user = True return new_user
def store_socket(): current_id = json.loads(session['log'])['id'] user = User() user.update_socket(current_id, '')
def store_socket(): current_id = json.loads(session['log'])['id'] socket_id = request.sid user = User() user.update_socket(current_id, socket_id)
def get_connected(): user = User() connected_users = user.get_connected() if len(connected_users) > 0: return jsonify(connected=connected_users) return jsonify(connected=[])
def login_update(var): user = User() info = json.loads(session['log']) user.dict_to_obj(info) if var == "login": user.login = request.form['login'] message = user.update_login() elif var == "firstname": user.firstname = request.form["firstname"] message = user.update_firstname() elif var == "lastname": user.lastname = request.form['lastname'] message = user.update_lastname() elif var == "password": user.oldpassword = request.form['oldpassword'] user.password = request.form['password'] user.repeatpassword = request.form['repeatpassword'] message = user.update_password() elif var == "email": user.email = request.form['email'] message = user.update_email() elif var == "gender": user.gender = request.form['gender'] message = user.update_gender() elif var == "orientation": user.orientation = request.form['orientation'] message = user.update_orientation() elif var == "bio": user.bio = request.form['bio'] message = user.update_bio() elif var == "birthdate": user.birthdate = request.form['birthdate'] message = user.update_birthdate() else: message = '' session['log'] = user.to_json() return message
def set_profile_pic(): user = User() val = user.set_profile_pic(json.loads(session['log'])['id'], request.args.get('pic_id')) return jsonify(pics=val)
def other_profile(username): user = User() logged = json.loads(session['log']) my_id = logged['id'] if not user.exists(username): return render_template('print.html', title='Error', values=['This user does not exist']) if username != logged['login']: vis = Visit(my_id) vis.add_visit(username) user.dict_to_obj(logged) session["log"] = user.to_json() logged = json.loads(session['log']) session["user_id"] = '' res = user.query('''SELECT users.id, login, gender, email, pic_id, popularity, firstname, lastname, longitude, latitude, bio, \ orientation, birthdate, city FROM users WHERE login = ?''', (username,)) profile_pic = user.query('''SELECT * FROM pics WHERE id = ?''', (res[0][4],)) pics = user.query('''SELECT * FROM pics WHERE user_id = ? AND id != ?''', (res[0][0], res[0][4])) info = {'id': res[0][0], 'login': res[0][1], 'gender': res[0][2], 'email': res[0][3], 'pic_id': res[0][4], 'popularity': res[0][5], 'firstname': res[0][6], 'lastname': res[0][7], 'longitude': res[0][8], 'latitude': res[0][9], 'bio': res[0][10], 'orientation': res[0][11], "birthdate": res[0][12], "city": res[0][13], "pics": pics, "likes": user.does_like(my_id, username), "blocks": user.does_block(my_id, username)} if len(profile_pic) > 0: info.update({'profile_pic': profile_pic[0]}) return render_template('profil.html', info=info, user=logged)
def del_pics(): user = User() val = user.del_pics(json.loads(session['log'])['id'], request.args.get('pic_id')) return jsonify(result=val)
def get_pics(): user = User() val = user.get_pics(json.loads(session['log'])['id']) return jsonify(pics=val)
def update(): user = User() info = json.loads(session['log']) user.dict_to_obj(info) return render_template('update.html', info=info)