from the_works.database import db from sqlalchemy.orm import relationship from sqlalchemy.ext.associationproxy import association_proxy from flask import url_for import sys # add method to sqlalchemy.orm.decl_api.Model def _asdict(self): d = {} for col in self.__table__.c: if type(col.type) == db.types.BLOB: d[col.key] = "Blob (NULL)" if self.__getattribute__(col.key) == None else f"Blob ({sys.getsizeof(self.__getattribute__(col.key))} Bytes)" else: value = str(self.__getattribute__(col.key)) d[col.key] = value[:50] + '...' if len(value) > 50 else value return d db.Model._asdict = _asdict # override repr() method from sqlalchemy.orm.decl_api.Model def __repr__(self): return f"{type(self).__name__}({str(self._asdict())})" db.Model.__repr__ = __repr__ class Text(db.Model): __table__ = db.Model.metadata.tables['Text'] reihe = relationship("Reihe", back_populates="text") textform = relationship("Textform", back_populates="text") sprache = relationship("Sprache", back_populates="text") veroeffentlichung = relationship("Veroeffentlichung", back_populates="text") text_genre = relationship("Text_Genre", back_populates="text", cascade="save-update, merge, delete, delete-orphan") genres = association_proxy("text_genre", "Genre") class Werk(db.Model): __table__ = db.Model.metadata.tables['Werk'] reihe = relationship("Reihe", back_populates="werk") verlag = relationship("Verlag", back_populates="werk") werksform = relationship("Werksform", back_populates="werk") veroeffentlichung = relationship("Veroeffentlichung", back_populates="werk") werk_genre = relationship("Werk_Genre", back_populates="werk", cascade="save-update, merge, delete, delete-orphan") # titelbild = relationship("Titelbild", back_populates="werk", cascade="all, delete-orphan", single_parent=True) titelbild = relationship("Titelbild", back_populates="werk") genres = association_proxy("werk_genre", "Genre") werk_herausgeber = relationship("Werk_Herausgeber", back_populates="werk", cascade="save-update, merge, delete, delete-orphan") herausgeber = association_proxy("werk_herausgeber", "Herausgeber") class Veroeffentlichung(db.Model): __table__ = db.Model.metadata.tables['Veroeffentlichung'] text = relationship("Text", back_populates="veroeffentlichung") werk = relationship("Werk", back_populates="veroeffentlichung") pseudonym = relationship("Pseudonym", back_populates="veroeffentlichung") class Reihe(db.Model): __table__ = db.Model.metadata.tables['Reihe'] text = relationship("Text", back_populates="reihe") werk = relationship("Werk", back_populates="reihe") verlag = relationship("Verlag", back_populates="reihe") class Verlag(db.Model): __table__ = db.Model.metadata.tables['Verlag'] werk = relationship("Werk", back_populates="verlag") reihe = relationship("Reihe", back_populates="verlag") class Sprache(db.Model): __table__ = db.Model.metadata.tables['Sprache'] text = relationship("Text", back_populates="sprache") class Textform(db.Model): __table__ = db.Model.metadata.tables['Textform'] text = relationship("Text", back_populates="textform") class Werksform(db.Model): __table__ = db.Model.metadata.tables['Werksform'] werk = relationship("Werk", back_populates="werksform") class Genre(db.Model): __table__ = db.Model.metadata.tables['Genre'] text_genre = relationship("Text_Genre", back_populates="genre") werk_genre = relationship("Werk_Genre", back_populates="genre") class Pseudonym(db.Model): __table__ = db.Model.metadata.tables['Pseudonym'] veroeffentlichung = relationship("Veroeffentlichung", back_populates="pseudonym") class Herausgeber(db.Model): __table__ = db.Model.metadata.tables['Herausgeber'] werk_herausgeber = relationship("Werk_Herausgeber", back_populates="herausgeber") class Titelbild(db.Model): __table__ = db.Model.metadata.tables['Titelbild'] werk = relationship("Werk", back_populates="titelbild") def _asdict_with_urls(self): tb = self._asdict() tb["Bild"] = url_for("titelbild.image", id=self.ID) tb["Thumbnail"] = url_for("titelbild.thumbnail", id=self.ID) return tb class Text_Genre(db.Model): __table__ = db.Model.metadata.tables['Text_Genre'] text = relationship("Text", back_populates="text_genre") genre = relationship("Genre", back_populates="text_genre") def __init__(self, genre: int): self.Genre = genre class Werk_Genre(db.Model): __table__ = db.Model.metadata.tables['Werk_Genre'] werk = relationship("Werk", back_populates="werk_genre") genre = relationship("Genre", back_populates="werk_genre") def __init__(self, genre: int): self.Genre = genre class Werk_Herausgeber(db.Model): __table__ = db.Model.metadata.tables['Werk_Herausgeber'] werk = relationship("Werk", back_populates="werk_herausgeber") herausgeber = relationship("Herausgeber", back_populates="werk_herausgeber") def __init__(self, hrsg: int): self.Herausgeber = hrsg