diff --git a/the_works/models.py b/the_works/models.py index 59cd9a6..1ce5b17 100644 --- a/the_works/models.py +++ b/the_works/models.py @@ -1,9 +1,7 @@ -# code is built upon output from sqlacodegen - import sys from typing import List, Optional from sqlalchemy import ForeignKey, types -from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship +from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship, validates from sqlalchemy.ext.associationproxy import AssociationProxy, association_proxy from flask import url_for @@ -22,7 +20,12 @@ class Base(DeclarativeBase): return d def __repr__(self) -> str: - return f"{type(self).__name__}({str(self.asdict())})" + return f"{type(self).__name__}({str(self.asdict())})" + + def validate_not_empty(self, value): + if not value: + raise ValueError("value can't be empty") + return value class Genre(Base): @@ -34,6 +37,9 @@ class Genre(Base): texte: Mapped[List['Text_Genre']] = relationship(back_populates='genre') werke: Mapped[List['Werk_Genre']] = relationship(back_populates='genre') + @validates("Genre") + def validate_genre(self, key, value): + return self.validate_not_empty(value) class Herausgeber(Base): __tablename__ = 'Herausgeber' @@ -43,6 +49,10 @@ class Herausgeber(Base): werke: Mapped[List['Werk_Herausgeber']] = relationship(back_populates='herausgeber') + @validates("Herausgeber") + def validate_herausgeber(self, key, value): + return self.validate_not_empty(value) + class Pseudonym(Base): __tablename__ = 'Pseudonym' @@ -52,6 +62,10 @@ class Pseudonym(Base): veroeffentlichung: Mapped[List['Veroeffentlichung']] = relationship(back_populates='pseudonym') + @validates("Pseudonym") + def validate_pseudonym(self, key, value): + return self.validate_not_empty(value) + class Sprache(Base): __tablename__ = 'Sprache' @@ -61,6 +75,10 @@ class Sprache(Base): text: Mapped[List['Text']] = relationship(back_populates='sprache') + @validates("Sprache") + def validate_sprache(self, key, value): + return self.validate_not_empty(value) + class Textform(Base): __tablename__ = 'Textform' @@ -70,6 +88,10 @@ class Textform(Base): text: Mapped[List['Text']] = relationship(back_populates='textform') + @validates("Textform") + def validate_textform(self, key, value): + return self.validate_not_empty(value) + class Titelbild(Base): __tablename__ = 'Titelbild' @@ -92,6 +114,10 @@ class Titelbild(Base): tb["Thumbnail"] = url_for("titelbild.thumbnail", id=self.ID) return tb + @validates("sha256") + def validate_titelbild(self, key, value): + return self.validate_not_empty(value) + class Verlag(Base): __tablename__ = 'Verlag' @@ -102,6 +128,10 @@ class Verlag(Base): reihe: Mapped[List['Reihe']] = relationship(back_populates='verlag') werk: Mapped[List['Werk']] = relationship(back_populates='verlag') + @validates("Verlag") + def validate_verlag(self, key, value): + return self.validate_not_empty(value) + class Werksform(Base): __tablename__ = 'Werksform' @@ -111,6 +141,10 @@ class Werksform(Base): werk: Mapped[List['Werk']] = relationship(back_populates='werksform') + @validates("Werksform") + def validate_werksform(self, key, value): + return self.validate_not_empty(value) + class Reihe(Base): __tablename__ = 'Reihe' @@ -124,6 +158,10 @@ class Reihe(Base): text: Mapped[List['Text']] = relationship(back_populates='reihe') werk: Mapped[List['Werk']] = relationship(back_populates='reihe') + @validates("Titel") + def validate_titel(self, key, value): + return self.validate_not_empty(value) + class Text(Base): __tablename__ = 'Text' @@ -148,6 +186,10 @@ class Text(Base): genres: Mapped[List['Text_Genre']] = relationship(back_populates='text', cascade="all, delete-orphan") genre_ids: AssociationProxy[List["Genre"]] = association_proxy("genres", "Genre", creator=lambda genre_id: Text_Genre(Genre=genre_id)) + @validates("Titel") + def validate_titel(self, key, value): + return self.validate_not_empty(value) + class Werk(Base): __tablename__ = 'Werk' @@ -184,6 +226,10 @@ class Werk(Base): herausgeber: Mapped[List['Werk_Herausgeber']] = relationship(back_populates='werk', cascade="all, delete-orphan") herausgeber_ids: AssociationProxy[List['Herausgeber']] = association_proxy("herausgeber", "Herausgeber", creator=lambda hrsg_id: Werk_Herausgeber(Herausgeber=hrsg_id)) + @validates("Titel") + def validate_titel(self, key, value): + return self.validate_not_empty(value) + class Veroeffentlichung(Base): __tablename__ = 'Veroeffentlichung'