Initial commit: AudiOhm - Alternative Spotify avec streaming YouTube
Backend: - FastAPI avec PostgreSQL et Redis - Authentification JWT complète - API REST pour musique, playlists, recherche - Streaming audio via yt-dlp - SQLAlchemy 2.0 async Frontend: - Flutter avec thème néon cyberpunk - State management Riverpod - Layout adaptatif desktop/mobile - Lecteur audio avec mini-player Infrastructure: - Docker Compose (PostgreSQL + Redis) - Scripts d'installation automatisés - Scripts de build pour exécutables Fichiers ajoutés: - BUILD_CLIENT_*.bat/sh: Scripts de compilation - BUILD_CLIENT_README.md: Documentation compilation - CHECK_FLUTTER.sh: Vérificateur d'environnement - requirements.txt mis à jour pour Python 3.13 - Modèles SQLAlchemy corrigés (metadata -> extra_metadata) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
"""SQLAlchemy models."""
|
||||
from app.models.album import Album
|
||||
from app.models.artist import Artist
|
||||
from app.models.playlist import Playlist
|
||||
from app.models.playlist_track import PlaylistTrack
|
||||
from app.models.track import Track
|
||||
from app.models.user import User
|
||||
|
||||
__all__ = [
|
||||
"Album",
|
||||
"Artist",
|
||||
"Playlist",
|
||||
"PlaylistTrack",
|
||||
"Track",
|
||||
"User",
|
||||
]
|
||||
@@ -0,0 +1,121 @@
|
||||
"""Album model."""
|
||||
import uuid
|
||||
from datetime import datetime, date
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import String, Integer, DATE, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.artist import Artist
|
||||
from app.models.track import Track
|
||||
|
||||
|
||||
class Album(Base):
|
||||
"""Album model representing music albums."""
|
||||
|
||||
__tablename__ = "albums"
|
||||
|
||||
# Primary key
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Basic info
|
||||
title: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
release_date: Mapped[date | None] = mapped_column(
|
||||
DATE,
|
||||
)
|
||||
image_url: Mapped[str | None] = mapped_column(
|
||||
String(500),
|
||||
)
|
||||
|
||||
# Album details
|
||||
total_tracks: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
default=0,
|
||||
)
|
||||
genre: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
)
|
||||
|
||||
# Foreign key to artist
|
||||
artist_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("artists.id", ondelete="CASCADE"),
|
||||
nullable=True,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# External IDs
|
||||
spotify_id: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
unique=True,
|
||||
index=True,
|
||||
)
|
||||
youtube_playlist_id: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
unique=True,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Additional metadata stored as JSON
|
||||
extra_metadata: Mapped[dict] = mapped_column(
|
||||
"metadata",
|
||||
JSONB,
|
||||
default=dict,
|
||||
)
|
||||
|
||||
# Timestamps
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
onupdate=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
# Relationships
|
||||
artist: Mapped["Artist"] = relationship(
|
||||
"Artist",
|
||||
back_populates="albums",
|
||||
lazy="selectin",
|
||||
)
|
||||
tracks: Mapped[list["Track"]] = relationship(
|
||||
"Track",
|
||||
back_populates="album",
|
||||
cascade="all, delete-orphan",
|
||||
lazy="selectin",
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Album {self.title}>"
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert album model to dictionary."""
|
||||
return {
|
||||
"id": str(self.id),
|
||||
"title": self.title,
|
||||
"release_date": self.release_date.isoformat() if self.release_date else None,
|
||||
"image_url": self.image_url,
|
||||
"total_tracks": self.total_tracks,
|
||||
"genre": self.genre,
|
||||
"artist_id": str(self.artist_id) if self.artist_id else None,
|
||||
"spotify_id": self.spotify_id,
|
||||
"youtube_playlist_id": self.youtube_playlist_id,
|
||||
"metadata": self.extra_metadata,
|
||||
"created_at": self.created_at.isoformat(),
|
||||
"updated_at": self.updated_at.isoformat(),
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
"""Artist model."""
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import String, ARRAY, Integer
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.album import Album
|
||||
from app.models.track import Track
|
||||
|
||||
|
||||
class Artist(Base):
|
||||
"""Artist model representing music artists."""
|
||||
|
||||
__tablename__ = "artists"
|
||||
|
||||
# Primary key
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Basic info
|
||||
name: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
image_url: Mapped[str | None] = mapped_column(
|
||||
String(500),
|
||||
)
|
||||
bio: Mapped[str | None] = mapped_column(
|
||||
String(2000),
|
||||
)
|
||||
|
||||
# Genres as array
|
||||
genres: Mapped[list[str]] = mapped_column(
|
||||
ARRAY(String(100)),
|
||||
default=list,
|
||||
)
|
||||
|
||||
# Popularity score (0-100)
|
||||
popularity: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
default=0,
|
||||
)
|
||||
|
||||
# External IDs
|
||||
spotify_id: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
unique=True,
|
||||
index=True,
|
||||
)
|
||||
youtube_id: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
unique=True,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Additional metadata stored as JSON
|
||||
extra_metadata: Mapped[dict] = mapped_column(
|
||||
"metadata",
|
||||
JSONB,
|
||||
default=dict,
|
||||
)
|
||||
|
||||
# Timestamps
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
onupdate=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
# Relationships
|
||||
albums: Mapped[list["Album"]] = relationship(
|
||||
"Album",
|
||||
back_populates="artist",
|
||||
cascade="all, delete-orphan",
|
||||
lazy="selectin",
|
||||
)
|
||||
tracks: Mapped[list["Track"]] = relationship(
|
||||
"Track",
|
||||
back_populates="artist",
|
||||
cascade="all, delete-orphan",
|
||||
lazy="selectin",
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Artist {self.name}>"
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert artist model to dictionary."""
|
||||
return {
|
||||
"id": str(self.id),
|
||||
"name": self.name,
|
||||
"image_url": self.image_url,
|
||||
"bio": self.bio,
|
||||
"genres": self.genres,
|
||||
"popularity": self.popularity,
|
||||
"spotify_id": self.spotify_id,
|
||||
"youtube_id": self.youtube_id,
|
||||
"metadata": self.extra_metadata,
|
||||
"created_at": self.created_at.isoformat(),
|
||||
"updated_at": self.updated_at.isoformat(),
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
"""Playlist model."""
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import String, Integer, Text, Boolean, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.user import User
|
||||
from app.models.playlist_track import PlaylistTrack
|
||||
|
||||
|
||||
class Playlist(Base):
|
||||
"""Playlist model representing user playlists."""
|
||||
|
||||
__tablename__ = "playlists"
|
||||
|
||||
# Primary key
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Foreign key to user
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Basic info
|
||||
name: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False,
|
||||
)
|
||||
description: Mapped[str | None] = mapped_column(
|
||||
Text,
|
||||
)
|
||||
image_url: Mapped[str | None] = mapped_column(
|
||||
String(500),
|
||||
)
|
||||
|
||||
# Playlist flags
|
||||
is_public: Mapped[bool] = mapped_column(
|
||||
Boolean,
|
||||
default=False,
|
||||
index=True,
|
||||
)
|
||||
is_collaborative: Mapped[bool] = mapped_column(
|
||||
Boolean,
|
||||
default=False,
|
||||
)
|
||||
is_smart: Mapped[bool] = mapped_column(
|
||||
Boolean,
|
||||
default=False,
|
||||
comment="True for rules-based smart playlists",
|
||||
)
|
||||
|
||||
# Smart playlist rules stored as JSON
|
||||
smart_rules: Mapped[dict] = mapped_column(
|
||||
JSONB,
|
||||
default=dict,
|
||||
comment="Rules for smart playlists",
|
||||
)
|
||||
|
||||
# Playlist stats
|
||||
track_count: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
default=0,
|
||||
)
|
||||
total_duration: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
default=0,
|
||||
comment="Total duration in seconds",
|
||||
)
|
||||
|
||||
# Timestamps
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
onupdate=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
# Relationships
|
||||
user: Mapped["User"] = relationship(
|
||||
"User",
|
||||
back_populates="playlists",
|
||||
lazy="selectin",
|
||||
)
|
||||
playlist_tracks: Mapped[list["PlaylistTrack"]] = relationship(
|
||||
"PlaylistTrack",
|
||||
back_populates="playlist",
|
||||
cascade="all, delete-orphan",
|
||||
lazy="selectin",
|
||||
order_by="PlaylistTrack.position",
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Playlist {self.name}>"
|
||||
|
||||
def to_dict(self, include_tracks: bool = False) -> dict:
|
||||
"""Convert playlist model to dictionary."""
|
||||
data = {
|
||||
"id": str(self.id),
|
||||
"user_id": str(self.user_id),
|
||||
"name": self.name,
|
||||
"description": self.description,
|
||||
"image_url": self.image_url,
|
||||
"is_public": self.is_public,
|
||||
"is_collaborative": self.is_collaborative,
|
||||
"is_smart": self.is_smart,
|
||||
"smart_rules": self.smart_rules,
|
||||
"track_count": self.track_count,
|
||||
"total_duration": self.total_duration,
|
||||
"created_at": self.created_at.isoformat(),
|
||||
"updated_at": self.updated_at.isoformat(),
|
||||
}
|
||||
|
||||
if include_tracks:
|
||||
data["tracks"] = [pt.track.to_dict() for pt in self.playlist_tracks]
|
||||
|
||||
return data
|
||||
@@ -0,0 +1,96 @@
|
||||
"""Playlist-Track association model."""
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import Integer, ForeignKey, UniqueConstraint
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.playlist import Playlist
|
||||
from app.models.track import Track
|
||||
from app.models.user import User
|
||||
|
||||
|
||||
class PlaylistTrack(Base):
|
||||
"""Association model for Playlist-Track many-to-many relationship."""
|
||||
|
||||
__tablename__ = "playlist_tracks"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("playlist_id", "position", name="uq_playlist_position"),
|
||||
)
|
||||
|
||||
# Primary key
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Foreign keys
|
||||
playlist_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("playlists.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
track_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("tracks.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Position in playlist (starts at 0)
|
||||
position: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
# User who added this track to playlist
|
||||
added_by: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
)
|
||||
|
||||
# Timestamp when track was added
|
||||
added_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
# Relationships
|
||||
playlist: Mapped["Playlist"] = relationship(
|
||||
"Playlist",
|
||||
back_populates="playlist_tracks",
|
||||
lazy="selectin",
|
||||
)
|
||||
track: Mapped["Track"] = relationship(
|
||||
"Track",
|
||||
lazy="selectin",
|
||||
)
|
||||
added_by_user: Mapped["User"] = relationship(
|
||||
"User",
|
||||
back_populates="added_playlist_tracks",
|
||||
lazy="selectin",
|
||||
foreign_keys=[added_by],
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<PlaylistTrack playlist={self.playlist_id} track={self.track_id} pos={self.position}>"
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert playlist-track model to dictionary."""
|
||||
return {
|
||||
"id": str(self.id),
|
||||
"playlist_id": str(self.playlist_id),
|
||||
"track_id": str(self.track_id),
|
||||
"position": self.position,
|
||||
"added_by": str(self.added_by) if self.added_by else None,
|
||||
"added_at": self.added_at.isoformat(),
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
"""Track model."""
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import String, Integer, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.artist import Artist
|
||||
from app.models.album import Album
|
||||
|
||||
|
||||
class Track(Base):
|
||||
"""Track model representing music tracks."""
|
||||
|
||||
__tablename__ = "tracks"
|
||||
|
||||
# Primary key
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Basic info
|
||||
title: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
duration: Mapped[int | None] = mapped_column(
|
||||
Integer,
|
||||
comment="Duration in seconds",
|
||||
)
|
||||
|
||||
# Track position
|
||||
track_number: Mapped[int | None] = mapped_column(
|
||||
Integer,
|
||||
)
|
||||
disc_number: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
default=1,
|
||||
)
|
||||
|
||||
# Cover art
|
||||
image_url: Mapped[str | None] = mapped_column(
|
||||
String(500),
|
||||
)
|
||||
|
||||
# Audio URLs
|
||||
audio_url: Mapped[str | None] = mapped_column(
|
||||
String(500),
|
||||
comment="Cached audio URL",
|
||||
)
|
||||
audio_quality: Mapped[str | None] = mapped_column(
|
||||
String(20),
|
||||
comment="low, medium, high",
|
||||
)
|
||||
|
||||
# Genre and mood
|
||||
genre: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
)
|
||||
mood: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
)
|
||||
|
||||
# Play count
|
||||
play_count: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
default=0,
|
||||
)
|
||||
|
||||
# Foreign keys
|
||||
artist_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("artists.id", ondelete="CASCADE"),
|
||||
nullable=True,
|
||||
index=True,
|
||||
)
|
||||
album_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("albums.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# External IDs (unique indices)
|
||||
spotify_id: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
unique=True,
|
||||
index=True,
|
||||
)
|
||||
youtube_id: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
unique=True,
|
||||
index=True,
|
||||
)
|
||||
soundcloud_id: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
unique=True,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Additional metadata stored as JSON
|
||||
extra_metadata: Mapped[dict] = mapped_column(
|
||||
"metadata",
|
||||
JSONB,
|
||||
default=dict,
|
||||
)
|
||||
|
||||
# Timestamps
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
onupdate=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
# Relationships
|
||||
artist: Mapped["Artist"] = relationship(
|
||||
"Artist",
|
||||
back_populates="tracks",
|
||||
lazy="selectin",
|
||||
)
|
||||
album: Mapped["Album"] = relationship(
|
||||
"Album",
|
||||
back_populates="tracks",
|
||||
lazy="selectin",
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Track {self.title}>"
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert track model to dictionary."""
|
||||
return {
|
||||
"id": str(self.id),
|
||||
"title": self.title,
|
||||
"duration": self.duration,
|
||||
"track_number": self.track_number,
|
||||
"disc_number": self.disc_number,
|
||||
"image_url": self.image_url,
|
||||
"audio_url": self.audio_url,
|
||||
"audio_quality": self.audio_quality,
|
||||
"genre": self.genre,
|
||||
"mood": self.mood,
|
||||
"play_count": self.play_count,
|
||||
"artist_id": str(self.artist_id) if self.artist_id else None,
|
||||
"album_id": str(self.album_id) if self.album_id else None,
|
||||
"spotify_id": self.spotify_id,
|
||||
"youtube_id": self.youtube_id,
|
||||
"soundcloud_id": self.soundcloud_id,
|
||||
"metadata": self.extra_metadata,
|
||||
"created_at": self.created_at.isoformat(),
|
||||
"updated_at": self.updated_at.isoformat(),
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
"""User model."""
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import DATE, String, Boolean, JSON
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.playlist import Playlist
|
||||
from app.models.playlist_track import PlaylistTrack
|
||||
|
||||
|
||||
class User(Base):
|
||||
"""User model for authentication and user management."""
|
||||
|
||||
__tablename__ = "users"
|
||||
|
||||
# Primary key
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# Authentication fields
|
||||
email: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
unique=True,
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
password_hash: Mapped[str] = mapped_column(
|
||||
String(255),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
# Profile fields
|
||||
username: Mapped[str] = mapped_column(
|
||||
String(50),
|
||||
unique=True,
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
display_name: Mapped[str | None] = mapped_column(
|
||||
String(100),
|
||||
)
|
||||
avatar_url: Mapped[str | None] = mapped_column(
|
||||
String(500),
|
||||
)
|
||||
date_of_birth: Mapped[datetime | None] = mapped_column(
|
||||
DATE,
|
||||
)
|
||||
country: Mapped[str | None] = mapped_column(
|
||||
String(2),
|
||||
)
|
||||
|
||||
# Preferences and settings stored as JSON
|
||||
preferences: Mapped[dict] = mapped_column(
|
||||
JSON,
|
||||
default=dict,
|
||||
)
|
||||
|
||||
# Premium status (for future use)
|
||||
is_premium: Mapped[bool] = mapped_column(
|
||||
Boolean,
|
||||
default=False,
|
||||
)
|
||||
|
||||
# Timestamps
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
default=datetime.utcnow,
|
||||
onupdate=datetime.utcnow,
|
||||
nullable=False,
|
||||
)
|
||||
last_login: Mapped[datetime | None] = mapped_column(
|
||||
default=None,
|
||||
)
|
||||
|
||||
# Relationships
|
||||
playlists: Mapped[list["Playlist"]] = relationship(
|
||||
"Playlist",
|
||||
back_populates="user",
|
||||
cascade="all, delete-orphan",
|
||||
lazy="selectin",
|
||||
)
|
||||
|
||||
added_playlist_tracks: Mapped[list["PlaylistTrack"]] = relationship(
|
||||
"PlaylistTrack",
|
||||
back_populates="added_by_user",
|
||||
foreign_keys="PlaylistTrack.added_by",
|
||||
lazy="selectin",
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<User {self.username} ({self.email})>"
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert user model to dictionary (excluding sensitive data)."""
|
||||
return {
|
||||
"id": str(self.id),
|
||||
"email": self.email,
|
||||
"username": self.username,
|
||||
"display_name": self.display_name,
|
||||
"avatar_url": self.avatar_url,
|
||||
"date_of_birth": self.date_of_birth.isoformat() if self.date_of_birth else None,
|
||||
"country": self.country,
|
||||
"preferences": self.preferences,
|
||||
"is_premium": self.is_premium,
|
||||
"created_at": self.created_at.isoformat(),
|
||||
"updated_at": self.updated_at.isoformat(),
|
||||
"last_login": self.last_login.isoformat() if self.last_login else None,
|
||||
}
|
||||
Reference in New Issue
Block a user