Esempio n. 1
0
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)
Esempio n. 2
0
def profile(username, friends_page_number=1):
    logged_user = None
    rel = None
    friends_page = False
    friends_per_page = 3
    profile_messages = []
    user = User.getByName(username)

    if user:
        if session['username']:
            logged_user = User.getByName(session['username'])
            rel = Relationship.get_relationship_status(logged_user.id, user.id)

        # get user friends
        friends_list = Relationship.get_friends(
            user=logged_user.id,
            rel_type=RELATIONSHIP_TYPE.get(FRIENDS),
            status=STATUS_TYPE.get(APPROVED)
        )

        friends_total = len(friends_list)

        if 'friends' in request.url:
            friends_page = True

            # pagination
            limit = friends_per_page * friends_page_number
            offset = limit - friends_per_page
            if friends_total >= limit:
                friends = friends_list[offset:limit]
            else:
                friends = friends_list[offset:friends_total]
        else:
            if friends_total >= 5:
                friends = friends_list[:5]
            else:
                friends = friends_list

        form = FeedPostForm()

        if logged_user and (rel == "SAME" or rel == "FRIENDS_APPROVED"):
            profile_messages = Message.getMessages(logged_user.id)

        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)
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
def process_message(message):

    # get the from_user friends
    from_user = User.getById(message.from_user)
    friends = Relationship.get_friends(user=from_user.id,
                                       rel_type=RELATIONSHIP_TYPE.get(FRIENDS),
                                       status=STATUS_TYPE.get(APPROVED))
    """
        Fan out pattern: means that each user have a list of posts of users that are friends with him (friends_approved).
        So each user have it's feed.
        NB: Fan out pattern is Scalable.
    """

    # get the from_user's friends
    for friend in friends:
        rel_status = Relationship.get_relationship_status(
            friend.to_user, message.to_user)
        if rel_status != "BLOCKED":
            Feed(
                user=friend.to_user,
                message=message.id,
            ).save_database()

    return True
Esempio n. 6
0
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)
Esempio n. 7
0
    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
Esempio n. 8
0
    def test_block_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

        """
            User 1 Block User 2
        """

        self.app.get('/block/' + user_2.username)

        """
            Check the relationship status between User 1 and User 2
        """

        status = Relationship.get_relationship_status(user_1.id, user_2.id)
        assert status == "BLOCKED"

        """
            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 relationship status between User 2 and User 1
        """

        status = Relationship.get_relationship_status(user_2.id, user_1.id)
        assert status == "REVERSE_BLOCKED"

        """
            Login as 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

        """
            User 1 Unblocks User 2
        """
        self.app.get('/unblock/' + user_2.username)

        """
            Check the relationship status between User 2 and User 1
        """

        status = Relationship.get_relationship_status(user_1.id, user_2.id)
        assert status is None  # No Relation between user 1 and user 2