models now validate that core fields are non-empty

This commit is contained in:
eclipse 2025-07-24 10:35:04 +02:00
parent 5058ba133a
commit 0522c5660e

View File

@ -1,9 +1,7 @@
# code is built upon output from sqlacodegen
import sys import sys
from typing import List, Optional from typing import List, Optional
from sqlalchemy import ForeignKey, types 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 sqlalchemy.ext.associationproxy import AssociationProxy, association_proxy
from flask import url_for from flask import url_for
@ -22,7 +20,12 @@ class Base(DeclarativeBase):
return d return d
def __repr__(self) -> str: 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): class Genre(Base):
@ -34,6 +37,9 @@ class Genre(Base):
texte: Mapped[List['Text_Genre']] = relationship(back_populates='genre') texte: Mapped[List['Text_Genre']] = relationship(back_populates='genre')
werke: Mapped[List['Werk_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): class Herausgeber(Base):
__tablename__ = 'Herausgeber' __tablename__ = 'Herausgeber'
@ -43,6 +49,10 @@ class Herausgeber(Base):
werke: Mapped[List['Werk_Herausgeber']] = relationship(back_populates='herausgeber') 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): class Pseudonym(Base):
__tablename__ = 'Pseudonym' __tablename__ = 'Pseudonym'
@ -52,6 +62,10 @@ class Pseudonym(Base):
veroeffentlichung: Mapped[List['Veroeffentlichung']] = relationship(back_populates='pseudonym') 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): class Sprache(Base):
__tablename__ = 'Sprache' __tablename__ = 'Sprache'
@ -61,6 +75,10 @@ class Sprache(Base):
text: Mapped[List['Text']] = relationship(back_populates='sprache') 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): class Textform(Base):
__tablename__ = 'Textform' __tablename__ = 'Textform'
@ -70,6 +88,10 @@ class Textform(Base):
text: Mapped[List['Text']] = relationship(back_populates='textform') 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): class Titelbild(Base):
__tablename__ = 'Titelbild' __tablename__ = 'Titelbild'
@ -92,6 +114,10 @@ class Titelbild(Base):
tb["Thumbnail"] = url_for("titelbild.thumbnail", id=self.ID) tb["Thumbnail"] = url_for("titelbild.thumbnail", id=self.ID)
return tb return tb
@validates("sha256")
def validate_titelbild(self, key, value):
return self.validate_not_empty(value)
class Verlag(Base): class Verlag(Base):
__tablename__ = 'Verlag' __tablename__ = 'Verlag'
@ -102,6 +128,10 @@ class Verlag(Base):
reihe: Mapped[List['Reihe']] = relationship(back_populates='verlag') reihe: Mapped[List['Reihe']] = relationship(back_populates='verlag')
werk: Mapped[List['Werk']] = 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): class Werksform(Base):
__tablename__ = 'Werksform' __tablename__ = 'Werksform'
@ -111,6 +141,10 @@ class Werksform(Base):
werk: Mapped[List['Werk']] = relationship(back_populates='werksform') 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): class Reihe(Base):
__tablename__ = 'Reihe' __tablename__ = 'Reihe'
@ -124,6 +158,10 @@ class Reihe(Base):
text: Mapped[List['Text']] = relationship(back_populates='reihe') text: Mapped[List['Text']] = relationship(back_populates='reihe')
werk: Mapped[List['Werk']] = 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): class Text(Base):
__tablename__ = 'Text' __tablename__ = 'Text'
@ -148,6 +186,10 @@ class Text(Base):
genres: Mapped[List['Text_Genre']] = relationship(back_populates='text', cascade="all, delete-orphan") 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)) 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): class Werk(Base):
__tablename__ = 'Werk' __tablename__ = 'Werk'
@ -184,6 +226,10 @@ class Werk(Base):
herausgeber: Mapped[List['Werk_Herausgeber']] = relationship(back_populates='werk', cascade="all, delete-orphan") 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)) 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): class Veroeffentlichung(Base):
__tablename__ = 'Veroeffentlichung' __tablename__ = 'Veroeffentlichung'