def user(user_id): user = User.query.filter_by(id=user_id).first_or_404() profile = user.profile total_friends = len(get_friends(user.id)) friends = get_friends(user.id) limited_friends = friends[:6] user_id_1 = session["current_user"]["id"] notifications = get_notifications(user_id_1) are_friends, is_pending_sent, is_pending_recieved = are_friends_or_pending( user_id_1, user_id) conversation_id = conversation_exists(user.id, user_id_1) return render_template('profile.html', user=user, profile=profile, total_friends=total_friends, are_friends=are_friends, is_pending_sent=is_pending_sent, is_pending_recieved=is_pending_recieved, friends=friends, notifications=notifications, limited_friends=limited_friends, conversation_id=conversation_id)
def get_user_data(user, current_user): are_friends, is_pending_sent, is_pending_received = are_friends_or_pending( current_user.id, user.id) friends = get_friends(user.id) limited_friends = friends[:6] total_friends = len(friends) notifications = get_notifications(current_user.id) conversation = conversation_exists(user.id, current_user.id) age = calculate_age(user.birthday) return are_friends, is_pending_sent, is_pending_received, friends, limited_friends, total_friends, notifications, conversation, age
def delete_friend_request(friend_id): are_friends, is_pending_sent, is_pending_received = are_friends_or_pending( current_user.id, friend_id) if is_pending_received: relationship = Friends.query.filter_by( user_id_1=friend_id, user_id_2=current_user.id, status=FriendStatus.requested).first() db.session.delete(relationship) db.session.commit() return "Request removed." else: return "You cannot remove this request."
def delete_friend_request(): """Delete a friend request from another user""" user_id_1 = session["current_user"]["id"] user_id_2 = request.form.get("user_id_2") are_friends, is_pending_sent, is_pending_recieved = are_friends_or_pending( user_id_1, user_id_2) if is_pending_recieved: relationship = Friends.query.filter_by( user_id_1=user_id_2, user_id_2=user_id_1, status=FriendStatus.requested).first() db.session.delete(relationship) db.session.commit() return "Request removed" else: return "You cannot remove this request"
def add_friend(friend_id): are_friends, is_pending_sent, is_pending_received = are_friends_or_pending( current_user.id, friend_id) if current_user.id == friend_id: return "You cannot add yourself as a friend." elif are_friends: return "You are already friends." elif is_pending_sent: return "Your friend request is pending." elif is_pending_received: return "You have already received a request from this user." else: friend_request = Friends(user_id_1=current_user.id, user_id_2=friend_id, status=FriendStatus.requested) db.session.add(friend_request) db.session.commit() return "Request sent."
def friends(user_id): """Show all friends""" user = User.query.filter_by(id=user_id).first_or_404() user_id_1 = session["current_user"]["id"] are_friends, is_pending_sent, is_pending_recieved = are_friends_or_pending( user_id_1, user_id) friends = get_friends(user_id) limited_friends = friends[:6] total_friends = len(friends) notifications = get_notifications(session["current_user"]["id"]) return render_template("friends.html", user=user, friends=friends, total_friends=total_friends, are_friends=are_friends, is_pending_sent=is_pending_sent, is_pending_recieved=is_pending_recieved, notifications=notifications, limited_friends=limited_friends)
def accept_friend(friend_id): are_friends, is_pending_sent, is_pending_received = are_friends_or_pending( current_user.id, friend_id) if current_user.id == friend_id: return "You cannot add yourself as a friend." elif are_friends: return "You are already friends." elif is_pending_received: friend_request = Friends.query.filter_by( user_id_1=friend_id, user_id_2=current_user.id, status=FriendStatus.requested).first() friend_request.status = FriendStatus.accepted db.session.commit() return jsonify({ "status": "Request accepted.", "user": current_user.serialize() }) else: return "An error occured."
def accept_friend(): """Accept a friend request from another user""" user_id_1 = session["current_user"]["id"] user_id_2 = request.form.get("user_id_2") are_friends, is_pending_sent, is_pending_recieved = are_friends_or_pending( user_id_1, user_id_2) if user_id_1 == user_id_2: return "You cannot add yourself as a friend" elif are_friends: return "You are already friends" elif is_pending_recieved: friend_request = Friends.query.filter_by( user_id_1=user_id_2, user_id_2=user_id_1, status=FriendStatus.requested).first() friend_request.status = FriendStatus.accepted db.session.commit() return "Request accepted" else: return "An error occured"
def unfriend(friend_id): are_friends, is_pending_sent, is_pending_received = are_friends_or_pending( current_user.id, friend_id) if are_friends: relationship = Friends.query.filter_by( user_id_1=current_user.id, user_id_2=friend_id, status=FriendStatus.accepted).first() if relationship is None: relationship = Friends.query.filter_by( user_id_1=friend_id, user_id_2=current_user.id, status=FriendStatus.accepted).first() db.session.delete(relationship) db.session.commit() return jsonify({ "status": "Sucessfully unfriended.", "user": current_user.serialize() }) else: return "You cannot unfriend this user."
def unfriend(): """Unfriend another user""" user_id_1 = session["current_user"]["id"] user_id_2 = request.form.get("friend_id") are_friends, is_pending_sent, is_pending_recieved = are_friends_or_pending( user_id_1, user_id_2) if are_friends: relationship = Friends.query.filter_by( user_id_1=user_id_1, user_id_2=user_id_2, status=FriendStatus.accepted).first() if relationship is None: relationship = Friends.query.filter_by( user_id_1=user_id_2, user_id_2=user_id_1, status=FriendStatus.accepted).first() db.session.delete(relationship) db.session.commit() return "Unfriend" else: return "You cannot unfriend this user"
def add_friend(): """Send a friend request to another user""" user_id_1 = session["current_user"]["id"] user_id_2 = request.form.get("user_id_2") are_friends, is_pending_sent, is_pending_recieved = are_friends_or_pending( user_id_1, user_id_2) if user_id_1 == user_id_2: return "You cannot add yourself as a friend" elif are_friends: return "You are already friends" elif is_pending_sent: return "Your friend request is pending" elif is_pending_recieved: return "You have already recieved a request from this user" else: friend_request = Friends(user_id_1=user_id_1, user_id_2=user_id_2, status=FriendStatus.requested) db.session.add(friend_request) db.session.commit() return "Request sent"