Coverage for app/backend/src/couchers/models/public_trips.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-10 09:48 +0000

1import enum 

2from datetime import date, datetime 

3from typing import TYPE_CHECKING 

4 

5from sqlalchemy import ( 

6 BigInteger, 

7 Boolean, 

8 CheckConstraint, 

9 Date, 

10 DateTime, 

11 Enum, 

12 ForeignKey, 

13 Index, 

14 String, 

15 func, 

16 select, 

17 text, 

18) 

19from sqlalchemy.orm import Mapped, column_property, mapped_column, relationship 

20from sqlalchemy.sql import expression 

21 

22from couchers.models.base import Base 

23from couchers.models.clusters import Node 

24from couchers.models.moderation import ModerationObjectType 

25from couchers.utils import date_in_timezone 

26 

27if TYPE_CHECKING: 

28 from couchers.models import User 

29 from couchers.models.host_requests import HostRequest 

30 from couchers.models.moderation import ModerationState 

31 

32 

33class PublicTripStatus(enum.Enum): 

34 searching_for_host = enum.auto() 

35 closed = enum.auto() 

36 

37 

38class PublicTrip(Base, kw_only=True): 

39 """ 

40 A public trip posted by a traveler looking for a host in a community. 

41 """ 

42 

43 __tablename__ = "public_trips" 

44 __moderation_author_column__ = "user_id" 

45 __moderation_object_type__ = ModerationObjectType.public_trip 

46 __moderation_has_own_visibility_mechanism__ = False 

47 

48 id: Mapped[int] = mapped_column(BigInteger, primary_key=True, init=False) 

49 

50 # The traveler posting the trip 

51 user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) 

52 

53 # The community/location (city-level node) 

54 node_id: Mapped[int] = mapped_column(ForeignKey("nodes.id"), index=True) 

55 

56 # Timezone at the location of the community. 

57 # Not deferred because we always need it to interpret the dates. 

58 timezone: Mapped[str] = column_property( 

59 func.coalesce(select(Node.timezone).where(Node.id == node_id).scalar_subquery(), "Etc/UTC") 

60 ) 

61 

62 # Trip dates, local to the community's timezone 

63 from_date: Mapped[date] = mapped_column(Date) 

64 to_date: Mapped[date] = mapped_column(Date) 

65 

66 # timezone-aware end time of the trip, can be compared to now() 

67 end_time = column_property(date_in_timezone(to_date, timezone) + text("interval '1 days'")) 

68 

69 # User's message about their trip 

70 description: Mapped[str] = mapped_column(String) 

71 

72 # Current status 

73 status: Mapped[PublicTripStatus] = mapped_column( 

74 Enum(PublicTripStatus), default=PublicTripStatus.searching_for_host 

75 ) 

76 

77 # If true, only users with the same gender as the poster can see this trip 

78 same_gender_only: Mapped[bool] = mapped_column(Boolean, default=False, server_default=expression.false()) 

79 

80 moderation_state_id: Mapped[int] = mapped_column(ForeignKey("moderation_states.id"), index=True) 

81 

82 # Timestamps 

83 created: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), init=False) 

84 

85 # Relationships 

86 user: Mapped[User] = relationship(init=False, back_populates="public_trips") 

87 node: Mapped[Node] = relationship(init=False, back_populates="public_trips") 

88 host_requests: Mapped[list[HostRequest]] = relationship(init=False, back_populates="public_trip") 

89 moderation_state: Mapped[ModerationState] = relationship(init=False) 

90 

91 __table_args__ = ( 

92 CheckConstraint("from_date < to_date", name="valid_date_range"), 

93 # Index for querying active trips in a community 

94 # Using partial index since we mostly query for active trips 

95 Index( 

96 "ix_public_trips_node_from_date_active", 

97 node_id, 

98 from_date, 

99 postgresql_where=status == PublicTripStatus.searching_for_host, 

100 ), 

101 )