import enum from sqlalchemy import event from flask_user import UserMixin from init_db import db user_roles = db.Table( "user_roles", db.Column( "user_id", db.Integer, db.ForeignKey("users.id", ondelete="CASCADE"), primary_key=True, ), db.Column( "role_id", db.Integer, db.ForeignKey("roles.id", ondelete="CASCADE"), primary_key=True, ), ) class User(db.Model, UserMixin): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(128), unique=True, nullable=False) first_name = db.Column(db.String(64), nullable=False) surname = db.Column(db.String(64), nullable=False)
class Tag(db.Model): __tablename__ = 'tags' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) def __repr__(self): return "<Tag: {}>".format(self.name) def __str__(self): return "Tag: {}".format(self.name) item_tags = db.Table( 'item_tags', db.Column('item_id', db.Integer, db.ForeignKey('library_item.id')), db.Column('tag_id', db.Integer, db.ForeignKey('tags.id'))) class LibraryItem(db.Model): __tablename__ = 'library_item' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(256)) table_of_contents = db.Column(db.String(256)) language = db.Column(db.String(56)) category = db.Column(db.String(56)) tags = db.relationship('Tag', secondary=item_tags, lazy='subquery', backref=db.backref('library_items')) description = db.Column(db.Text)
@property def authors_string(self): if self.authors: return ", ".join(a.full_name for a in self.authors) else: return "-" class Author(db.Model): __tablename__ = "authors" id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(64)) last_name = db.Column(db.String(64)) @property def full_name(self): return "{} {}".format(self.first_name, self.last_name) def __str__(self): return "{}".format(self.full_name) def __repr__(self): return "<Author: {} {}>".format(self.first_name, self.last_name) book_author = db.Table( "books_authors", db.Column("author_id", db.Integer, db.ForeignKey("authors.id")), db.Column("book_id", db.Integer, db.ForeignKey("books.id")), )