コード例 #1
0
ファイル: api.py プロジェクト: telalpal/couchers
    def ListFriendRequests(self, request, context):
        # both sent and received
        with session_scope() as session:
            sent_requests = (
                session.query(FriendRelationship)
                .filter(FriendRelationship.from_user_id == context.user_id)
                .filter(FriendRelationship.status == FriendStatus.pending)
                .all()
            )

            received_requests = (
                session.query(FriendRelationship)
                .filter(FriendRelationship.to_user_id == context.user_id)
                .filter(FriendRelationship.status == FriendStatus.pending)
                .all()
            )

            return api_pb2.ListFriendRequestsRes(
                sent=[
                    api_pb2.FriendRequest(
                        friend_request_id=friend_request.id,
                        state=api_pb2.FriendRequest.FriendRequestStatus.PENDING,
                        user_id=friend_request.to_user.id,
                    )
                    for friend_request in sent_requests
                ],
                received=[
                    api_pb2.FriendRequest(
                        friend_request_id=friend_request.id,
                        state=api_pb2.FriendRequest.FriendRequestStatus.PENDING,
                        user_id=friend_request.from_user.id,
                    )
                    for friend_request in received_requests
                ],
            )
コード例 #2
0
def user_model_to_pb(db_user, session, context):
    num_references = session.query(Reference.from_user_id).filter(Reference.to_user_id == db_user.id).count()

    # returns (lat, lng)
    # we put people without coords on null island
    # https://en.wikipedia.org/wiki/Null_Island
    lat, lng = db_user.coordinates or (0, 0)

    pending_friend_request = None
    if db_user.id == context.user_id:
        friends_status = api_pb2.User.FriendshipStatus.NA
    else:
        friend_relationship = (
            session.query(FriendRelationship)
            .filter(
                or_(
                    and_(
                        FriendRelationship.from_user_id == context.user_id, FriendRelationship.to_user_id == db_user.id
                    ),
                    and_(
                        FriendRelationship.from_user_id == db_user.id, FriendRelationship.to_user_id == context.user_id
                    ),
                )
            )
            .filter(
                or_(
                    FriendRelationship.status == FriendStatus.accepted,
                    FriendRelationship.status == FriendStatus.pending,
                )
            )
            .one_or_none()
        )

        if friend_relationship:
            if friend_relationship.status == FriendStatus.accepted:
                friends_status = api_pb2.User.FriendshipStatus.FRIENDS
            else:
                friends_status = api_pb2.User.FriendshipStatus.PENDING
                if friend_relationship.from_user_id == context.user_id:
                    # we sent it
                    pending_friend_request = api_pb2.FriendRequest(
                        friend_request_id=friend_relationship.id,
                        state=api_pb2.FriendRequest.FriendRequestStatus.PENDING,
                        user_id=friend_relationship.to_user.id,
                        sent=True,
                    )
                else:
                    # we received it
                    pending_friend_request = api_pb2.FriendRequest(
                        friend_request_id=friend_relationship.id,
                        state=api_pb2.FriendRequest.FriendRequestStatus.PENDING,
                        user_id=friend_relationship.from_user.id,
                        sent=False,
                    )
        else:
            friends_status = api_pb2.User.FriendshipStatus.NOT_FRIENDS

    user = api_pb2.User(
        user_id=db_user.id,
        username=db_user.username,
        name=db_user.name,
        city=db_user.city,
        hometown=db_user.hometown,
        lat=lat,
        lng=lng,
        radius=db_user.geom_radius,
        verification=db_user.verification,
        community_standing=db_user.community_standing,
        num_references=num_references,
        gender=db_user.gender,
        pronouns=db_user.pronouns,
        age=db_user.age,
        joined=Timestamp_from_datetime(db_user.display_joined),
        last_active=Timestamp_from_datetime(db_user.display_last_active),
        hosting_status=hostingstatus2api[db_user.hosting_status],
        meetup_status=meetupstatus2api[db_user.meetup_status],
        occupation=db_user.occupation,
        education=db_user.education,
        about_me=db_user.about_me,
        my_travels=db_user.my_travels,
        things_i_like=db_user.things_i_like,
        about_place=db_user.about_place,
        languages=db_user.languages.split("|") if db_user.languages else [],
        countries_visited=db_user.countries_visited.split("|") if db_user.countries_visited else [],
        countries_lived=db_user.countries_lived.split("|") if db_user.countries_lived else [],
        additional_information=db_user.additional_information,
        friends=friends_status,
        pending_friend_request=pending_friend_request,
        mutual_friends=[
            api_pb2.MutualFriend(user_id=mutual_friend.id, username=mutual_friend.username, name=mutual_friend.name)
            for mutual_friend in db_user.mutual_friends(context.user_id)
        ],
        smoking_allowed=smokinglocation2api[db_user.smoking_allowed],
        sleeping_arrangement=sleepingarrangement2api[db_user.sleeping_arrangement],
        parking_details=parkingdetails2api[db_user.parking_details],
        avatar_url=db_user.avatar.full_url if db_user.avatar else None,
    )

    if db_user.max_guests is not None:
        user.max_guests.value = db_user.max_guests

    if db_user.last_minute is not None:
        user.last_minute.value = db_user.last_minute

    if db_user.has_pets is not None:
        user.has_pets.value = db_user.has_pets

    if db_user.accepts_pets is not None:
        user.accepts_pets.value = db_user.accepts_pets

    if db_user.pet_details is not None:
        user.pet_details.value = db_user.pet_details

    if db_user.has_kids is not None:
        user.has_kids.value = db_user.has_kids

    if db_user.accepts_kids is not None:
        user.accepts_kids.value = db_user.accepts_kids

    if db_user.kid_details is not None:
        user.kid_details.value = db_user.kid_details

    if db_user.has_housemates is not None:
        user.has_housemates.value = db_user.has_housemates

    if db_user.housemate_details is not None:
        user.housemate_details.value = db_user.housemate_details

    if db_user.wheelchair_accessible is not None:
        user.wheelchair_accessible.value = db_user.wheelchair_accessible

    if db_user.smokes_at_home is not None:
        user.smokes_at_home.value = db_user.smokes_at_home

    if db_user.drinking_allowed is not None:
        user.drinking_allowed.value = db_user.drinking_allowed

    if db_user.drinks_at_home is not None:
        user.drinks_at_home.value = db_user.drinks_at_home

    if db_user.other_host_info is not None:
        user.other_host_info.value = db_user.other_host_info

    if db_user.sleeping_details is not None:
        user.sleeping_details.value = db_user.sleeping_details

    if db_user.area is not None:
        user.area.value = db_user.area

    if db_user.house_rules is not None:
        user.house_rules.value = db_user.house_rules

    if db_user.parking is not None:
        user.parking.value = db_user.parking

    if db_user.camping_ok is not None:
        user.camping_ok.value = db_user.camping_ok

    return user