extract SQLAlchemy model in separate python library
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
|
||||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy.orm import relationship, mapped_column, Mapped
|
||||
|
||||
from src.db.models.base import Base, BaseMixin
|
||||
|
||||
|
||||
class Profile(Base, BaseMixin):
|
||||
__tablename__ = 'profile'
|
||||
first_name: Mapped[str]
|
||||
last_name: Mapped[str]
|
||||
user_name: Mapped[str] = mapped_column(nullable=False)
|
||||
email: Mapped[str]
|
||||
password: Mapped[str]
|
||||
enabled: Mapped[bool]
|
||||
assignments: Mapped[List["Assignment"]] = relationship(back_populates="profile")
|
||||
tokens: Mapped[List["Token"]] = relationship(back_populates="profile")
|
||||
|
||||
def get_full_name(self) -> str:
|
||||
full_name: str = ""
|
||||
if self.first_name is not None:
|
||||
full_name += str(self.first_name)
|
||||
if self.last_name is not None:
|
||||
if len(full_name) > 0:
|
||||
full_name += " "
|
||||
full_name += str(self.last_name)
|
||||
return full_name
|
||||
|
||||
def __str__(self):
|
||||
return f"Profile({self.id} {self.user_name}, {self.email})"
|
||||
|
||||
|
||||
class Token(Base, BaseMixin):
|
||||
__tablename__ = "token"
|
||||
token: Mapped[str] = mapped_column(nullable=False, unique=True)
|
||||
name: Mapped[str]
|
||||
last_used_date: Mapped[datetime]
|
||||
enabled: Mapped[bool]
|
||||
profile_id = mapped_column(ForeignKey("profile.id"), nullable=False)
|
||||
profile: Mapped[Profile] = relationship(back_populates="tokens")
|
||||
|
||||
|
||||
class Permission(Base, BaseMixin):
|
||||
__tablename__ = "permission"
|
||||
name: Mapped[str] = mapped_column(nullable=False)
|
||||
assignments: Mapped[List["Assignment"]] = relationship(back_populates="permission")
|
||||
|
||||
|
||||
class Assignment(Base, BaseMixin):
|
||||
__tablename__ = "assignment"
|
||||
profile_id = mapped_column(ForeignKey("profile.id"), nullable=False)
|
||||
profile: Mapped[Profile] = relationship(back_populates="assignments")
|
||||
permission_id = mapped_column(ForeignKey("permission.id"), nullable=False)
|
||||
permission: Mapped[Permission] = relationship(back_populates="assignments")
|
||||
|
||||
|
||||
class MailAccount(Base, BaseMixin):
|
||||
__tablename__ = "mail_account"
|
||||
host: Mapped[str]
|
||||
port: Mapped[int]
|
||||
protocol: Mapped[str]
|
||||
user_name: Mapped[str]
|
||||
password: Mapped[str]
|
||||
start_tls: Mapped[bool]
|
||||
|
||||
|
||||
class Mail(Base, BaseMixin):
|
||||
__tablename__ = "mail"
|
||||
folder: Mapped[str]
|
||||
subject: Mapped[str]
|
||||
body: Mapped[str]
|
||||
sent_date: Mapped[datetime]
|
||||
received_date: Mapped[datetime]
|
||||
@@ -1,27 +0,0 @@
|
||||
from typing import Optional
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class BaseMixin:
|
||||
id: Mapped[str] = mapped_column(primary_key=True, default=str(uuid.uuid4()))
|
||||
created_date: Mapped[datetime] = mapped_column(default=func.now())
|
||||
last_modified_date: Mapped[datetime] = mapped_column(default=func.now())
|
||||
version: Mapped[int] = mapped_column(default=0)
|
||||
|
||||
|
||||
class BaseVideoMixin:
|
||||
cloud_link: Mapped[Optional[str]]
|
||||
file_name: Mapped[Optional[str]]
|
||||
path: Mapped[str]
|
||||
review: Mapped[bool]
|
||||
title: Mapped[str]
|
||||
url: Mapped[str]
|
||||
should_download: Mapped[bool]
|
||||
@@ -1,52 +0,0 @@
|
||||
from typing import List
|
||||
|
||||
from sqlalchemy import Column, ForeignKey, Integer, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from src.db.models.base import Base, BaseMixin
|
||||
|
||||
|
||||
class Article(Base, BaseMixin):
|
||||
__tablename__ = 'article'
|
||||
title: Mapped[str] = mapped_column(unique=True)
|
||||
article_authors = relationship("ArticleAuthor")
|
||||
|
||||
|
||||
class Author(Base, BaseMixin):
|
||||
__tablename__ = 'author'
|
||||
first_name: Mapped[str]
|
||||
last_name: Mapped[str]
|
||||
article_authors: Mapped[List["ArticleAuthor"]] = relationship(back_populates="author")
|
||||
book_authors: Mapped[List["BookAuthor"]] = relationship(back_populates="author")
|
||||
|
||||
|
||||
class BookshelfPublisher(Base, BaseMixin):
|
||||
__tablename__ = 'bookshelf_publisher'
|
||||
name: Mapped[str] = mapped_column(unique=True)
|
||||
books: Mapped[List["Book"]] = relationship(back_populates="publisher")
|
||||
|
||||
|
||||
class Book(Base, BaseMixin):
|
||||
__tablename__ = 'book'
|
||||
isbn: Mapped[str] = mapped_column(unique=True)
|
||||
title: Mapped[str]
|
||||
year: Mapped[int] = mapped_column(nullable=False)
|
||||
publisher_id: Mapped[str] = mapped_column(ForeignKey("bookshelf_publisher.id"), nullable=False)
|
||||
publisher: Mapped[BookshelfPublisher] = relationship(back_populates="books")
|
||||
book_authors: Mapped[List["BookAuthor"]] = relationship(back_populates="book")
|
||||
|
||||
|
||||
class ArticleAuthor(Base, BaseMixin):
|
||||
__tablename__ = 'article_author'
|
||||
article_id: Mapped[str] = mapped_column(ForeignKey("article.id"), nullable=False)
|
||||
article: Mapped[Article] = relationship(back_populates="article_authors")
|
||||
author_id: Mapped[str] = mapped_column(ForeignKey("author.id"), nullable=False)
|
||||
author: Mapped[Author] = relationship(back_populates="article_authors")
|
||||
|
||||
|
||||
class BookAuthor(Base, BaseMixin):
|
||||
__tablename__ = 'book_author'
|
||||
author_id: Mapped[str] = mapped_column(ForeignKey("author.id"), nullable=False)
|
||||
author: Mapped[Author] = relationship(back_populates="book_authors")
|
||||
book_id: Mapped[str] = mapped_column(ForeignKey("book.id"), nullable=False)
|
||||
book: Mapped[Book] = relationship(back_populates="book_authors")
|
||||
@@ -1,188 +0,0 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Dict, List, Optional, Any
|
||||
from natsort import natsorted
|
||||
from sqlalchemy import ForeignKey, func
|
||||
from sqlalchemy.orm import relationship, Mapped, mapped_column
|
||||
|
||||
from src.db.models.base import Base, BaseMixin
|
||||
|
||||
|
||||
class Publisher(Base):
|
||||
__tablename__ = "publisher"
|
||||
id: Mapped[str] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||
created_date: Mapped[datetime] = mapped_column(default=func.now())
|
||||
last_modified_date: Mapped[datetime] = mapped_column(default=func.now())
|
||||
version: Mapped[int] = mapped_column(default=0)
|
||||
name: Mapped[str] = mapped_column(unique=True)
|
||||
weblink: Mapped[Optional[str]]
|
||||
parent_publisher_id: Mapped[Optional[str]] = mapped_column(ForeignKey('publisher.id'))
|
||||
parent_publisher: Mapped[Optional['Publisher']] = relationship(back_populates="imprints", remote_side=[id])
|
||||
imprints: Mapped[List['Publisher']] = relationship(back_populates="parent_publisher")
|
||||
comics: Mapped[List["Comic"]] = relationship(back_populates="publisher")
|
||||
|
||||
def __repr__(self):
|
||||
return f'Publisher({self.id} {self.name})'
|
||||
|
||||
def __str__(self):
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class Comic(Base, BaseMixin):
|
||||
__tablename__ = 'comic'
|
||||
title: Mapped[str] = mapped_column(unique=True)
|
||||
publisher_id: Mapped[str] = mapped_column(ForeignKey('publisher.id'), nullable=False)
|
||||
publisher: Mapped[Publisher] = relationship(back_populates="comics")
|
||||
current_order: Mapped[bool]
|
||||
completed: Mapped[bool]
|
||||
weblink: Mapped[Optional[str]]
|
||||
issues: Mapped[List["Issue"]] = relationship(back_populates="comic", order_by="Issue.issue_number")
|
||||
story_arcs: Mapped[List["StoryArc"]] = relationship(back_populates="comic")
|
||||
trade_paperbacks: Mapped[List["TradePaperback"]] = relationship(back_populates="comic")
|
||||
volumes: Mapped[List["Volume"]] = relationship(back_populates="comic")
|
||||
comic_works: Mapped[List["ComicWork"]] = relationship(back_populates="comic")
|
||||
|
||||
def __repr__(self):
|
||||
return f'Comic({self.id} {self.version} {self.title} {self.publisher.name})'
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.title}({self.id})'
|
||||
|
||||
def get_artists(self) -> Dict[Any, List[Any]]:
|
||||
works: Dict[Any, List[Any]] = {}
|
||||
for work in self.comic_works:
|
||||
work_type = work.work_type
|
||||
artist = work.artist
|
||||
if work_type in works:
|
||||
works[work_type].append(artist)
|
||||
else:
|
||||
works[work_type] = [artist]
|
||||
return works
|
||||
|
||||
def sorted_issues(self):
|
||||
sorted_issues = natsorted(self.issues, key=lambda x: getattr(x, 'issue_number'))
|
||||
return sorted_issues
|
||||
|
||||
|
||||
class Volume(Base, BaseMixin):
|
||||
__tablename__ = "volume"
|
||||
name: Mapped[str] = mapped_column(nullable=False)
|
||||
comic_id: Mapped[str] = mapped_column(ForeignKey("comic.id"), nullable=False)
|
||||
comic: Mapped[Comic] = relationship(back_populates="volumes")
|
||||
story_arcs: Mapped[List["StoryArc"]] = relationship(back_populates="volume")
|
||||
issues: Mapped[List["Issue"]] = relationship(back_populates="volume")
|
||||
|
||||
|
||||
class TradePaperback(Base, BaseMixin):
|
||||
__tablename__ = "trade_paperback"
|
||||
name: Mapped[str] = mapped_column(nullable=False)
|
||||
issue_start: Mapped[int]
|
||||
issue_end: Mapped[int]
|
||||
comic_id: Mapped[str] = mapped_column(ForeignKey("comic.id"), nullable=False)
|
||||
comic: Mapped[Comic] = relationship(back_populates="trade_paperbacks")
|
||||
|
||||
|
||||
class StoryArc(Base, BaseMixin):
|
||||
__tablename__ = "story_arc"
|
||||
name: Mapped[str] = mapped_column(nullable=False)
|
||||
comic_id: Mapped[str] = mapped_column(ForeignKey("comic.id"), nullable=False)
|
||||
comic: Mapped[Comic] = relationship(back_populates="story_arcs")
|
||||
volume_id: Mapped[str] = mapped_column(ForeignKey("volume.id"), nullable=True)
|
||||
volume: Mapped[Volume] = relationship(back_populates="story_arcs")
|
||||
issues: Mapped[List["Issue"]] = relationship(back_populates="story_arc")
|
||||
|
||||
|
||||
class Issue(Base, BaseMixin):
|
||||
__tablename__ = "issue"
|
||||
issue_number: Mapped[str]
|
||||
title: Mapped[Optional[str]]
|
||||
published_on: Mapped[Optional[datetime]]
|
||||
in_stock: Mapped[bool]
|
||||
is_read: Mapped[bool]
|
||||
comic_id: Mapped[str] = mapped_column(ForeignKey("comic.id"), nullable=False)
|
||||
comic: Mapped[Comic] = relationship(back_populates="issues")
|
||||
volume_id: Mapped[str] = mapped_column(ForeignKey("volume.id"), nullable=True)
|
||||
volume: Mapped[Volume] = relationship(back_populates="issues")
|
||||
story_arc_id: Mapped[str] = mapped_column(ForeignKey("story_arc.id"), nullable=True)
|
||||
story_arc: Mapped[StoryArc] = relationship(back_populates="issues")
|
||||
issue_works: Mapped[List["IssueWork"]] = relationship(back_populates="issue")
|
||||
|
||||
def get_full_title(self) -> str:
|
||||
full_title: str = str(self.issue_number)
|
||||
if self.title:
|
||||
full_title += str(": " + self.title)
|
||||
return full_title
|
||||
|
||||
def get_artists(self) -> Dict[Any, List[Any]]:
|
||||
works: Dict[Any, List[Any]] = {}
|
||||
for work in self.issue_works:
|
||||
work_type = work.work_type
|
||||
artist = work.artist
|
||||
if work_type in works:
|
||||
works[work_type].append(artist)
|
||||
else:
|
||||
works[work_type] = [artist]
|
||||
return works
|
||||
|
||||
|
||||
class Artist(Base, BaseMixin):
|
||||
__tablename__ = "artist"
|
||||
name: Mapped[str] = mapped_column(nullable=False)
|
||||
weblink: Mapped[str] = mapped_column(nullable=True)
|
||||
comic_works: Mapped[List["ComicWork"]] = relationship(back_populates="artist")
|
||||
issue_works: Mapped[List["IssueWork"]] = relationship(back_populates="artist")
|
||||
|
||||
def get_comics(self) -> Dict[Any, List[Comic]]:
|
||||
works: Dict[Any, List[Comic]] = {}
|
||||
for work in self.comic_works:
|
||||
work_type = work.work_type
|
||||
comic = work.comic
|
||||
if work_type in works:
|
||||
works[work_type].append(comic)
|
||||
else:
|
||||
works[work_type] = [comic]
|
||||
return works
|
||||
|
||||
|
||||
class WorkType(Base, BaseMixin):
|
||||
__tablename__ = "worktype"
|
||||
name: Mapped[str] = mapped_column(nullable=False, unique=True)
|
||||
comic_works: Mapped[List["ComicWork"]] = relationship(back_populates="work_type")
|
||||
issue_works: Mapped[List["IssueWork"]] = relationship(back_populates="work_type")
|
||||
|
||||
def get_artists(self) -> Dict[str, List[Artist]]:
|
||||
works: Dict[str, List[Artist]] = {}
|
||||
for work in self.comic_works:
|
||||
comic = work.comic.title
|
||||
artist = work.artist
|
||||
if comic in works:
|
||||
works[comic].append(artist)
|
||||
else:
|
||||
works[comic] = [artist]
|
||||
return works
|
||||
|
||||
def __repr__(self):
|
||||
return f'Worktype({self.id} {self.version} {self.name} {len(self.comic_works)})'
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.name}({self.id})'
|
||||
|
||||
|
||||
class ComicWork(Base, BaseMixin):
|
||||
__tablename__ = "comic_work"
|
||||
comic_id: Mapped[str] = mapped_column(ForeignKey("comic.id"), nullable=False)
|
||||
comic: Mapped[Comic] = relationship(back_populates="comic_works")
|
||||
artist_id: Mapped[str] = mapped_column(ForeignKey("artist.id"), nullable=False)
|
||||
artist: Mapped[Artist] = relationship(back_populates="comic_works")
|
||||
work_type_id: Mapped[str] = mapped_column(ForeignKey("worktype.id"), nullable=False)
|
||||
work_type: Mapped[WorkType] = relationship(back_populates="comic_works")
|
||||
|
||||
|
||||
class IssueWork(Base, BaseMixin):
|
||||
__tablename__ = "issue_work"
|
||||
issue_id: Mapped[str] = mapped_column(ForeignKey("issue.id"), nullable=False)
|
||||
issue: Mapped[Issue] = relationship(back_populates="issue_works")
|
||||
artist_id: Mapped[str] = mapped_column(ForeignKey("artist.id"), nullable=False)
|
||||
artist: Mapped[Artist] = relationship(back_populates="issue_works")
|
||||
work_type_id: Mapped[str] = mapped_column(ForeignKey("worktype.id"), nullable=False)
|
||||
work_type: Mapped[WorkType] = relationship(back_populates="issue_works")
|
||||
@@ -1,97 +0,0 @@
|
||||
import logging
|
||||
import re
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy.orm import Mapped, relationship, mapped_column
|
||||
|
||||
from src.db.models.base import Base, BaseMixin, BaseVideoMixin
|
||||
|
||||
|
||||
class MediaFile(Base, BaseMixin, BaseVideoMixin):
|
||||
"""
|
||||
MediaFile represents video link.
|
||||
"""
|
||||
|
||||
__tablename__ = "media_file"
|
||||
media_actor_files: Mapped[List["MediaActorFile"]] = relationship(
|
||||
back_populates="media_file"
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return f"MediaFile({self.id} {self.title} {self.title})"
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.title}({self.id})"
|
||||
|
||||
def update_title(self):
|
||||
"""
|
||||
Update title from url.
|
||||
"""
|
||||
|
||||
|
||||
class MediaActor(Base, BaseMixin):
|
||||
__tablename__ = "media_actor"
|
||||
name: Mapped[str]
|
||||
url: Mapped[Optional[str]] = mapped_column(unique=True)
|
||||
media_actor_files = relationship("MediaActorFile")
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"MediaActor({self.id} {self.name} {self.url})"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.url}({self.id})"
|
||||
|
||||
|
||||
class MediaActorFile(Base, BaseMixin):
|
||||
__tablename__ = "media_actor_file"
|
||||
media_actor_id: Mapped[str] = mapped_column(
|
||||
ForeignKey("media_actor.id"), nullable=False
|
||||
)
|
||||
media_actor: Mapped[MediaActor] = relationship(back_populates="media_actor_files")
|
||||
media_file_id: Mapped[str] = mapped_column(
|
||||
ForeignKey("media_file.id"), nullable=True
|
||||
)
|
||||
media_file: Mapped[MediaFile] = relationship(back_populates="media_actor_files")
|
||||
|
||||
def __repr__(self):
|
||||
return f"MediaActorFile({self.id} {self.media_actor_id} {self.media_file_id})"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.id} {self.media_actor_id} {self.media_file_id}"
|
||||
|
||||
|
||||
class MediaArticle(Base, BaseMixin):
|
||||
__tablename__ = "media_article"
|
||||
review: Mapped[bool]
|
||||
title: Mapped[str]
|
||||
url: Mapped[str] = mapped_column(unique=True)
|
||||
|
||||
|
||||
class MediaVideo(Base, BaseMixin):
|
||||
"""
|
||||
MediaFile represents video link.
|
||||
"""
|
||||
|
||||
__tablename__ = "media_video"
|
||||
cloud_link: Mapped[str]
|
||||
file_name: Mapped[str]
|
||||
path: Mapped[str]
|
||||
review: Mapped[bool]
|
||||
title: Mapped[str]
|
||||
url: Mapped[str] = mapped_column(unique=True)
|
||||
should_download: Mapped[bool]
|
||||
|
||||
def __repr__(self):
|
||||
return f"MediaFile({self.id} {self.title} {self.url})"
|
||||
|
||||
def __str__(self):
|
||||
if self.title is None:
|
||||
return f"{self.url}({self.id})"
|
||||
else:
|
||||
return f"{self.title}({self.id})"
|
||||
@@ -1,100 +0,0 @@
|
||||
from typing import List
|
||||
from sqlalchemy import ForeignKey, UniqueConstraint
|
||||
from sqlalchemy.orm import relationship, mapped_column, Mapped
|
||||
|
||||
from src.db.models.base import Base, BaseMixin
|
||||
|
||||
|
||||
class Sport(Base, BaseMixin):
|
||||
__tablename__ = "sport"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("name"),
|
||||
)
|
||||
name: Mapped[str] = mapped_column(nullable=False, index=True, unique=True)
|
||||
teams: Mapped[List["Team"]] = relationship(back_populates="sport")
|
||||
positions: Mapped[List["FieldPosition"]] = relationship(back_populates="sport")
|
||||
|
||||
|
||||
class Team(Base, BaseMixin):
|
||||
__tablename__ = "team"
|
||||
name: Mapped[str] = mapped_column(nullable=False, index=True, unique=True)
|
||||
short_name: Mapped[str] = mapped_column(nullable=False)
|
||||
sport_id: Mapped[str] = mapped_column(ForeignKey("sport.id"), nullable=False)
|
||||
sport: Mapped[Sport] = relationship(back_populates="teams")
|
||||
roosters: Mapped[List["Rooster"]] = relationship(back_populates="team")
|
||||
|
||||
|
||||
class FieldPosition(Base, BaseMixin):
|
||||
__tablename__ = "field_position"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("name", "sport_id"),
|
||||
UniqueConstraint("short_name", "sport_id"),
|
||||
)
|
||||
name: Mapped[str] = mapped_column(nullable=False, index=True)
|
||||
short_name: Mapped[str] = mapped_column(nullable=False)
|
||||
sport_id: Mapped[str] = mapped_column(ForeignKey("sport.id"), nullable=False, index=True)
|
||||
sport: Mapped[Sport] = relationship(back_populates="positions")
|
||||
roosters: Mapped[List["Rooster"]] = relationship(back_populates="position")
|
||||
|
||||
|
||||
class Player(Base, BaseMixin):
|
||||
__tablename__ = "player"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("first_name", "last_name"),
|
||||
)
|
||||
first_name: Mapped[str] = mapped_column(nullable=False, index=True)
|
||||
last_name: Mapped[str] = mapped_column(nullable=False, index=True)
|
||||
roosters: Mapped[List["Rooster"]] = relationship(back_populates="player")
|
||||
|
||||
def get_full_name(self) -> str:
|
||||
return f"{self.last_name}, {self.first_name}"
|
||||
|
||||
|
||||
class Rooster(Base, BaseMixin):
|
||||
__tablename__ = "rooster"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("year", "team_id", "player_id", "position_id"),
|
||||
)
|
||||
year: Mapped[int]
|
||||
team_id: Mapped[str] = mapped_column(ForeignKey("team.id"), nullable=False, index=True)
|
||||
team: Mapped[Team] = relationship(back_populates="roosters")
|
||||
player_id: Mapped[str] = mapped_column(ForeignKey("player.id"), nullable=False, index=True)
|
||||
player: Mapped[Player] = relationship(back_populates="roosters")
|
||||
position_id: Mapped[str] = mapped_column(ForeignKey("field_position.id"), nullable=False, index=True)
|
||||
position: Mapped[FieldPosition] = relationship(back_populates="roosters")
|
||||
cards: Mapped[List["Card"]] = relationship(back_populates="rooster")
|
||||
|
||||
|
||||
class Vendor(Base, BaseMixin):
|
||||
__tablename__ = "vendor"
|
||||
name: Mapped[str] = mapped_column(nullable=False, unique=True, index=True)
|
||||
card_sets: Mapped[List["CardSet"]] = relationship(back_populates="vendor")
|
||||
cards: Mapped[List["Card"]] = relationship(back_populates="vendor")
|
||||
|
||||
|
||||
class CardSet(Base, BaseMixin):
|
||||
__tablename__ = "card_set"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("name", "vendor_id"),
|
||||
)
|
||||
name: Mapped[str] = mapped_column(index=True)
|
||||
parallel_set: Mapped[bool]
|
||||
insert_set: Mapped[bool]
|
||||
vendor_id: Mapped[str] = mapped_column(ForeignKey("vendor.id"), nullable=False, index=True)
|
||||
vendor: Mapped[Vendor] = relationship(back_populates="card_sets")
|
||||
cards: Mapped[List["Card"]] = relationship(back_populates="card_set")
|
||||
|
||||
|
||||
class Card(Base, BaseMixin):
|
||||
__tablename__ = "card"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("card_number", "year", "vendor_id", "card_set_id"),
|
||||
)
|
||||
card_number: Mapped[int] = mapped_column(index=True)
|
||||
year: Mapped[int] = mapped_column(index=True)
|
||||
card_set_id: Mapped[str] = mapped_column(ForeignKey("card_set.id"), nullable=False)
|
||||
card_set: Mapped[CardSet] = relationship(back_populates="cards")
|
||||
rooster_id: Mapped[str] = mapped_column(ForeignKey("rooster.id"), nullable=False)
|
||||
rooster: Mapped[Rooster] = relationship(back_populates="cards")
|
||||
vendor_id: Mapped[str] = mapped_column(ForeignKey("vendor.id"), nullable=False)
|
||||
vendor: Mapped[Vendor] = relationship(back_populates="cards")
|
||||
@@ -1,18 +0,0 @@
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.db.models.admin import Profile
|
||||
|
||||
|
||||
def get_profile_by_username(username: str, db: Session) -> Optional[Profile]:
|
||||
profile = db.query(Profile).filter(Profile.user_name == username).first()
|
||||
return profile
|
||||
|
||||
def get_profile_by_email(email: str, db: Session) -> Optional[Profile]:
|
||||
profile = db.query(Profile).filter(Profile.email == email).first()
|
||||
return profile
|
||||
|
||||
def is_database_empty(db: Session) -> bool:
|
||||
profiles = db.query(Profile).all()
|
||||
return len(profiles) == 0
|
||||
@@ -1,47 +0,0 @@
|
||||
import uuid
|
||||
from typing import List, Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.core.log_conf import logger
|
||||
from src.db.models.comic import Artist
|
||||
from src.schema.comics.artist import AddArtist
|
||||
from src.schema.comics.artist_details import ArtistDetailResponse, ArtistWorktypeComicResponse, ArtistWorktypeIssueResponse
|
||||
from src.schema.comics.comic import ComicResponse
|
||||
from src.schema.comics.worktype import WorktypeResponse
|
||||
|
||||
|
||||
def get_artist_details(artist: Artist) -> ArtistDetailResponse:
|
||||
comic_works: List[ArtistWorktypeComicResponse] = []
|
||||
comic_works_map = {}
|
||||
for work in artist.comic_works:
|
||||
worktype_id = work.work_type.id
|
||||
if worktype_id in comic_works_map:
|
||||
comic = ComicResponse(id=work.comic.id, title=work.comic.title, completed=work.comic.completed)
|
||||
comic_works_map[worktype_id].comics.append(comic)
|
||||
else:
|
||||
comic_works_map[worktype_id] = ArtistWorktypeComicResponse(
|
||||
worktype=WorktypeResponse(id=worktype_id, name=work.work_type.name),
|
||||
comics=[ComicResponse(id=work.comic.id, title=work.comic.title, completed=work.comic.completed)]
|
||||
)
|
||||
for value in comic_works_map.values():
|
||||
comic_works.append(value)
|
||||
issue_works: List[ArtistWorktypeIssueResponse] = []
|
||||
response = ArtistDetailResponse(
|
||||
id=artist.id,
|
||||
name=str(artist.name),
|
||||
weblink=str(artist.weblink),
|
||||
comic_works=comic_works,
|
||||
issue_works=issue_works,
|
||||
)
|
||||
return response
|
||||
|
||||
def update_artist(add_artist: AddArtist, artist_id: str, db: Session) -> Optional[Artist]:
|
||||
logger.info("update artist")
|
||||
artist: Optional[Artist] = db.get(Artist, artist_id)
|
||||
if artist is not None:
|
||||
artist.name = add_artist.name
|
||||
db.add(artist)
|
||||
db.commit()
|
||||
db.refresh(artist)
|
||||
return artist
|
||||
@@ -1,72 +0,0 @@
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.core.log_conf import logger
|
||||
from src.db.models.comic import Comic, Issue
|
||||
from src.schema.comics.artist import artist_to_response
|
||||
from src.schema.comics.comic import ComicSchema, comic_to_response
|
||||
from src.schema.comics.comic_details import ComicDetailsResponse, ComicWorktypeArtistResponse
|
||||
from src.schema.comics.issue import IssueResponse, issue_to_response
|
||||
from src.schema.comics.issue_details import IssueDetailsResponse
|
||||
from src.schema.comics.publisher import publisher_to_response
|
||||
from src.schema.comics.volume import VolumeResponse, volume_to_response
|
||||
from src.schema.comics.worktype import worktype_to_response
|
||||
|
||||
|
||||
def get_issue_details(issue: Issue) -> IssueDetailsResponse:
|
||||
volume = None
|
||||
if issue.volume:
|
||||
volume = volume_to_response(issue.volume)
|
||||
response = IssueDetailsResponse(
|
||||
id=issue.id,
|
||||
issue_number=str(issue.issue_number),
|
||||
in_stock=bool(issue.in_stock),
|
||||
is_read=bool(issue.is_read),
|
||||
comic=comic_to_response(issue.comic),
|
||||
volume=volume
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
def update_comic(new_comic: ComicSchema, comic_id: str, db: Session) -> Optional[Comic]:
|
||||
logger.info(f"update_comic: {new_comic} with {comic_id}")
|
||||
comic: Optional[Comic] = db.get(Comic, comic_id)
|
||||
return comic
|
||||
|
||||
def get_comic_details(comic: Comic) -> ComicDetailsResponse:
|
||||
volumes: List[VolumeResponse] = []
|
||||
for volume in comic.volumes:
|
||||
volumes.append(volume_to_response(volume))
|
||||
issues: List[IssueResponse] = []
|
||||
for issue in comic.issues:
|
||||
issues.append(issue_to_response(issue))
|
||||
works: List[ComicWorktypeArtistResponse] = []
|
||||
works_map: Dict[str, ComicWorktypeArtistResponse] = {}
|
||||
for work in comic.comic_works:
|
||||
worktype_id = work.work_type.id
|
||||
if worktype_id in works_map:
|
||||
artist = artist_to_response(work.artist)
|
||||
works_map[worktype_id].artists.append(artist)
|
||||
logger.info(f"add artist to response map: {artist} -> {works_map}")
|
||||
print(f"add artist to response map: {artist} -> {works_map}")
|
||||
else:
|
||||
works_map[worktype_id] = ComicWorktypeArtistResponse(
|
||||
worktype=worktype_to_response(work.work_type),
|
||||
artists=[artist_to_response(work.artist)]
|
||||
)
|
||||
for value in works_map.values():
|
||||
works.append(value)
|
||||
response = ComicDetailsResponse(
|
||||
id=str(comic.id),
|
||||
created=str(comic.created_date),
|
||||
title=str(comic.title),
|
||||
completed=bool(comic.completed),
|
||||
current_order=bool(comic.current_order),
|
||||
weblink=str(comic.weblink),
|
||||
publisher=publisher_to_response(comic.publisher),
|
||||
issues=issues,
|
||||
volumes=volumes,
|
||||
works=works
|
||||
)
|
||||
return response
|
||||
@@ -1,24 +0,0 @@
|
||||
from typing import List
|
||||
|
||||
from src.db.models.comic import Publisher
|
||||
from src.schema.comics.comic import ComicResponse
|
||||
from src.schema.comics.publisher import PublisherResponse
|
||||
from src.schema.comics.publisher_details import PublisherDetailsResponse
|
||||
|
||||
|
||||
def get_publisher_details(publisher: Publisher) -> PublisherDetailsResponse:
|
||||
imprints: List[PublisherResponse] = []
|
||||
for imprint in publisher.imprints:
|
||||
imprints.append(PublisherResponse(id=imprint.id, name=str(imprint.name)))
|
||||
comics: List[ComicResponse] = []
|
||||
for comic in publisher.comics:
|
||||
comics.append(
|
||||
ComicResponse(id=comic.id, title=comic.title, completed=comic.completed)
|
||||
)
|
||||
parent_publisher: PublisherResponse | None = None
|
||||
if publisher.parent_publisher:
|
||||
parent_publisher = PublisherResponse(id=publisher.parent_publisher.id, name=str(publisher.parent_publisher.name))
|
||||
response: PublisherDetailsResponse = PublisherDetailsResponse(
|
||||
id=publisher.id, name=str(publisher.name), parent_publisher=parent_publisher, imprints=imprints, comics=comics
|
||||
)
|
||||
return response
|
||||
@@ -1,33 +0,0 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import AnyStr, Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.core.log_conf import logger
|
||||
from src.db.models.comic import WorkType
|
||||
from src.schema.comics.worktype import AddWorkType
|
||||
|
||||
|
||||
def create_new_worktype(work: AddWorkType, db: Session) -> WorkType:
|
||||
worktype = WorkType()
|
||||
worktype.id = str(uuid.uuid4())
|
||||
worktype.created_date = datetime.now()
|
||||
worktype.last_modified_date = datetime.now()
|
||||
worktype.name = work.worktype
|
||||
db.add(worktype)
|
||||
db.commit()
|
||||
db.refresh(worktype)
|
||||
logger.info(f"create_new_worktype: {worktype}")
|
||||
return worktype
|
||||
|
||||
|
||||
def update_worktype(work: AddWorkType, worktype_id: AnyStr, db: Session) -> Optional[WorkType]:
|
||||
logger.info("update worktype")
|
||||
worktype: Optional[WorkType] = db.get(WorkType, worktype_id)
|
||||
if worktype is not None:
|
||||
worktype.name = work.worktype
|
||||
db.add(worktype)
|
||||
db.commit()
|
||||
db.refresh(worktype)
|
||||
return worktype
|
||||
@@ -1,66 +0,0 @@
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.core.log_conf import logger
|
||||
from src.db.models.media import MediaActor
|
||||
from src.db.repository.media.actorfile import delete_mediaactorfile
|
||||
from src.schema.media.actor import MediaActorModel
|
||||
|
||||
|
||||
def create_new_mediaactor(new_actor: MediaActorModel, db: Session) -> MediaActor:
|
||||
logger.info(f"create MediaActor with url {new_actor.url}")
|
||||
media_actor: MediaActor = MediaActor()
|
||||
media_actor.id = str(uuid.uuid4())
|
||||
if new_actor.name is not None:
|
||||
media_actor.name = new_actor.name
|
||||
media_actor.url = new_actor.url
|
||||
media_actor.created_date = datetime.now()
|
||||
media_actor.last_modified_date = datetime.now()
|
||||
media_actor.version = 0
|
||||
db.add(media_actor)
|
||||
db.commit()
|
||||
db.refresh(media_actor)
|
||||
logger.info(f"created {media_actor}")
|
||||
return media_actor
|
||||
|
||||
def delete_mediaactor(db: Session, actor_id: str):
|
||||
logger.info(f"delete MediaActor with id {actor_id}")
|
||||
media_actor = db.get(MediaActor, actor_id)
|
||||
if media_actor is not None:
|
||||
actor_files = media_actor.media_actor_files
|
||||
for actor_file in actor_files:
|
||||
delete_mediaactorfile(db, actorfile_id=actor_file.id)
|
||||
db.refresh(media_actor)
|
||||
db.delete(media_actor)
|
||||
db.commit()
|
||||
|
||||
def import_mediaactor(db: Session, new_actor: MediaActorModel) -> MediaActor:
|
||||
"""
|
||||
import MediaFile and set missing values with default ones.
|
||||
"""
|
||||
logger.info("import MediaActor with %s", new_actor)
|
||||
media_actor: MediaActor = MediaActor()
|
||||
media_actor.id = new_actor.id
|
||||
if new_actor.created_date:
|
||||
media_actor.created_date = new_actor.created_date
|
||||
else:
|
||||
media_actor.created_date = datetime.now()
|
||||
if new_actor.last_modified_date:
|
||||
media_actor.last_modified_date = new_actor.last_modified_date
|
||||
else:
|
||||
media_actor.last_modified_date = datetime.now()
|
||||
media_actor.version = new_actor.version
|
||||
if new_actor.name:
|
||||
media_actor.name = new_actor.name
|
||||
else:
|
||||
media_actor.name = ""
|
||||
if new_actor.url:
|
||||
media_actor.url = new_actor.url
|
||||
else:
|
||||
media_actor.url = ""
|
||||
db.add(media_actor)
|
||||
db.commit()
|
||||
db.refresh(media_actor)
|
||||
return media_actor
|
||||
@@ -1,64 +0,0 @@
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.core.log_conf import logger
|
||||
from src.db.models.media import MediaActorFile
|
||||
from src.schema.media.actorfile import MediaActorFileModel
|
||||
|
||||
|
||||
def create_new_mediaactorfile(
|
||||
db: Session, actor_id: str, file_id: str
|
||||
) -> MediaActorFile:
|
||||
"""
|
||||
Create relation for MediaFile and MediaActor
|
||||
"""
|
||||
logger.info("create MediaActorFile with actor %s and file %s", actor_id, file_id)
|
||||
media_actor_file: MediaActorFile = MediaActorFile()
|
||||
media_actor_file.id = str(uuid.uuid4())
|
||||
media_actor_file.created_date = datetime.now()
|
||||
media_actor_file.last_modified_date = datetime.now()
|
||||
media_actor_file.version = 0
|
||||
media_actor_file.media_actor_id = actor_id
|
||||
media_actor_file.media_file_id = file_id
|
||||
db.add(media_actor_file)
|
||||
db.commit()
|
||||
db.refresh(media_actor_file)
|
||||
return media_actor_file
|
||||
|
||||
|
||||
def delete_mediaactorfile(db: Session, actorfile_id: str):
|
||||
"""
|
||||
Delete relation between MediaFile and MediaActor.
|
||||
"""
|
||||
logger.info("delete MediaActorFile with id %s", actorfile_id)
|
||||
media_actorfile = db.get(MediaActorFile, actorfile_id)
|
||||
db.delete(media_actorfile)
|
||||
db.commit()
|
||||
|
||||
|
||||
def import_mediaactorfile(
|
||||
db: Session, new_actorfile: MediaActorFileModel
|
||||
) -> MediaActorFile:
|
||||
"""
|
||||
Import MediaFile and set missing values with default ones.
|
||||
"""
|
||||
logger.info("import MediaActorFile with %s", new_actorfile)
|
||||
media_actor_file: MediaActorFile = MediaActorFile()
|
||||
media_actor_file.id = new_actorfile.id
|
||||
if new_actorfile.created_date:
|
||||
media_actor_file.created_date = new_actorfile.created_date
|
||||
else:
|
||||
media_actor_file.created_date = datetime.now()
|
||||
if new_actorfile.last_modified_date:
|
||||
media_actor_file.last_modified_date = new_actorfile.last_modified_date
|
||||
else:
|
||||
media_actor_file.last_modified_date = datetime.now()
|
||||
media_actor_file.version = new_actorfile.version
|
||||
media_actor_file.media_actor_id = new_actorfile.media_actor_id
|
||||
media_actor_file.media_file_id = new_actorfile.media_file_id
|
||||
db.add(media_actor_file)
|
||||
db.commit()
|
||||
db.refresh(media_actor_file)
|
||||
return media_actor_file
|
||||
@@ -1,78 +0,0 @@
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.core.log_conf import logger
|
||||
from src.db.models.media import MediaFile
|
||||
from src.schema.media.file import MediaFileModel
|
||||
|
||||
|
||||
def create_new_mediafile(link: str, db: Session) -> MediaFile:
|
||||
"""
|
||||
Create MediaFile with gievne URL.
|
||||
"""
|
||||
logger.info("create MediaFile with url {link}")
|
||||
media_file: MediaFile = MediaFile()
|
||||
media_file.id = str(uuid.uuid4())
|
||||
media_file.url = link
|
||||
media_file.created_date = datetime.now()
|
||||
media_file.last_modified_date = datetime.now()
|
||||
media_file.version = 0
|
||||
media_file.review = True
|
||||
media_file.should_download = True
|
||||
db.add(media_file)
|
||||
db.commit()
|
||||
db.refresh(media_file)
|
||||
logger.info("created %s", media_file)
|
||||
return media_file
|
||||
|
||||
|
||||
def delete_mediafile(db: Session, media_file_id: str):
|
||||
"""
|
||||
Delete MediaFile with given ID from db.
|
||||
"""
|
||||
logger.info("delete MediaFile with id %s", media_file_id)
|
||||
media_file = db.get(MediaFile, media_file_id)
|
||||
db.delete(media_file)
|
||||
db.commit()
|
||||
|
||||
|
||||
def import_mediafile(db: Session, new_file: MediaFileModel) -> MediaFile:
|
||||
"""
|
||||
import MediaActor and set missing values with defautl ones.
|
||||
"""
|
||||
logger.info("import MediaFile with %s", new_file)
|
||||
media_file: MediaFile = MediaFile()
|
||||
media_file.id = new_file.id
|
||||
if new_file.created_date:
|
||||
media_file.created_date = new_file.created_date
|
||||
else:
|
||||
media_file.created_date = datetime.now()
|
||||
if new_file.last_modified_date:
|
||||
media_file.last_modified_date = new_file.last_modified_date
|
||||
else:
|
||||
media_file.last_modified_date = datetime.now()
|
||||
media_file.version = new_file.version
|
||||
if new_file.title:
|
||||
media_file.title = new_file.title
|
||||
else:
|
||||
media_file.title = ""
|
||||
if new_file.file_name:
|
||||
media_file.file_name = new_file.file_name
|
||||
else:
|
||||
media_file.file_name = ""
|
||||
if new_file.cloud_link:
|
||||
media_file.cloud_link = new_file.cloud_link
|
||||
else:
|
||||
media_file.cloud_link = ""
|
||||
if new_file.url:
|
||||
media_file.url = new_file.url
|
||||
else:
|
||||
media_file.url = ""
|
||||
media_file.review = new_file.review
|
||||
media_file.should_download = new_file.should_download
|
||||
db.add(media_file)
|
||||
db.commit()
|
||||
db.refresh(media_file)
|
||||
return media_file
|
||||
@@ -1,23 +0,0 @@
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.db.models.media import MediaVideo
|
||||
from src.webapps.media.forms import AddLinkForm
|
||||
|
||||
|
||||
def create_new_video(video: AddLinkForm, db: Session) -> MediaVideo:
|
||||
print(video.url)
|
||||
media_video = MediaVideo()
|
||||
media_video.id = str(uuid.uuid4())
|
||||
media_video.url = str(video.url)
|
||||
media_video.created_date = datetime.now()
|
||||
media_video.last_modified_date = datetime.now()
|
||||
media_video.review = True
|
||||
media_video.should_download = True
|
||||
db.add(media_video)
|
||||
db.commit()
|
||||
db.refresh(media_video)
|
||||
print(media_video)
|
||||
return media_video
|
||||
@@ -2,10 +2,10 @@ from datetime import datetime
|
||||
from typing import Optional
|
||||
import uuid
|
||||
from src.core.log_conf import logger
|
||||
from src.db.models.admin import Assignment, Profile
|
||||
from kontor_model.db.models.admin import Assignment, Profile
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.schema.user.profile import ProfileModel
|
||||
from kontor_model.schema.user.profile import ProfileModel
|
||||
|
||||
|
||||
def create_new_profile(new_profile: ProfileModel, db: Session) -> Profile:
|
||||
|
||||
Reference in New Issue
Block a user