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