|
| 1 | +from datetime import date, datetime |
| 2 | + |
| 3 | +from sqlalchemy import Table, ForeignKey, Integer, Column |
| 4 | +from sqlalchemy.orm import Mapped, relationship, mapped_column |
| 5 | + |
| 6 | +from src.app.auth.artists.models import user_to_artist_association, ArtistProfile, followers_to_artists_association |
| 7 | +from src.app.auth.producers.models import ( |
| 8 | + ProducerProfile, |
| 9 | + user_to_producer_association, |
| 10 | + followers_to_producers_association, |
| 11 | +) |
| 12 | +from src.app.music.albums.interfaces.da.models import saver_to_albums_association, Album |
| 13 | +from src.app.music.squads.models import Squad, follower_to_squads_association |
| 14 | +from src.app.social.licenses.models import author_to_licenses_association, License |
| 15 | +from src.app.social.playlists.models import Playlist, author_to_playlists_association |
| 16 | +from src.app.social.tags.models import Tag |
| 17 | +from src.domain.auth.users.interfaces.da.models import BaseUserModel |
| 18 | +from src.infrastructure.postgres import Base |
| 19 | + |
| 20 | +follower_to_tag_association = Table( |
| 21 | + "follower_to_tag_association", |
| 22 | + Base.metadata, |
| 23 | + Column("user_id", ForeignKey("users.id"), primary_key=True), |
| 24 | + Column("tag_id", ForeignKey("tags.id"), primary_key=True), |
| 25 | +) |
| 26 | + |
| 27 | +saver_to_playlists_association = Table( |
| 28 | + "saver_to_playlists_association", |
| 29 | + Base.metadata, |
| 30 | + Column("saver_id", Integer, ForeignKey("users.id"), primary_key=True), |
| 31 | + Column("playlist_id", Integer, ForeignKey("playlists.id"), primary_key=True), |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +class User(BaseUserModel, Base): |
| 36 | + __tablename__ = "users" |
| 37 | + |
| 38 | + id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) |
| 39 | + username: Mapped[str] |
| 40 | + description: Mapped[str | None] |
| 41 | + email: Mapped[str] |
| 42 | + password: Mapped[str] |
| 43 | + picture_url: Mapped[str | None] |
| 44 | + |
| 45 | + created_at: Mapped[date] |
| 46 | + updated_at: Mapped[datetime] |
| 47 | + |
| 48 | + access_level: Mapped[str] |
| 49 | + premium_level: Mapped[str] |
| 50 | + telegram_id: Mapped[int | None] |
| 51 | + is_verified: Mapped[bool] |
| 52 | + |
| 53 | + artist_id: Mapped[int] = mapped_column(ForeignKey("artist_profiles.id")) |
| 54 | + producer_id: Mapped[int] = mapped_column(ForeignKey("producer_profiles.id")) |
| 55 | + |
| 56 | + artist_profile: Mapped[ArtistProfile] = relationship( |
| 57 | + argument="ArtistProfile", |
| 58 | + secondary=user_to_artist_association, |
| 59 | + cascade="all, delete-orphan", |
| 60 | + single_parent=True, |
| 61 | + lazy="joined", |
| 62 | + ) |
| 63 | + producer_profile: Mapped[ProducerProfile] = relationship( |
| 64 | + argument="ProducerProfile", |
| 65 | + secondary=user_to_producer_association, |
| 66 | + cascade="all, delete-orphan", |
| 67 | + single_parent=True, |
| 68 | + lazy="joined", |
| 69 | + ) |
| 70 | + licenses: Mapped[list[License]] = relationship( |
| 71 | + argument="License", |
| 72 | + secondary=author_to_licenses_association, |
| 73 | + back_populates="author", |
| 74 | + lazy="selectin", |
| 75 | + ) |
| 76 | + followed_squads: Mapped[list[Squad]] = relationship( |
| 77 | + argument="Squad", |
| 78 | + secondary=follower_to_squads_association, |
| 79 | + back_populates="followers", |
| 80 | + lazy="selectin", |
| 81 | + ) |
| 82 | + followed_artists: Mapped[list[ArtistProfile]] = relationship( |
| 83 | + argument="ArtistProfile", |
| 84 | + secondary=followers_to_artists_association, |
| 85 | + back_populates="followers", |
| 86 | + lazy="selectin", |
| 87 | + ) |
| 88 | + coauthored_playlists: Mapped[list[Playlist]] = relationship( |
| 89 | + argument="Playlist", |
| 90 | + secondary=author_to_playlists_association, |
| 91 | + back_populates="authors", |
| 92 | + lazy="selectin", |
| 93 | + ) |
| 94 | + saved_playlists: Mapped[list[Playlist]] = relationship( |
| 95 | + argument="Playlist", |
| 96 | + secondary=saver_to_playlists_association, |
| 97 | + lazy="selectin", |
| 98 | + ) |
| 99 | + followed_producers: Mapped[list[ProducerProfile]] = relationship( |
| 100 | + argument="ProducerProfile", |
| 101 | + secondary=followers_to_producers_association, |
| 102 | + back_populates="followers", |
| 103 | + lazy="selectin", |
| 104 | + ) |
| 105 | + saved_albums: Mapped[list[Album]] = relationship( |
| 106 | + argument="Album", |
| 107 | + secondary=saver_to_albums_association, |
| 108 | + lazy="selectin", |
| 109 | + ) |
| 110 | + followed_tags: Mapped[list[Tag]] = relationship( |
| 111 | + argument="Tag", |
| 112 | + secondary=follower_to_tag_association, |
| 113 | + lazy="selectin", |
| 114 | + ) |
0 commit comments