class Album(db.Model): id = db.Column(db.Integer, primary_key=True) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) title = db.Column(db.String) author = db.relationship('User', back_populates='albums') category = db.relationship('Category', back_populates='albums') entries = db.relationship('AlbumEntry', back_populates='album', order_by='AlbumEntry.order') @property def first_entry(self) -> Optional['AlbumEntry']: if len(self.entries) != 0: return self.entries[0] return None @property def first_entry_url(self) -> Optional[str]: entry = self.first_entry if entry is not None: return entry.image_url return None def __repr__(self): return '<Album: %r>' % self.title
class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True, nullable=False) albums = db.relationship('Album', back_populates='category') stories = db.relationship('Story', back_populates='category') def __repr__(self): return '<Category: %r>' % self.name
class UserModel(db.Model, UserMixin): __tablename__ = 'user_model' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) books = db.relationship('Book', backref='user_book', lazy=True) def __init__(self, username, email, password): self.username = username self.email = email self.password = password def __repr__(self): return f"<User('{self.username}', '{self.email}')>"
class User(db.Model): id = db.Column(db.Integer, primary_key=True) nickname = db.Column(db.String, unique=True, nullable=False) avatar = entity.image_attachment('UserAvatar') albums = db.relationship('Album', back_populates='author') stories = db.relationship('Story', back_populates='author') @property def avatar_url(self) -> Optional[str]: with store_context(image_store): try: return self.avatar.locate() except IOError: pass return None def __repr__(self): return '<User: %r>' % self.nickname
class AlbumEntry(db.Model): id = db.Column(db.Integer, primary_key=True) album_id = db.Column(db.Integer, db.ForeignKey('album.id'), nullable=False) order = db.Column(db.Integer, nullable=False) caption = db.Column(db.String) album = db.relationship('Album', back_populates='entries') image = entity.image_attachment('AlbumImage') @property def image_url(self) -> Optional[str]: with store_context(image_store): try: return self.image.locate() except IOError: pass return None def __repr__(self): return '<AlbumEntry: %r>' % self.caption
class Book(db.Model): __tablename__ = 'book' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(120), nullable=False) author = db.Column(db.String(120), nullable=False) #image_file = db.Column(db.String(20), nullable=False, default='default.jpg') num_pages = db.Column(db.Integer, nullable=False) date_started = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) # date_finished = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) complete = db.Column(db.Boolean) user_id = db.Column(db.Integer, db.ForeignKey('user_model.id'), nullable=False) def __repr__(self): return f"<BookID: '{self.id}'>"
class Story(db.Model): id = db.Column(db.Integer, primary_key=True) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) title = db.Column(db.String) flavour_text = db.Column(db.String) fulltext_html = db.Column(db.String) cover = entity.image_attachment('StoryCover') author = db.relationship('User', back_populates='stories') category = db.relationship('Category', back_populates='stories') @property def cover_url(self) -> Optional[str]: with store_context(image_store): try: return self.cover.locate() except IOError: pass return None def __repr__(self): return '<Story: %r>' % self.title
class StoryCover(db.Model, entity.Image): __tablename__ = 'story_cover' story_id = db.Column(db.Integer, db.ForeignKey('story.id'), primary_key=True, unique=True) def __repr__(self): return '<StoryCover: %r>' % self.story_id
class AlbumImage(db.Model, entity.Image): __tablename__ = 'album_image' entry_id = db.Column(db.Integer, db.ForeignKey('album_entry.id'), primary_key=True, unique=True) def __repr__(self): return '<AlbumImage: %r>' % self.entry_id
class UserAvatar(db.Model, entity.Image): __tablename__ = 'user_avatar' user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True, unique=True) def __repr__(self): return '<UserAvatar: %r>' % self.user_id