Coverage for app/backend/src/couchers/servicers/screens.py: 100%
26 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-25 01:09 +0000
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-25 01:09 +0000
1from google.protobuf import empty_pb2
2from sqlalchemy.orm import Session
4from couchers.context import CouchersContext
5from couchers.proto import (
6 communities_pb2,
7 discussions_pb2,
8 events_pb2,
9 messages_pb2,
10 public_trips_pb2,
11 requests_pb2,
12 screens_pb2,
13 screens_pb2_grpc,
14)
15from couchers.servicers.account import Account
16from couchers.servicers.communities import Communities
17from couchers.servicers.discussions import Discussions
18from couchers.servicers.events import Events
19from couchers.servicers.public_trips import PublicTrips
20from couchers.servicers.requests import Requests
23class Screens(screens_pb2_grpc.ScreensServicer):
24 def GetDashboard(
25 self, request: screens_pb2.GetDashboardReq, context: CouchersContext, session: Session
26 ) -> screens_pb2.GetDashboardRes:
27 # How many items each dashboard section returns. We want a few screens' worth rather than a single page.
28 stays_page_size = 10
29 my_events_page_size = 9
30 community_events_page_size = 9
31 discussions_page_size = 9
32 public_trips_page_size = 10
33 communities_page_size = 25
35 def upcoming_host_requests_req(*, surfing: bool) -> requests_pb2.ListHostRequestsReq:
36 return requests_pb2.ListHostRequestsReq(
37 only_sent=surfing,
38 only_received=not surfing,
39 only_active=True,
40 status_in=[
41 messages_pb2.HOST_REQUEST_STATUS_ACCEPTED,
42 messages_pb2.HOST_REQUEST_STATUS_CONFIRMED,
43 ],
44 sort_by=requests_pb2.HOST_REQUEST_SORT_BY_FROM_DATE,
45 number=stays_page_size,
46 )
48 my_public_trips = None
49 community_public_trips = None
50 if context.get_boolean_value("public_trips_enabled", default=False):
51 my_public_trips = PublicTrips().ListPublicTripsByUser(
52 public_trips_pb2.ListPublicTripsByUserReq(
53 user_id=context.user_id,
54 page_size=public_trips_page_size,
55 statuses_in=[public_trips_pb2.PUBLIC_TRIP_STATUS_SEARCHING_FOR_HOST],
56 ascending=True,
57 ),
58 context,
59 session,
60 )
61 community_public_trips = PublicTrips().ListMyCommunitiesPublicTrips(
62 public_trips_pb2.ListMyCommunitiesPublicTripsReq(page_size=public_trips_page_size),
63 context,
64 session,
65 )
67 return screens_pb2.GetDashboardRes(
68 reminders=Account().GetReminders(empty_pb2.Empty(), context, session),
69 surfing=Requests().ListHostRequests(upcoming_host_requests_req(surfing=True), context, session),
70 hosting=Requests().ListHostRequests(upcoming_host_requests_req(surfing=False), context, session),
71 my_events=Events().ListMyEvents(
72 events_pb2.ListMyEventsReq(page_size=my_events_page_size, attending=True, organizing=True),
73 context,
74 session,
75 ),
76 community_events=Events().ListMyEvents(
77 events_pb2.ListMyEventsReq(
78 page_size=community_events_page_size,
79 my_communities=True,
80 my_communities_exclude_global=True,
81 ),
82 context,
83 session,
84 ),
85 discussions=Discussions().ListMyCommunitiesDiscussions(
86 discussions_pb2.ListMyCommunitiesDiscussionsReq(page_size=discussions_page_size),
87 context,
88 session,
89 ),
90 my_public_trips=my_public_trips,
91 community_public_trips=community_public_trips,
92 my_communities=Communities().ListUserCommunities(
93 communities_pb2.ListUserCommunitiesReq(page_size=communities_page_size), context, session
94 ),
95 )