From 4cea5541160e32fe449dd2be34de5f42479a9e64 Mon Sep 17 00:00:00 2001 From: Thomas Peetz Date: Tue, 14 Jan 2025 17:53:05 +0100 Subject: [PATCH] add missing schema classes --- python/kontor/database/__init__.py | 11 ++++- python/kontor/database/bookshelf.py | 75 +++++++++++++++++++++++++++++ python/kontor/database/media.py | 28 ++++++++++- 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 python/kontor/database/bookshelf.py diff --git a/python/kontor/database/__init__.py b/python/kontor/database/__init__.py index c7e6cb5..f436cbe 100644 --- a/python/kontor/database/__init__.py +++ b/python/kontor/database/__init__.py @@ -3,10 +3,11 @@ from datetime import datetime from pathlib import Path from .base import Base +from .bookshelf import Article, Book, Author, BookshelfPublisher, ArticleAuthor, BookAuthor from .comic import Comic, Artist, Publisher, Issue, StoryArc, TradePaperback, Volume, ComicWork, WorkType from .metadata import MetaDataTable, MetaDataColumn from .tysc import Card, CardSet, Sport, Team, FieldPosition, Rooster, Player, Vendor -from .media import MediaFile +from .media import MediaFile, MediaArticle, MediaVideo class KontorDB: @@ -35,7 +36,15 @@ class KontorDB: self.registry['volume'] = Volume self.registry['comic_work'] = ComicWork self.registry['worktype'] = WorkType + self.registry['article'] = Article + self.registry['book'] = Book + self.registry['author'] = Author + self.registry['bookshelf_publisher'] = BookshelfPublisher + self.registry['article_author'] = ArticleAuthor + self.registry['book_author'] = BookAuthor self.registry['media_file'] = MediaFile + self.registry['media_article'] = MediaArticle + self.registry['media_video'] = MediaVideo def get_table_names(self) -> list: tables = self.session.query(MetaDataTable).all() diff --git a/python/kontor/database/bookshelf.py b/python/kontor/database/bookshelf.py new file mode 100644 index 0000000..0e09fd0 --- /dev/null +++ b/python/kontor/database/bookshelf.py @@ -0,0 +1,75 @@ +from sqlalchemy import Column, DateTime, ForeignKey, Integer, String +from sqlalchemy.dialects.mysql import BIT +from sqlalchemy.orm import relationship + +from .base import Base + + +class Article(Base): + __tablename__ = 'article' + id = Column(String, primary_key=True) + created_date = Column(DateTime) + last_modified_date = Column(DateTime) + version = Column(Integer) + title = Column(String(length=255), unique=True) + article_authors = relationship("ArticleAuthor") + + +class Author(Base): + __tablename__ = 'author' + id = Column(String, primary_key=True) + created_date = Column(DateTime) + last_modified_date = Column(DateTime) + version = Column(Integer) + first_name = Column(String(255)) + last_name = Column(String(255)) + article_authors = relationship("ArticleAuthor") + book_authors = relationship("BookAuthor") + + +class BookshelfPublisher(Base): + __tablename__ = 'bookshelf_publisher' + id = Column(String, primary_key=True) + created_date = Column(DateTime) + last_modified_date = Column(DateTime) + version = Column(Integer) + name = Column(String(length=255), unique=True) + books = relationship("Book") + + +class Book(Base): + __tablename__ = 'book' + id = Column(String, primary_key=True) + created_date = Column(DateTime) + last_modified_date = Column(DateTime) + version = Column(Integer) + isbn = Column(String(255), unique=True) + title = Column(String(255)) + year = Column(Integer, nullable=False) + publisher_id = Column(String, ForeignKey('bookshelf_publisher.id'), nullable=False) + publisher = relationship('BookshelfPublisher', back_populates="books") + book_authors = relationship("BookAuthor") + + +class ArticleAuthor(Base): + __tablename__ = 'article_author' + id = Column(String, primary_key=True) + created_date = Column(DateTime) + last_modified_date = Column(DateTime) + version = Column(Integer) + article_id = Column(String, ForeignKey('article.id'), nullable=False) + article = relationship('Article', back_populates="article_authors") + author_id = Column(String, ForeignKey('author.id'), nullable=False) + author = relationship('Author', back_populates="article_authors") + + +class BookAuthor(Base): + __tablename__ = 'book_author' + id = Column(String, primary_key=True) + created_date = Column(DateTime) + last_modified_date = Column(DateTime) + version = Column(Integer) + author_id = Column(String, ForeignKey('author.id'), nullable=False) + author = relationship('Author', back_populates="book_authors") + book_id = Column(String, ForeignKey('book.id'), nullable=False) + book = relationship('Book', back_populates="book_authors") diff --git a/python/kontor/database/media.py b/python/kontor/database/media.py index 0129c03..85e6155 100644 --- a/python/kontor/database/media.py +++ b/python/kontor/database/media.py @@ -15,7 +15,7 @@ class MediaFile(Base): path = Column(String(255)) review = Column(BIT(1)) title = Column(String(255)) - url = Column(String(255)) + url = Column(String(255), unique=True) should_download = Column(BIT(1)) def __repr__(self): @@ -23,3 +23,29 @@ class MediaFile(Base): def __str__(self): return f'{self.title}({self.id})' + + +class MediaArticle(Base): + __tablename__ = 'media_article' + id = Column(String, primary_key=True) + created_date = Column(DateTime) + last_modified_date = Column(DateTime) + version = Column(Integer) + review = Column(BIT(1)) + title = Column(String(255)) + url = Column(String(255), unique=True) + + +class MediaVideo(Base): + __tablename__ = 'media_video' + id = Column(String, primary_key=True) + created_date = Column(DateTime) + last_modified_date = Column(DateTime) + version = Column(Integer) + cloud_link = Column(String(255)) + file_name = Column(String(255)) + path = Column(String(255)) + review = Column(BIT(1)) + title = Column(String(255)) + url = Column(String(255), unique=True) + should_download = Column(BIT(1))