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

230 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-05 09:44 +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 select, 

25 text, 

26) 

27from sqlalchemy import LargeBinary as Binary 

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 COMPLETED_PROFILE_MINIMUM_CHAR_LENGTH, 

35 EMAIL_REGEX, 

36 GUIDELINES_VERSION, 

37 PHONE_VERIFICATION_LIFETIME, 

38 SMS_CODE_LIFETIME, 

39 TOS_VERSION, 

40) 

41from couchers.models.activeness_probe import ActivenessProbe 

42from couchers.models.base import Base, Geom 

43from couchers.models.mod_note import ModNote 

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

45from couchers.utils import get_coordinates, last_active_coarsen, now 

46 

47if TYPE_CHECKING: 

48 from couchers.models import UserBadge 

49 from couchers.models.admin import UserAdminTag 

50 from couchers.models.public_trips import PublicTrip 

51 from couchers.models.rest import InviteCode, ModerationUserList 

52 from couchers.models.uploads import PhotoGallery 

53 

54 

55class HostingStatus(enum.Enum): 

56 can_host = enum.auto() 

57 maybe = enum.auto() 

58 cant_host = enum.auto() 

59 

60 

61class MeetupStatus(enum.Enum): 

62 wants_to_meetup = enum.auto() 

63 open_to_meetup = enum.auto() 

64 does_not_want_to_meetup = enum.auto() 

65 

66 

67class SmokingLocation(enum.Enum): 

68 yes = enum.auto() 

69 window = enum.auto() 

70 outside = enum.auto() 

71 no = enum.auto() 

72 

73 

74class SleepingArrangement(enum.Enum): 

75 private = enum.auto() 

76 common = enum.auto() 

77 shared_room = enum.auto() 

78 

79 

80class ParkingDetails(enum.Enum): 

81 free_onsite = enum.auto() 

82 free_offsite = enum.auto() 

83 paid_onsite = enum.auto() 

84 paid_offsite = enum.auto() 

85 

86 

87class ProfilePublicVisibility(enum.Enum): 

88 # no public info 

89 nothing = enum.auto() 

90 # only show on map, randomized, unclickable 

91 map_only = enum.auto() 

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

93 limited = enum.auto() 

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

95 most = enum.auto() 

96 # all but references 

97 full = enum.auto() 

98 

99 

100class User(Base, kw_only=True): 

101 """ 

102 Basic user and profile details 

103 """ 

104 

105 __tablename__ = "users" 

106 

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

108 

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

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

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

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

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

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

115 # language preference -- defaults to empty string 

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

117 

118 # timezones should always be UTC 

119 ## location 

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

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

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

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

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

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

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

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

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

129 # "Grew up in" on profile 

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

131 

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

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

134 ) 

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

136 

137 timezone = column_property( 

138 select(TimezoneArea.tzid).where(func.ST_Contains(TimezoneArea.geom, geom)).limit(1).scalar_subquery(), 

139 deferred=True, 

140 ) 

141 

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

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

144 profile_last_updated: Mapped[datetime] = mapped_column( 

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

146 ) 

147 

148 public_visibility: Mapped[ProfilePublicVisibility] = mapped_column( 

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

150 ) 

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

152 

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

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

155 # same as above for host requests 

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

157 

158 # display name 

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

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

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

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

163 

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

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

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

167 

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

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

170 

171 # community standing score 

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

173 

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

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

176 

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

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

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

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

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

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

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

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

185 

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

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

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

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

190 

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

192 # accidental or they changed their mind 

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

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

195 # validity of the undelete token 

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

197 

198 # hosting preferences 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

216 

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

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

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

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

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

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

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

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

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

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

227 Enum(ParkingDetails), default=None 

228 ) # CommonMark without images 

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

230 

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

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

233 # whether the user has filled in the contributor form 

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

235 

236 # number of onboarding emails sent 

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

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

239 

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

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

242 # opted out of the newsletter 

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

244 

245 # set to null to receive no digests 

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

247 last_digest_sent: Mapped[datetime] = mapped_column( 

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

249 ) 

250 

251 # for changing their email 

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

253 

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

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

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

257 

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

259 

260 mod_score: Mapped[float] = mapped_column(Float, server_default="1", init=False) 

261 

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

263 # ,-------------------, 

264 # | Start | 

265 # | phone = None | someone else 

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

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

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

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

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

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

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

273 # '-----------------' +-------- | ------+----+--------------------+ '-----------------------' 

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

275 # | | ^ V | | 

276 # ,-----------------, | | ,-------------------, | ,-----------------------, 

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

278 # | phone = xx | | phone = xx | | | phone = xx | 

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

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

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

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

283 # '-----------------' '-------------------' '-----------------------' 

284 

285 # randomly generated Luhn 6-digit string 

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

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

288 ) 

289 

290 phone_verification_sent: Mapped[datetime] = mapped_column( 

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

292 ) 

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

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

295 ) 

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

297 

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

299 # e.g. cus_JjoXHttuZopv0t 

300 # for new US entity 

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

302 # for old AU entity 

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

304 

305 has_passport_sex_gender_exception: Mapped[bool] = mapped_column( 

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

307 ) 

308 

309 # checking for phone verification 

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

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

312 ) 

313 

314 # whether this user has all emails turned off 

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

316 

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

318 

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

320 

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

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

323 

324 last_antibot: Mapped[datetime] = mapped_column( 

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

326 ) 

327 

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

329 

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

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

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

333 

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

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

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

337 

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

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

340 ) 

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

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

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

344 ) 

345 mod_notes: DynamicMapped[ModNote] = relationship( 

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

347 ) 

348 

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

350 

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

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

353 ) 

354 

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

356 init=False, 

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

358 uselist=False, 

359 back_populates="user", 

360 ) 

361 

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

363 

364 __table_args__ = ( 

365 # Verified phone numbers should be unique 

366 Index( 

367 "ix_users_unique_phone", 

368 phone, 

369 unique=True, 

370 postgresql_where=phone_verification_verified != None, 

371 ), 

372 Index( 

373 "ix_users_active", 

374 id, 

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

376 ), 

377 Index( 

378 "ix_users_geom_active", 

379 geom, 

380 id, 

381 username, 

382 postgresql_using="gist", 

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

384 ), 

385 Index( 

386 "ix_users_by_id", 

387 id, 

388 postgresql_using="hash", 

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

390 ), 

391 Index( 

392 "ix_users_by_username", 

393 username, 

394 postgresql_using="hash", 

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

396 ), 

397 Index( 

398 "ix_users_visible_with_about_me", 

399 id, 

400 postgresql_where=and_( 

401 banned_at.is_(None), 

402 deleted_at.is_(None), 

403 profile_gallery_id.isnot(None), 

404 func.coalesce(func.character_length(about_me), 0) >= COMPLETED_PROFILE_MINIMUM_CHAR_LENGTH, 

405 ), 

406 ), 

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

408 CheckConstraint( 

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

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

411 name="check_new_email_token_state", 

412 ), 

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

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

415 CheckConstraint( 

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

417 name="phone_verified_conditions", 

418 ), 

419 # Email must match our regex 

420 CheckConstraint( 

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

422 name="valid_email", 

423 ), 

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

425 CheckConstraint( 

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

427 name="undelete_nullity", 

428 ), 

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

430 CheckConstraint( 

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

432 name="do_not_email_inactive", 

433 ), 

434 # Superusers must be editors 

435 CheckConstraint( 

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

437 name="superuser_is_editor", 

438 ), 

439 ) 

440 

441 @hybrid_property 

442 def has_completed_my_home(self) -> bool: 

443 # completed my profile means that: 

444 # 1. has filled out max_guests 

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

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

447 return ( 

448 self.max_guests is not None 

449 and self.sleeping_arrangement is not None 

450 and ( 

451 self.about_place is not None 

452 or self.other_host_info is not None 

453 or self.sleeping_details is not None 

454 or self.area is not None 

455 or self.house_rules is not None 

456 ) 

457 ) 

458 

459 @has_completed_my_home.inplace.expression 

460 @classmethod 

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

462 return and_( 

463 cls.max_guests != None, 

464 cls.sleeping_arrangement != None, 

465 or_( 

466 cls.about_place != None, 

467 cls.other_host_info != None, 

468 cls.sleeping_details != None, 

469 cls.area != None, 

470 cls.house_rules != None, 

471 ), 

472 ) 

473 

474 @hybrid_property 

475 def jailed_missing_tos(self) -> bool: 

476 return self.accepted_tos < TOS_VERSION 

477 

478 @hybrid_property 

479 def jailed_missing_community_guidelines(self) -> bool: 

480 return self.accepted_community_guidelines < GUIDELINES_VERSION 

481 

482 @hybrid_property 

483 def jailed_pending_mod_notes(self) -> Any: 

484 # mod_notes come from a backref in ModNote 

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

486 

487 @hybrid_property 

488 def jailed_pending_activeness_probe(self) -> Any: 

489 # search for User.pending_activeness_probe 

490 return self.pending_activeness_probe != None 

491 

492 @hybrid_property 

493 def is_jailed(self) -> Any: 

494 return ( 

495 self.jailed_missing_tos 

496 | self.jailed_missing_community_guidelines 

497 | self.is_missing_location 

498 | self.jailed_pending_mod_notes 

499 | self.jailed_pending_activeness_probe 

500 ) 

501 

502 @hybrid_property 

503 def is_missing_location(self) -> bool: 

504 return self.needs_to_update_location 

505 

506 @hybrid_property 

507 def is_visible(self) -> bool: 

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

509 

510 @is_visible.inplace.expression 

511 @classmethod 

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

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

514 

515 @property 

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

517 return get_coordinates(self.geom) 

518 

519 @property 

520 def display_joined(self) -> datetime: 

521 """ 

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

523 """ 

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

525 

526 @property 

527 def display_last_active(self) -> datetime: 

528 """ 

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

530 """ 

531 return last_active_coarsen(self.last_active) 

532 

533 @hybrid_property 

534 def phone_is_verified(self) -> bool: 

535 return ( 

536 self.phone_verification_verified is not None 

537 and now() - self.phone_verification_verified < PHONE_VERIFICATION_LIFETIME 

538 ) 

539 

540 @phone_is_verified.inplace.expression 

541 @classmethod 

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

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

544 now() - cls.phone_verification_verified < PHONE_VERIFICATION_LIFETIME 

545 ) 

546 

547 @hybrid_property 

548 def phone_code_expired(self) -> bool: 

549 return now() - self.phone_verification_sent > SMS_CODE_LIFETIME 

550 

551 def __repr__(self) -> str: 

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

553 

554 

555class LanguageFluency(enum.Enum): 

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

557 beginner = 1 

558 conversational = 2 

559 fluent = 3 

560 

561 

562class LanguageAbility(Base, kw_only=True): 

563 __tablename__ = "language_abilities" 

564 __table_args__ = ( 

565 # Users can only have one language ability per language 

566 UniqueConstraint("user_id", "language_code"), 

567 ) 

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 language_code: Mapped[str] = mapped_column(ForeignKey("languages.code", deferrable=True)) 

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

573 

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

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

576 

577 

578class RegionVisited(Base, kw_only=True): 

579 __tablename__ = "regions_visited" 

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

581 

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

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

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

585 

586 

587class RegionLived(Base, kw_only=True): 

588 __tablename__ = "regions_lived" 

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

590 

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

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

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