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

230 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-19 14:14 +0000

1import enum 

2from datetime import date, datetime, timedelta 

3from typing import TYPE_CHECKING, Any 

4 

5from geoalchemy2 import Geometry 

6from sqlalchemy import ( 

7 ARRAY, 

8 BigInteger, 

9 Boolean, 

10 CheckConstraint, 

11 Date, 

12 DateTime, 

13 Enum, 

14 Float, 

15 ForeignKey, 

16 Index, 

17 Integer, 

18 Interval, 

19 String, 

20 UniqueConstraint, 

21 and_, 

22 func, 

23 or_, 

24 text, 

25) 

26from sqlalchemy import LargeBinary as Binary 

27from sqlalchemy import select as sa_select 

28from sqlalchemy.ext.hybrid import hybrid_property 

29from sqlalchemy.orm import DynamicMapped, Mapped, column_property, mapped_column, relationship 

30from sqlalchemy.sql import expression 

31from sqlalchemy.sql.elements import ColumnElement 

32 

33from couchers.constants import ( 

34 EMAIL_REGEX, 

35 GUIDELINES_VERSION, 

36 PHONE_VERIFICATION_LIFETIME, 

37 SMS_CODE_LIFETIME, 

38 TOS_VERSION, 

39) 

40from couchers.models.activeness_probe import ActivenessProbe 

41from couchers.models.base import Base, Geom 

42from couchers.models.mod_note import ModNote 

43from couchers.models.static import Language, Region, TimezoneArea 

44from couchers.utils import get_coordinates, last_active_coarsen, now 

45 

46if TYPE_CHECKING: 

47 from couchers.models import UserBadge 

48 from couchers.models.admin import UserAdminTag 

49 from couchers.models.public_trips import PublicTrip 

50 from couchers.models.rest import InviteCode, ModerationUserList 

51 from couchers.models.uploads import PhotoGallery 

52 

53 

54class HostingStatus(enum.Enum): 

55 can_host = enum.auto() 

56 maybe = enum.auto() 

57 cant_host = enum.auto() 

58 

59 

60class MeetupStatus(enum.Enum): 

61 wants_to_meetup = enum.auto() 

62 open_to_meetup = enum.auto() 

63 does_not_want_to_meetup = enum.auto() 

64 

65 

66class SmokingLocation(enum.Enum): 

67 yes = enum.auto() 

68 window = enum.auto() 

69 outside = enum.auto() 

70 no = enum.auto() 

71 

72 

73class SleepingArrangement(enum.Enum): 

74 private = enum.auto() 

75 common = enum.auto() 

76 shared_room = enum.auto() 

77 

78 

79class ParkingDetails(enum.Enum): 

80 free_onsite = enum.auto() 

81 free_offsite = enum.auto() 

82 paid_onsite = enum.auto() 

83 paid_offsite = enum.auto() 

84 

85 

86class ProfilePublicVisibility(enum.Enum): 

87 # no public info 

88 nothing = enum.auto() 

89 # only show on map, randomized, unclickable 

90 map_only = enum.auto() 

91 # name, gender, location, hosting/meetup status, badges, number of references, and signup time 

92 limited = enum.auto() 

93 # full about me except additional info (hide my home) 

94 most = enum.auto() 

95 # all but references 

96 full = enum.auto() 

97 

98 

99class User(Base, kw_only=True): 

100 """ 

101 Basic user and profile details 

102 """ 

103 

104 __tablename__ = "users" 

105 

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

107 

108 username: Mapped[str] = mapped_column(String, unique=True) 

109 email: Mapped[str] = mapped_column(String, unique=True) 

110 # stored in libsodium hash format, can be null for email login 

111 hashed_password: Mapped[bytes] = mapped_column(Binary) 

112 # phone number in E.164 format with leading +, for example "+46701740605" 

113 phone: Mapped[str | None] = mapped_column(String, default=None, server_default=expression.null()) 

114 # language preference -- defaults to empty string 

115 ui_language_preference: Mapped[str | None] = mapped_column(String, default=None, server_default="") 

116 

117 # timezones should always be UTC 

118 ## location 

119 # point describing their location. EPSG4326 is the SRS (spatial ref system, = way to describe a point on earth) used 

120 # by GPS, it has the WGS84 geoid with lat/lon 

121 geom: Mapped[Geom] = mapped_column(Geometry(geometry_type="POINT", srid=4326)) 

122 # randomized coordinates within a radius of 0.02-0.1 degrees, equates to about 2-10 km 

123 randomized_geom: Mapped[Geom | None] = mapped_column(Geometry(geometry_type="POINT", srid=4326), default=None) 

124 # their display location (displayed to other users), in meters 

125 geom_radius: Mapped[float] = mapped_column(Float) 

126 # the display address (text) shown on their profile 

127 city: Mapped[str] = mapped_column(String) 

128 # "Grew up in" on profile 

129 hometown: Mapped[str | None] = mapped_column(String, default=None) 

130 

131 regions_visited: Mapped[list[Region]] = relationship( 

132 init=False, secondary="regions_visited", order_by="Region.name" 

133 ) 

134 regions_lived: Mapped[list[Region]] = relationship(init=False, secondary="regions_lived", order_by="Region.name") 

135 

136 timezone = column_property( 

137 sa_select(TimezoneArea.tzid).where(func.ST_Contains(TimezoneArea.geom, geom)).limit(1).scalar_subquery(), 

138 deferred=True, 

139 ) 

140 

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

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

143 profile_last_updated: Mapped[datetime] = mapped_column( 

144 DateTime(timezone=True), server_default=func.now(), init=False 

145 ) 

146 

147 public_visibility: Mapped[ProfilePublicVisibility] = mapped_column( 

148 Enum(ProfilePublicVisibility), server_default="map_only", init=False 

149 ) 

150 has_modified_public_visibility: Mapped[bool] = mapped_column(Boolean, server_default=expression.false(), init=False) 

151 

152 # id of the last message that they received a notification about 

153 last_notified_message_id: Mapped[int] = mapped_column(BigInteger, default=0) 

154 # same as above for host requests 

155 last_notified_request_message_id: Mapped[int] = mapped_column(BigInteger, server_default=text("0"), init=False) 

156 

157 # display name 

158 name: Mapped[str] = mapped_column(String) 

159 gender: Mapped[str] = mapped_column(String) 

160 pronouns: Mapped[str | None] = mapped_column(String, default=None) 

161 birthdate: Mapped[date] = mapped_column(Date) # in the timezone of birthplace 

162 

163 # Profile photo gallery for this user (photos about themselves) 

164 # The first photo in the gallery (by position) is used as the avatar 

165 profile_gallery_id: Mapped[int | None] = mapped_column(ForeignKey("photo_galleries.id"), default=None) 

166 

167 hosting_status: Mapped[HostingStatus] = mapped_column(Enum(HostingStatus)) 

168 meetup_status: Mapped[MeetupStatus] = mapped_column(Enum(MeetupStatus), server_default="open_to_meetup", init=False) 

169 

170 # community standing score 

171 community_standing: Mapped[float | None] = mapped_column(Float, default=None) 

172 

173 occupation: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

174 education: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

175 

176 # "Who I am" under "About Me" tab 

177 about_me: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

178 # "What I do in my free time" under "About Me" tab 

179 things_i_like: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

180 # "About my home" under "My Home" tab 

181 about_place: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

182 # "Additional information" under "About Me" tab 

183 additional_information: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

184 

185 banned_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) 

186 deleted_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) 

187 is_superuser: Mapped[bool] = mapped_column(Boolean, server_default=expression.false(), init=False) 

188 is_editor: Mapped[bool] = mapped_column(Boolean, server_default=expression.false(), init=False) 

189 

190 # the undelete token allows a user to recover their account for a couple of days after deletion in case it was 

191 # accidental or they changed their mind 

192 # constraints make sure these are non-null only if deleted_at is set and that these are null in unison 

193 undelete_token: Mapped[str | None] = mapped_column(String, default=None) 

194 # validity of the undelete token 

195 undelete_until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) 

196 

197 # hosting preferences 

198 max_guests: Mapped[int | None] = mapped_column(Integer, default=None) 

199 last_minute: Mapped[bool | None] = mapped_column(Boolean, default=None) 

200 has_pets: Mapped[bool | None] = mapped_column(Boolean, default=None) 

201 accepts_pets: Mapped[bool | None] = mapped_column(Boolean, default=None) 

202 pet_details: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

203 has_kids: Mapped[bool | None] = mapped_column(Boolean, default=None) 

204 accepts_kids: Mapped[bool | None] = mapped_column(Boolean, default=None) 

205 kid_details: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

206 has_housemates: Mapped[bool | None] = mapped_column(Boolean, default=None) 

207 housemate_details: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

208 wheelchair_accessible: Mapped[bool | None] = mapped_column(Boolean, default=None) 

209 smoking_allowed: Mapped[SmokingLocation | None] = mapped_column(Enum(SmokingLocation), default=None) 

210 smokes_at_home: Mapped[bool | None] = mapped_column(Boolean, default=None) 

211 drinking_allowed: Mapped[bool | None] = mapped_column(Boolean, default=None) 

212 drinks_at_home: Mapped[bool | None] = mapped_column(Boolean, default=None) 

213 # "Additional information" under "My Home" tab 

214 other_host_info: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

215 

216 # "Sleeping privacy" (not long-form text) 

217 sleeping_arrangement: Mapped[SleepingArrangement | None] = mapped_column(Enum(SleepingArrangement), default=None) 

218 # "Sleeping arrangement" under "My Home" tab 

219 sleeping_details: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

220 # "Local area information" under "My Home" tab 

221 area: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

222 # "House rules" under "My Home" tab 

223 house_rules: Mapped[str | None] = mapped_column(String, default=None) # CommonMark without images 

224 parking: Mapped[bool | None] = mapped_column(Boolean, default=None) 

225 parking_details: Mapped[ParkingDetails | None] = mapped_column( 

226 Enum(ParkingDetails), default=None 

227 ) # CommonMark without images 

228 camping_ok: Mapped[bool | None] = mapped_column(Boolean, default=None) 

229 

230 accepted_tos: Mapped[int] = mapped_column(Integer, default=0) 

231 accepted_community_guidelines: Mapped[int] = mapped_column(Integer, server_default="0", init=False) 

232 # whether the user has filled in the contributor form 

233 filled_contributor_form: Mapped[bool] = mapped_column(Boolean, server_default=expression.false(), init=False) 

234 

235 # number of onboarding emails sent 

236 onboarding_emails_sent: Mapped[int] = mapped_column(Integer, server_default="0", init=False) 

237 last_onboarding_email_sent: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) 

238 

239 # whether we need to sync the user's newsletter preferences with the newsletter server 

240 in_sync_with_newsletter: Mapped[bool] = mapped_column(Boolean, server_default=expression.false(), init=False) 

241 # opted out of the newsletter 

242 opt_out_of_newsletter: Mapped[bool] = mapped_column(Boolean, server_default=expression.false(), init=False) 

243 

244 # set to null to receive no digests 

245 digest_frequency: Mapped[timedelta | None] = mapped_column(Interval, default=None) 

246 last_digest_sent: Mapped[datetime] = mapped_column( 

247 DateTime(timezone=True), server_default=text("to_timestamp(0)"), init=False 

248 ) 

249 

250 # for changing their email 

251 new_email: Mapped[str | None] = mapped_column(String, default=None) 

252 

253 new_email_token: Mapped[str | None] = mapped_column(String, default=None) 

254 new_email_token_created: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) 

255 new_email_token_expiry: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) 

256 

257 recommendation_score: Mapped[float] = mapped_column(Float, server_default="0", init=False) 

258 

259 # Columns for verifying their phone number. State chart: 

260 # ,-------------------, 

261 # | Start | 

262 # | phone = None | someone else 

263 # ,-----------------, | token = None | verifies ,-----------------------, 

264 # | Code Expired | | sent = 1970 or zz | phone xx | Verification Expired | 

265 # | phone = xx | time passes | verified = None | <------, | phone = xx | 

266 # | token = yy | <------------, | attempts = 0 | | | token = None | 

267 # | sent = zz (exp.)| | '-------------------' | | sent = zz | 

268 # | verified = None | | V ^ +-----------< | verified = ww (exp.) | 

269 # | attempts = 0..2 | >--, | | | ChangePhone("") | | attempts = 0 | 

270 # '-----------------' +-------- | ------+----+--------------------+ '-----------------------' 

271 # | | | | ChangePhone(xx) | ^ time passes 

272 # | | ^ V | | 

273 # ,-----------------, | | ,-------------------, | ,-----------------------, 

274 # | Too Many | >--' '--< | Code sent | >------+ | Verified | 

275 # | phone = xx | | phone = xx | | | phone = xx | 

276 # | token = yy | VerifyPhone(wrong)| token = yy | '-----------< | token = None | 

277 # | sent = zz | <------+--------< | sent = zz | | sent = zz | 

278 # | verified = None | | | verified = None | VerifyPhone(correct) | verified = ww | 

279 # | attempts = 3 | '--------> | attempts = 0..2 | >------------------> | attempts = 0 | 

280 # '-----------------' '-------------------' '-----------------------' 

281 

282 # randomly generated Luhn 6-digit string 

283 phone_verification_token: Mapped[str | None] = mapped_column( 

284 String(6), default=None, server_default=expression.null(), init=False 

285 ) 

286 

287 phone_verification_sent: Mapped[datetime] = mapped_column( 

288 DateTime(timezone=True), server_default=text("to_timestamp(0)"), init=False 

289 ) 

290 phone_verification_verified: Mapped[datetime | None] = mapped_column( 

291 DateTime(timezone=True), default=None, server_default=expression.null(), init=False 

292 ) 

293 phone_verification_attempts: Mapped[int] = mapped_column(Integer, server_default=text("0"), init=False) 

294 

295 # the stripe customer identifier if the user has donated to Couchers 

296 # e.g. cus_JjoXHttuZopv0t 

297 # for new US entity 

298 stripe_customer_id: Mapped[str | None] = mapped_column(String, default=None) 

299 # for old AU entity 

300 stripe_customer_id_old: Mapped[str | None] = mapped_column(String, default=None) 

301 

302 has_passport_sex_gender_exception: Mapped[bool] = mapped_column( 

303 Boolean, server_default=expression.false(), init=False 

304 ) 

305 

306 # checking for phone verification 

307 last_donated: Mapped[datetime | None] = mapped_column( 

308 DateTime(timezone=True), default=None, server_default=expression.null() 

309 ) 

310 

311 # whether this user has all emails turned off 

312 do_not_email: Mapped[bool] = mapped_column(Boolean, server_default=expression.false(), init=False) 

313 

314 profile_gallery: Mapped[PhotoGallery | None] = relationship(init=False, foreign_keys="User.profile_gallery_id") 

315 

316 admin_note: Mapped[str] = mapped_column(String, server_default=text("''"), init=False) 

317 

318 # whether mods have marked this user has having to update their location 

319 needs_to_update_location: Mapped[bool] = mapped_column(Boolean, server_default=expression.false(), init=False) 

320 

321 last_antibot: Mapped[datetime] = mapped_column( 

322 DateTime(timezone=True), server_default=text("to_timestamp(0)"), init=False 

323 ) 

324 

325 age = column_property(func.date_part("year", func.age(birthdate))) 

326 

327 # ID of the invite code used to sign up (if any) 

328 invite_code_id: Mapped[str | None] = mapped_column(ForeignKey("invite_codes.id"), default=None) 

329 invite_code: Mapped[InviteCode | None] = relationship(init=False, foreign_keys=[invite_code_id]) 

330 

331 # Signup motivations - how they heard about us and what they want to do 

332 heard_about_couchers: Mapped[str | None] = mapped_column(String, default=None) 

333 signup_motivations: Mapped[list[str] | None] = mapped_column(ARRAY(String), default=None) 

334 

335 moderation_user_lists: Mapped[list[ModerationUserList]] = relationship( 

336 init=False, secondary="moderation_user_list_members", back_populates="users" 

337 ) 

338 language_abilities: Mapped[list[LanguageAbility]] = relationship(init=False, back_populates="user") 

339 galleries: Mapped[list[PhotoGallery]] = relationship( 

340 init=False, foreign_keys="PhotoGallery.owner_user_id", back_populates="owner_user" 

341 ) 

342 mod_notes: DynamicMapped[ModNote] = relationship( 

343 init=False, foreign_keys="ModNote.user_id", back_populates="user", lazy="dynamic" 

344 ) 

345 

346 badges: Mapped[list[UserBadge]] = relationship(init=False, back_populates="user") 

347 

348 admin_tags: Mapped[list[UserAdminTag]] = relationship( 

349 init=False, foreign_keys="UserAdminTag.user_id", overlaps="user" 

350 ) 

351 

352 pending_activeness_probe: Mapped[ActivenessProbe | None] = relationship( 

353 init=False, 

354 primaryjoin="and_(ActivenessProbe.user_id == User.id, ActivenessProbe.is_pending)", 

355 uselist=False, 

356 back_populates="user", 

357 ) 

358 

359 public_trips: Mapped[list[PublicTrip]] = relationship(init=False, back_populates="user") 

360 

361 __table_args__ = ( 

362 # Verified phone numbers should be unique 

363 Index( 

364 "ix_users_unique_phone", 

365 phone, 

366 unique=True, 

367 postgresql_where=phone_verification_verified != None, 

368 ), 

369 Index( 

370 "ix_users_active", 

371 id, 

372 postgresql_where=and_(banned_at.is_(None), deleted_at.is_(None)), 

373 ), 

374 Index( 

375 "ix_users_geom_active", 

376 geom, 

377 id, 

378 username, 

379 postgresql_using="gist", 

380 postgresql_where=and_(banned_at.is_(None), deleted_at.is_(None)), 

381 ), 

382 Index( 

383 "ix_users_by_id", 

384 id, 

385 postgresql_using="hash", 

386 postgresql_where=and_(banned_at.is_(None), deleted_at.is_(None)), 

387 ), 

388 Index( 

389 "ix_users_by_username", 

390 username, 

391 postgresql_using="hash", 

392 postgresql_where=and_(banned_at.is_(None), deleted_at.is_(None)), 

393 ), 

394 # There are two possible states for new_email_token, new_email_token_created, and new_email_token_expiry 

395 CheckConstraint( 

396 "(new_email_token IS NOT NULL AND new_email_token_created IS NOT NULL AND new_email_token_expiry IS NOT NULL) OR \ 

397 (new_email_token IS NULL AND new_email_token_created IS NULL AND new_email_token_expiry IS NULL)", 

398 name="check_new_email_token_state", 

399 ), 

400 # Whenever a phone number is set, it must either be pending verification or already verified. 

401 # Exactly one of the following must always be true: not phone, token, verified. 

402 CheckConstraint( 

403 "(phone IS NULL)::int + (phone_verification_verified IS NOT NULL)::int + (phone_verification_token IS NOT NULL)::int = 1", 

404 name="phone_verified_conditions", 

405 ), 

406 # Email must match our regex 

407 CheckConstraint( 

408 f"email ~ '{EMAIL_REGEX}'", 

409 name="valid_email", 

410 ), 

411 # Undelete token + time are coupled: either both null or neither; and if they're not null then the account is deleted 

412 CheckConstraint( 

413 "((undelete_token IS NULL) = (undelete_until IS NULL)) AND ((undelete_token IS NULL) OR deleted_at IS NOT NULL)", 

414 name="undelete_nullity", 

415 ), 

416 # If the user disabled all emails, then they can't host or meet up 

417 CheckConstraint( 

418 "(do_not_email IS FALSE) OR ((hosting_status = 'cant_host') AND (meetup_status = 'does_not_want_to_meetup'))", 

419 name="do_not_email_inactive", 

420 ), 

421 # Superusers must be editors 

422 CheckConstraint( 

423 "(is_superuser IS FALSE) OR (is_editor IS TRUE)", 

424 name="superuser_is_editor", 

425 ), 

426 ) 

427 

428 @hybrid_property 

429 def has_completed_my_home(self) -> bool: 

430 # completed my profile means that: 

431 # 1. has filled out max_guests 

432 # 2. has filled out sleeping_arrangement (sleeping privacy) 

433 # 3. has some text in at least one of the my home free text fields 

434 return ( 

435 self.max_guests is not None 

436 and self.sleeping_arrangement is not None 

437 and ( 

438 self.about_place is not None 

439 or self.other_host_info is not None 

440 or self.sleeping_details is not None 

441 or self.area is not None 

442 or self.house_rules is not None 

443 ) 

444 ) 

445 

446 @has_completed_my_home.inplace.expression 

447 @classmethod 

448 def _has_completed_my_home_expression(cls) -> ColumnElement[bool]: 

449 return and_( 

450 cls.max_guests != None, 

451 cls.sleeping_arrangement != None, 

452 or_( 

453 cls.about_place != None, 

454 cls.other_host_info != None, 

455 cls.sleeping_details != None, 

456 cls.area != None, 

457 cls.house_rules != None, 

458 ), 

459 ) 

460 

461 @hybrid_property 

462 def jailed_missing_tos(self) -> bool: 

463 return self.accepted_tos < TOS_VERSION 

464 

465 @hybrid_property 

466 def jailed_missing_community_guidelines(self) -> bool: 

467 return self.accepted_community_guidelines < GUIDELINES_VERSION 

468 

469 @hybrid_property 

470 def jailed_pending_mod_notes(self) -> Any: 

471 # mod_notes come from a backref in ModNote 

472 return self.mod_notes.where(ModNote.is_pending).count() > 0 

473 

474 @hybrid_property 

475 def jailed_pending_activeness_probe(self) -> Any: 

476 # search for User.pending_activeness_probe 

477 return self.pending_activeness_probe != None 

478 

479 @hybrid_property 

480 def is_jailed(self) -> Any: 

481 return ( 

482 self.jailed_missing_tos 

483 | self.jailed_missing_community_guidelines 

484 | self.is_missing_location 

485 | self.jailed_pending_mod_notes 

486 | self.jailed_pending_activeness_probe 

487 ) 

488 

489 @hybrid_property 

490 def is_missing_location(self) -> bool: 

491 return self.needs_to_update_location 

492 

493 @hybrid_property 

494 def is_visible(self) -> bool: 

495 return self.banned_at is None and self.deleted_at is None 

496 

497 @is_visible.inplace.expression 

498 @classmethod 

499 def _is_visible_expression(cls) -> ColumnElement[bool]: 

500 return and_(cls.banned_at.is_(None), cls.deleted_at.is_(None)) 

501 

502 @property 

503 def coordinates(self) -> tuple[float, float]: 

504 return get_coordinates(self.geom) 

505 

506 @property 

507 def display_joined(self) -> datetime: 

508 """ 

509 Returns the last active time rounded down to the nearest hour. 

510 """ 

511 return self.joined.replace(minute=0, second=0, microsecond=0) 

512 

513 @property 

514 def display_last_active(self) -> datetime: 

515 """ 

516 Returns the last active time rounded down whatever is the "last active" coarsening. 

517 """ 

518 return last_active_coarsen(self.last_active) 

519 

520 @hybrid_property 

521 def phone_is_verified(self) -> bool: 

522 return ( 

523 self.phone_verification_verified is not None 

524 and now() - self.phone_verification_verified < PHONE_VERIFICATION_LIFETIME 

525 ) 

526 

527 @phone_is_verified.inplace.expression 

528 @classmethod 

529 def _phone_is_verified_expression(cls) -> ColumnElement[bool]: 

530 return (cls.phone_verification_verified != None) & ( 

531 now() - cls.phone_verification_verified < PHONE_VERIFICATION_LIFETIME 

532 ) 

533 

534 @hybrid_property 

535 def phone_code_expired(self) -> bool: 

536 return now() - self.phone_verification_sent > SMS_CODE_LIFETIME 

537 

538 def __repr__(self) -> str: 

539 return f"User(id={self.id}, email={self.email}, username={self.username})" 

540 

541 

542class LanguageFluency(enum.Enum): 

543 # note that the numbering is important here, these are ordinal 

544 beginner = 1 

545 conversational = 2 

546 fluent = 3 

547 

548 

549class LanguageAbility(Base, kw_only=True): 

550 __tablename__ = "language_abilities" 

551 __table_args__ = ( 

552 # Users can only have one language ability per language 

553 UniqueConstraint("user_id", "language_code"), 

554 ) 

555 

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

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

558 language_code: Mapped[str] = mapped_column(ForeignKey("languages.code", deferrable=True)) 

559 fluency: Mapped[LanguageFluency] = mapped_column(Enum(LanguageFluency)) 

560 

561 user: Mapped[User] = relationship(init=False, back_populates="language_abilities") 

562 language: Mapped[Language] = relationship(init=False) 

563 

564 

565class RegionVisited(Base, kw_only=True): 

566 __tablename__ = "regions_visited" 

567 __table_args__ = (UniqueConstraint("user_id", "region_code"),) 

568 

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

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

571 region_code: Mapped[str] = mapped_column(ForeignKey("regions.code", deferrable=True)) 

572 

573 

574class RegionLived(Base, kw_only=True): 

575 __tablename__ = "regions_lived" 

576 __table_args__ = (UniqueConstraint("user_id", "region_code"),) 

577 

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

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

580 region_code: Mapped[str] = mapped_column(ForeignKey("regions.code", deferrable=True))