Exemple #1
0
def test_insert_friends_by_user_relationship(app):
    user1 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password="******")
    insert_user(user1)

    user2 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password="******")
    insert_user(user2)

    user3 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password="******")
    insert_user(user3)

    user1.friends.append(Friend(friend=user2))
    user1.friends.append(Friend(friend=user3))

    friends_user1 = get_friends_by_user_id(user1.id)

    assert len(friends_user1) == 2
    assert friends_user1[0].friend_id == user2.id
    assert friends_user1[1].friend_id == user3.id
Exemple #2
0
def test_get_friends_by_user_id(app):
    user1 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password="******")
    insert_user(user1)

    user2 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password="******")
    insert_user(user2)

    user3 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password="******")
    insert_user(user3)

    friend1 = Friend(user=user1, friend=user2)
    insert_friend(friend1)

    friend2 = Friend(user=user1, friend=user3)
    insert_friend(friend2)

    friends_user1 = get_friends_by_user_id(user1.id)

    assert len(friends_user1) == 2
    assert friends_user1[0] == friend1
    assert friends_user1[1] == friend2
Exemple #3
0
def insert_friend(owner_user_id, other_user_id):
    friendship = Friend(user_id=owner_user_id, friend_user_id=other_user_id)

    additional_friendship = Friend(user_id=other_user_id,
                                   friend_user_id=owner_user_id)

    session.add(friendship)
    session.add(additional_friendship)
    session.commit()
Exemple #4
0
 def post(self):
     logger.log_python_api_post(FriendsResource.api_url)
     friend_id = request.json['friend_id']
     query = Friend.select().where(Friend.user == current_user.id,
                                   Friend.friend == friend_id)
     if not query.exists():
         data = {'user': current_user.id, 'friend': friend_id}
         Friend.create(**data)
     return friend_id, 201
Exemple #5
0
def logout():
    """Logout view"""
    if current_user.is_authenticated:
        user_id = current_user.id
        logout_user()
        if request.args.get('clear') == 'true':
            Friend.delete().where(Friend.user == user_id).execute()
            Friend.delete().where(Friend.friend == user_id).execute()
            Position.delete().where(Position.profile == user_id).execute()
            Profile.delete().where(Profile.id == user_id).execute()
            FlaskStorage.user.delete().where(
                FlaskStorage.user.user == user_id).execute()
            User.delete().where(User.id == user_id).execute()
    return redirect('/')
def test_get_only_friends_of_user(test_client, api_headers_bearer,
                                  insert_tokens):
    password = "******"

    user1 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password=password)
    insert_user(user1)
    user1_tokens = insert_tokens(user1.email)

    user2 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password=password)
    insert_user(user2)
    user2_tokens = insert_tokens(user2.email)

    user3 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password=password)
    insert_user(user3)

    user1.friends.append(Friend(friend=user2))
    user1.friends.append(Friend(friend=user3))

    user2.friends.append(Friend(friend=user1))

    response = test_client.get("/friends",
                               headers=api_headers_bearer(
                                   user1_tokens["access_token"]["token"]))
    json_response = json.loads(response.get_data(as_text=True))

    returned_friends = json_response["friends"]

    assert len(returned_friends) == 2
    assert returned_friends[0]["user_id"] == user2.id
    assert returned_friends[1]["user_id"] == user3.id

    response = test_client.get("/friends",
                               headers=api_headers_bearer(
                                   user2_tokens["access_token"]["token"]))
    json_response = json.loads(response.get_data(as_text=True))

    returned_friends = json_response["friends"]

    assert len(returned_friends) == 1
    assert returned_friends[0]["user_id"] == user1.id
Exemple #7
0
def update_friends(user_id_list):
    for user_id in user_id_list:
        user = get_user_by_id(user_id)
        for friend_id in user_id_list:
            if user.id == friend_id:
                continue
            if not is_friend_with_user(user.id, friend_id):
                user.friends.append(Friend(friend=get_user_by_id(friend_id)))
Exemple #8
0
    def get(self):
        logger.log_python_api_get(SuggestedProfilesResource.api_url)
        current_user_profile = Profile.get(id=current_user.id)
        skills_list = safe_split_strip_remove_empty(
            current_user_profile.skills)
        location_part_list = safe_split_strip_remove_empty(
            current_user_profile.location)
        position_title_list = [p.title for p in current_user_profile.positions]

        clauses = [Profile.id != current_user.id]

        or_clauses = []
        for skill in skills_list:
            or_clauses.append(Profile.skills.contains(skill))
        for location_part in location_part_list:
            or_clauses.append(Profile.location.contains(location_part))
        if any(position_title_list):
            subquery = Position.select(Param('1')).where(
                Position.profile == Profile.id,
                Position.title << position_title_list)
            or_clauses.append(Clause(SQL('EXISTS'), subquery))
        if any(or_clauses):
            clauses.append(reduce(operator.or_, or_clauses))

        friends = Friend.select(
            Friend.friend).where(Friend.user == current_user.id).execute()
        clauses.append(~(Profile.id << [f.friend.id for f in friends]))

        profiles = Profile.select().where(reduce(
            operator.and_, clauses)).order_by(fn.Rand()).limit(100)
        for profile in profiles:
            profile.score = 0
            for skill in skills_list:
                if profile.skills and skill in profile.skills:
                    profile.score += 10
            for part in location_part_list:
                if profile.location and part in profile.location:
                    profile.score += 10
            if any(position_title_list):
                profile.position_fetch = profile.positions.execute()
                for position_title in position_title_list:
                    if any(position.title == position_title
                           for position in profile.position_fetch):
                        profile.score += 10

        suggested_profiles = sorted(profiles,
                                    key=lambda profile: -profile.score)[:2]

        return list(map(lambda p: self.profile_to_dict(p), suggested_profiles))
Exemple #9
0
def test_is_friends_with_user_return_false_if_not_friend_with_user(app):
    user1 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password="******")
    insert_user(user1)

    user2 = User(first_name="Max",
                 last_name="Muster",
                 email="*****@*****.**",
                 password="******")
    insert_user(user2)

    insert_friend(Friend(user=user1, friend=user2))

    result = is_friend_with_user(user2.id, user1.id)

    assert result is False
Exemple #10
0
 def get(self):
     logger.log_python_api_get(FriendsResource.api_url)
     friends = Friend.select(
         Friend.friend).where(Friend.user == current_user.id)
     return list(map(lambda f: f.friend.id, friends))