def block(to_username): ref = request.referrer logged_user = User.getByName(session.get('username')) to_user = User.getByName(to_username) if to_user: rel_status = Relationship.get_relationship_status(logged_user.id, to_user.id) if rel_status == "FRIENDS_PENDING" \ or rel_status == "FRIENDS_APPROVED" \ or rel_status == "REVERSE_FRIENDS_PENDING": rel = Relationship.get_relationship(logged_user.id, to_user.id) rel.delete_record() reverse_rel = Relationship.get_relationship(to_user.id, logged_user.id) reverse_rel.delete_record() Relationship( from_user=logged_user.id, to_user=to_user.id, rel_type=RELATIONSHIP_TYPE.get(BLOCKED), status=STATUS_TYPE.get(APPROVED) ).save_database() if ref: return redirect(ref) else: return redirect(url_for('user_blueprint.profile', username=to_user.username)) else: abort(404)
def _get_relationship_between_logged_user_and_viewing_user_profile(user): username = Session.get_session_by_name("username") if username: logged_user = User.objects.filter(username=username).first() return Relationship.get_relationship(logged_user, user)
def block(to_username): ref = request.referrer logged_user = User.objects.filter(username=session.get('username')).first() to_user = User.objects.filter(username=to_username).first() if to_user: rel = Relationship.get_relationship(logged_user, to_user) to_username = to_user.username if rel == "FRIENDS_PENDING" or rel == "FRIENDS_APPROVED" or rel == "REVERSE_FRIENDS_PENDING": rel = Relationship.objects.filter(from_user=logged_user, to_user=to_user).delete() reverse_rel = Relationship.objects.filter( from_user=to_user, to_user=logged_user).delete() Relationship(from_user=logged_user, to_user=to_user, rel_type=Relationship.BLOCKED, status=Relationship.APPROVED).save() if ref: return redirect(ref) else: return redirect( url_for('user_app.profile', username=to_user.username)) else: abort(404)
def add_friend(to_username): logged_user = User.objects.filter(username=session.get('username')).first() to_user = User.objects.filter(username=to_username).first() if to_user: rel = Relationship.get_relationship(logged_user, to_user) to_username = to_user.username if rel == "REVERSE_FRIENDS_PENDING": Relationship( from_user=logged_user, to_user=to_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED ).save() reverse_rel = Relationship.objects.get( from_user=to_user, to_user=logged_user ) reverse_rel.status=Relationship.APPROVED reverse_rel.save() elif rel == None and rel != "REVERSE_BLOCKED": Relationship( from_user=logged_user, to_user=to_user, rel_type=Relationship.FRIENDS, status=Relationship.PENDING ).save() return redirect(url_for('user_app.profile', username=to_username)) else: abort(404)
def add_friend(to_username): ref = request.referrer logged_user = User.objects.filter(username=session.get('username')).first() to_user = User.objects.filter(username=to_username).first() if to_user: to_username = to_user.username rel = Relationship.get_relationship(logged_user, to_user) if rel == "FRIENDS_PENDING": return rel elif rel == "BLOCKED": return rel elif rel == "FRIENDS_APPROVED": return rel elif rel == "REVERSE_FRIENDS_PENDING": #Friend has approved Relationship(from_user=logged_user, to_user=to_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED).save() reverse_rel = Relationship.objects.get(from_user=to_user, to_user=logged_user) reverse_rel.status = Relationship.APPROVED reverse_rel.save() #return "FRIENDS_APPROVED" elif rel == None and rel != "REVERSE_BLOCKED": #notFriend, notBlocked Relationship(from_user=logged_user, to_user=to_user, rel_type=Relationship.FRIENDS, status=Relationship.PENDING).save() return "FRIENDSHIP_REQUESTED" return redirect(url_for('user_app.profile', username=to_username)) else: abort(404)
def block(to_username): ref = request.referrer logged_user = User.objects.filter(username=session.get('username')).first() to_user = User.objects.filter(username=to_username).first() if to_user: rel = Relationship.get_relationship(logged_user, to_user) to_username = to_user.username if rel == "FRIENDS_PENDING" or rel == "FRIENDS_APPROVED" or rel == "REVERSE_FRIENDS_PENDING": rel = Relationship.objects.filter( from_user=logged_user, to_user=to_user).delete() reverse_rel = Relationship.objects.filter( from_user=to_user, to_user=logged_user).delete() Relationship( from_user=logged_user, to_user=to_user, rel_type=Relationship.BLOCKED, status=Relationship.APPROVED ).save() if ref: return redirect(ref) else: return redirect(url_for('user_app.profile', username=to_user.username)) else: abort(404)
def _is_there_relationship_between_users(logged_user, to_user): relationship_type = Relationship.get_relationship(logged_user, to_user) return relationship_type == constants.PENDING_REQUEST or \ relationship_type == constants.FRIEND_APPROVED or \ relationship_type == constants.REVERSED_BLOCK or \ relationship_type == constants.REVERSE_PENDING_FRIEND_REQUEST or \ relationship_type == constants.FRIENDS_PENDING or constants.BLOCKED
def post_message_to_all_friends_feeds(self): friends = self._get_all_my_friends_obj() for friend in friends: rel_status = Relationship.get_relationship( friend.to_user, self._message_obj.to_user) if rel_status != constants.BLOCKED: Feed(user=friend.to_user, message=self._message_obj).save() return True
def add_friend(to_username): ref = request.referrer logged_user = User.getByName(session.get('username')) to_user = User.getByName(to_username) if to_user: rel_status = Relationship.get_relationship_status(logged_user.id, to_user.id) if rel_status == "REVERSE_FRIENDS_PENDING": Relationship( from_user=logged_user.id, to_user=to_user.id, rel_type=RELATIONSHIP_TYPE.get(FRIENDS), status=STATUS_TYPE.get(APPROVED) ).save_database() reverse_rel = Relationship.get_relationship(to_user.id, logged_user.id) reverse_rel.status = STATUS_TYPE.get(APPROVED) reverse_rel.update_record() elif rel_status is None: # and rel_status != "REVERSE_BLOCKED" Relationship( from_user=logged_user.id, to_user=to_user.id, rel_type=RELATIONSHIP_TYPE.get(FRIENDS), status=STATUS_TYPE.get(PENDING) ).save_database() # email the user body_html = render_template( 'mail/relationship/added_friend.html', from_user=logged_user, to_user=to_user, ) body_text = render_template( 'mail/relationship/added_friend.txt', from_user=logged_user, to_user=to_user, ) email(to_user.email, ("%s has requested to be friends") % logged_user.first_name, body_html, body_text) if ref: return redirect(ref) else: return redirect(url_for('user_blueprint.profile', username=to_user.username)) else: abort(404)
def Unblock(to_username): logged_user = User.objects.filter(username=session.get('username')).first() to_user = User.objects.filter(username=to_username).first() if to_user: rel = Relationship.get_relationship(logged_user, to_user) if rel == "BLOCKED": rel = Relationship.objects.filter(from_user=logged_user, to_user=to_user).delete() return redirect(url_for('user_app.profile', username=to_username)) else: abort(404)
def unblock(username): logged_user, to_user = _get_requesting_user_and_logged_in_user_obj( username) if to_user: rel = Relationship.get_relationship(logged_user, to_user) if rel == constants.BLOCKED: Relationship.delete_connection_between_users(logged_user, to_user) return _redirect_to_referred_url_if_safe(username) abort(404)
def profile(username): edit_profile = False rel = None user = User.objects.filter(username=username).first() if user.username == session.get('username') and session.get('username'): edit_profile = True if user: if session.get('username'): logged_user = User.objects.filter(username=session.get('username')).first() rel = Relationship.get_relationship(logged_user, user) return render_template('user/profile.html', user=user, rel=rel, edit_profile=edit_profile) else: abort(404)
def process_message(message): # Get the from users friends from_user = message.from_user friends = Relationship.objects.filter(from_user=from_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) for friend in friends: rel = Relationship.get_relationship(friend.to_user, message.to_user) if rel != 'BLOCKED': feed = Feed(user=friend.to_user, message=message).save() return True
def add_friend(to_username): ref = request.referrer logged_user = User.objects.filter(username=session.get('username')).first() to_user = User.objects.filter(username=to_username).first() if to_user: rel = Relationship.get_relationship(logged_user, to_user) to_username = to_user.username if rel == "REVERSE_FRIENDS_PENDING": Relationship( from_user=logged_user, to_user=to_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED ).save() reverse_rel = Relationship.objects.get( from_user=to_user, to_user=logged_user) reverse_rel.status=Relationship.APPROVED reverse_rel.save() elif rel == None and rel != "REVERSE_BLOCKED": Relationship( from_user=logged_user, to_user=to_user, rel_type=Relationship.FRIENDS, status=Relationship.PENDING ).save() # email the user body_html = render_template( 'mail/relationship/added_friend.html', from_user=logged_user, to_user=to_user, ) body_text = render_template( 'mail/relationship/added_friend.txt', from_user=logged_user, to_user=to_user, ) email(to_user.email, ("%s has requested to be friends") % logged_user.first_name, body_html, body_text) if ref: return redirect(ref) else: return redirect(url_for('user_app.profile', username=to_user.username)) else: abort(404)
def add_friend(to_username): ref = request.referrer logged_user = User.objects.filter(username=session.get('username')).first() to_user = User.objects.filter(username=to_username).first() if to_user: rel = Relationship.get_relationship(logged_user, to_user) to_username = to_user.username if rel == "REVERSE_FRIENDS_PENDING": Relationship( from_user=logged_user, to_user=to_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED ).save() reverse_rel = Relationship.objects.get( from_user=to_user, to_user=logged_user) reverse_rel.status = Relationship.APPROVED reverse_rel.save() elif rel == None and rel != "REVERSE_BLOCKED": Relationship( from_user=logged_user, to_user=to_user, rel_type=Relationship.FRIENDS, status=Relationship.PENDING ).save() # email the user body_html = render_template( 'mail/relationship/added_friend.html', from_user=logged_user, to_user=to_user, ) body_text = render_template( 'mail/relationship/added_friend.txt', from_user=logged_user, to_user=to_user, ) email(to_user.email, ("%s has requested to be friends") % logged_user.first_name, body_html, body_text) if ref: return redirect(ref) else: return redirect(url_for('user_app.profile', username=to_user.username)) else: abort(404)
def remove_friend(to_username): ref = request.referrer logged_user = User.getByName(session.get('username')) to_user = User.getByName(to_username) if to_user: rel_status = Relationship.get_relationship_status(logged_user.id, to_user.id) if rel_status == "FRIENDS_PENDING" \ or rel_status == "FRIENDS_APPROVED" \ or rel_status == "REVERSE_FRIENDS_PENDING": rel = Relationship.get_relationship(logged_user.id, to_user.id) rel.delete_record() reverse_rel = Relationship.get_relationship(to_user.id, logged_user.id) if reverse_rel: reverse_rel.delete_record() if ref: return redirect(ref) else: return redirect(url_for('user_blueprint.profile', username=to_user.username)) else: abort(404)
def remove_friend(to_username): logged_user = User.objects.filter(username=session.get('username')).first() to_user = User.objects.filter(username=to_username).first() if to_user: rel = Relationship.get_relationship(logged_user, to_user) to_username = to_user.username if rel == "FRIENDS_PENDING" or rel == "FRIENDS_APPROVED" or rel == "REVERSE_FRIENDS_PENDING": rel = Relationship.objects.filter(from_user=logged_user, to_user=to_user).delete() reverse_rel = Relationship.objects.filter( from_user=to_user, to_user=logged_user).delete() return redirect(url_for('user_app.profile', username=to_username)) else: abort(404)
def profile(username, friends_page_number=1): # Removed everything with edit_profile variable because it isn't needed logged_user = None rel = None friends_page = False user = User.objects.filter(username=username).first() profile_messages = [] if user: if session.get("username"): logged_user = User.objects.filter(username=session.get("username")).first() rel=Relationship.get_relationship(logged_user, user) # Get friends friends = Relationship.objects.filter( from_user=user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED ) friends_total = friends.count() if "friends" in request.url: friends_page = True friends = friends.paginate(page=friends_page_number, per_page=3) # paginate is a mongoengine helper else: friends = friends[:5] form = FeedPostForm() # Get user messages if friends or self if logged_user and (rel == "SAME" or rel == "FRIENDS_APPROVED"): profile_messages = Message.objects.filter( Q(from_user=user) | Q(to_user=user), message_type=POST ).order_by("-create_date")[:10] return render_template("user/profile.html", user=user, rel=rel, logged_user=logged_user, friends=friends, friends_total=friends_total, friends_page=friends_page, form=form, profile_messages=profile_messages, ) else: # Don't find the user abort(404)
def profile(username, friends_page_number=1): logged_user = None rel = None friends_page = False user = User.objects.filter(username=username).first() profile_messages = [] if user: if session.get('username'): logged_user = User.objects.filter(username=session.get('username')).first() rel = Relationship.get_relationship(logged_user, user) # get friends friends = Relationship.objects.filter( from_user=user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED ) friends_total = friends.count() if 'friends' in request.url: friends_page = True friends = friends.paginate(page=friends_page_number, per_page=3) else: friends = friends[:5] form = FeedPostForm() # get user messages if friends or self if logged_user and (rel == "SAME" or rel == "FRIENDS_APPROVED"): profile_messages = Message.objects.filter( Q(from_user=user) | Q(to_user=user), message_type=POST ).order_by('-create_date')[:10] return render_template('user/profile.html', user=user, logged_user=logged_user, rel=rel, friends=friends, friends_total=friends_total, friends_page=friends_page, form=form, profile_messages=profile_messages ) else: abort(404)
def profile(username, friends_page_number=1): logged_user = None edit_profile = False rel = None friends_page = False user = User.objects.filter(username=username).first() if user: if session.get('username'): logged_user = User.objects.filter( username=session.get('username')).first() rel = Relationship.get_relationship(logged_user, user) if session.get('username') and user.username == session.get( 'username'): edit_profile = True # get friends friends = Relationship.objects.filter(from_user=user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) friends_total = friends.count() if 'friends' in request.url: friends_page = True friends = friends.paginate(page=friends_page_number, per_page=3) else: friends = friends[:5] form = FeedPostForm() # get user messages profile_messages = Message.objects.filter( Q(from_user=user) | Q(to_user=user)).order_by('-create_date')[:10] return render_template('user/profile.html', user=user, logged_user=logged_user, rel=rel, edit_profile=edit_profile, friends=friends, friends_total=friends_total, friends_page=friends_page, form=form, profile_messages=profile_messages, images=None) else: abort(404)
def add_friend(to_username): ref = request.referrer logged_user = User.objects.filter(username=session.get("username")).first() to_user = User.objects.filter(username=to_username).first() if to_user: rel = Relationship.get_relationship(logged_user, to_user) to_username = to_user.username if rel == "REVERSE_FRIENDS_PENDING": # Person has requested you a friendship and waiting on you Relationship(from_user=logged_user, to_user=to_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED).save() reverse_rel = Relationship.objects.get(from_user=to_user, to_user=logged_user) reverse_rel.status = Relationship.APPROVED reverse_rel.save() elif rel == None and rel != "REVERSE_BLOCKED": Relationship(from_user=logged_user, to_user=to_user, rel_type=Relationship.FRIENDS, status=Relationship.PENDING).save() # Email the user body_html = render_template( "mail/relationship/added_friend.html", from_user=logged_user, to_user=to_user, ) body_text = render_template( "mail/relationship/added_friend.txt", from_user=logged_user, to_user=to_user, ) email( to_user.email, "{0} has requested to be friends".format( logged_user.first_name), body_html, body_text) if ref: return redirect(ref) else: return redirect( url_for("user_app.profile", username=to_user.username)) else: abort(404)
def profile(username, friends_page_number=1): logged_user = None rel = None friends_page = None user = User.objects.filter(username=username).first() profile_messages = [] if user: if session.get('username'): logged_user = User.objects.filter( username=session.get('username')).first() rel = Relationship.get_relationship(logged_user, user) # Get friends friends = Relationship.objects.filter( from_user=user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) friends_total = friends.count() if 'friends' in request.url: friends_page = True friends = friends.paginate(page=page, per_page=3) else: friends = friends[:5] form = FeedPostForm() # Get user messages if friends or self if logged_user and (rel == "SAME" or rel == "FRIENDS_APPROVED"): profile_messages = Message.objects.filter( Q(from_user=user) | Q(to_user=user), message_type=POST).order_by('-create_date')[:10] return render_template('user/profile.html', user=user, logged_user=logged_user, rel=rel, friends=friends, friends_total=friends_total, friends_page=friends_page, form=form, profile_messages=profile_messages) else: abort(404)
def unblock(to_username): ref = request.referrer logged_user = User.objects.filter(username=session.get("username")).first() to_user = User.objects.filter(username=to_username).first() if to_user: rel = Relationship.get_relationship(logged_user, to_user) to_username = to_user.username if rel == "BLOCKED": rel = Relationship.objects.filter(from_user=logged_user, to_user=to_user).delete() if ref: return redirect(ref) else: return redirect( url_for("user_app.profile", username=to_user.username)) else: abort(404)
def unblock(to_username): ref = request.referrer logged_user = User.getByName(session.get('username')) to_user = User.getByName(to_username) if to_user: rel_status = Relationship.get_relationship_status(logged_user.id, to_user.id) if rel_status == "BLOCKED": rel = Relationship.get_relationship(logged_user.id, to_user.id) rel.delete_record() if ref: return redirect(ref) else: return redirect(url_for('user_blueprint.profile', username=to_user.username)) else: abort(404)
def process_message(message): ## get from user's friends and broadcast message ## to all his friends # get the from_user's friends from_user = message.from_user friends = Relationship.objects.filter(from_user=from_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) for friend in friends: # post on all of friends feeds the message # except if to_user is blocked rel = Relationship.get_relationship(friend.to_user, message.to_user) if rel != "BLOCKED": feed = Feed(user=friend.to_user, message=message).save() return True
def process_message(message): # get the from_user's friends from_user = message.from_user friends = Relationship.objects.filter( from_user=from_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED ) # get the from_user's friends for friend in friends: rel = Relationship.get_relationship(friend.to_user, message.to_user) if rel != "BLOCKED": feed = Feed( user=friend.to_user, message=message ).save() return True
def profile(username): logged_user = None edit_profile = False rel = None user = User.objects.filter(username=username).first() profile_messages = [] if user: if session.get('username'): # compare relationship between user being looked at and user that is logged in logged_user = User.objects.filter( username=session.get('username')).first() rel = Relationship.get_relationship(logged_user, user) if user.username == session.get( 'username'): #if user is looking at his own profile page edit_profile = True #get friends friends = Relationship.objects.filter(from_user=user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) friends_total = friends.count() if 'friends' in request.url: friends_page = True friends = friends.paginate(page=page, per_page=3) else: friends = friends[:5] form = FeedPostForm() return render_template('user/profile.html', user=user, logged_user=logged_user, rel=rel, edit_profile=edit_profile, friends=friends, friends_total=friends_total, form=form) else: abort(404)
def add_friend(username): logged_user, to_user = _get_requesting_user_and_logged_in_user_obj( username) if to_user: relationship = Relationship.get_relationship(logged_user, to_user) if relationship == constants.REVERSE_PENDING_FRIEND_REQUEST: _save_approved_relationship_to_database(logged_user, to_user) elif relationship is None and relationship != constants.REVERSED_BLOCK: Relationship(from_user=logged_user, to_user=to_user, relationship_type=Relationship.FRIENDS, status=Relationship.PENDING).save() _email_user_about_friend_request(logged_user, to_user) return _redirect_to_referred_url_if_safe(username) abort(404)
def unblock(to_username): ref = request.referrer logged_user = User.objects.filter(username=session.get('username')).first() to_user = User.objects.filter(username=to_username).first() if to_user: rel = Relationship.get_relationship(logged_user, to_user) to_username = to_user.username if rel == "BLOCKED": rel = Relationship.objects.filter( from_user=logged_user, to_user=to_user).delete() if ref: return redirect(ref) else: return redirect(url_for('user_app.profile', username=to_user.username)) else: abort(404)
def profile(username, page=1): logged_user = None edit_profile = False rel = None friends_page = False user = User.objects.filter(username=username).first() if user: if session.get('username'): logged_user = User.objects.filter( username=session.get('username')).first() rel = Relationship.get_relationship(logged_user, user) if session.get('username') and user.username == session.get( 'username'): edit_profile = True # get friends friends = Relationship.objects.filter(from_user=user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) friends_total = friends.count() if 'friends' in request.url: friends_page = True friends = friends.paginate(page=page, per_page=3) else: friends = friends[:5] return render_template( 'user/profile.html', user=user, logged_user=logged_user, rel=rel, edit_profile=edit_profile, friends=friends, friends_total=friends_total, friends_page=friends_page, ) else: abort(404)
def test_friends_operations(self): """ Registeration: User 1 & User 2 """ user_1 = self.getUser1() self.app.post("/register", data=user_1, follow_redirects=True) user_1 = User.getByName(username=user_1['username']) code = user_1.change_configuration.get('confirmation_code') rv = self.app.get('/confirm/' + user_1.username + '/' + code) assert "Your email has been confirmed" in str(rv.data) user_2 = self.getUser2() self.app.post("/register", data=user_2, follow_redirects=True) user_2 = User.getByName(username=user_2['username']) code = user_2.change_configuration.get('confirmation_code') rv = self.app.get('/confirm/' + user_2.username + '/' + code) assert "Your email has been confirmed" in str(rv.data) """ Login: User 1 """ self.app.post('/login', data=dict( username=user_1.username, password=self.getUser1()['password'] )) with self.app as c: c.get('/') assert session.get('username') == user_1.username """ Add User 2 as Friend """ self.app.get('/add_friend/' + user_2.username) """ Login as User 2 """ self.app.post('/login', data=dict( username=user_2.username, password=self.getUser2()['password'] )) with self.app as c: c.get('/') assert session.get('username') == user_2.username """ Check the status of relationship between User 1, User 2. """ status = Relationship.get_relationship_status(user_1.id, user_2.id) assert status == "FRIENDS_PENDING" """ Accept Friendship from User 1. """ self.app.get('/add_friend/' + user_1.username) """ Check that the relationship between User 1 and User 2 has been APPROVED """ status = Relationship.get_relationship_status(user_1.id, user_2.id) assert status == "FRIENDS_APPROVED" """ User 2 Unfriend User 1 """ self.app.get('/remove_friend/' + user_1.username) assert Relationship.get_relationship(user_2.id, user_1.id) is None """ Check that no relationship exist """ assert Database.find('relationships', {}).retrieved == 0