class Copy(db.Model): id = db.Column(db.Integer, primary_key=True) asset_code = db.Column(db.String(8), unique=True) library_item_id = db.Column(db.Integer, db.ForeignKey('library_item.id'), nullable=False) library_item = db.relationship('LibraryItem', foreign_keys=library_item_id, uselist=False, backref=db.backref( 'copies', lazy='select', cascade='all, delete-orphan')) shelf = db.Column(db.String(56)) has_cd_disk = db.Column(db.Boolean) available_status = db.Column(ChoiceType(BookStatus, impl=db.Integer()), server_default='3', default=BookStatus.RETURNED) rental_logs = db.relationship('RentalLog', lazy='dynamic', cascade='all, delete-orphan', backref=db.backref('copy', uselist=False)) def __str__(self): return "Copy asset_code: {}, type/title: {}/{}".format( self.asset_code, self.library_item.type, self.library_item.title) def __repr__(self): return "<Copy: {} library_item_id={}>".format(self.asset_code, self.library_item_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) type = db.Column(db.String(32)) __mapper_args__ = { 'polymorphic_identity': 'library_item', 'polymorphic_on': type } @property def tags_string(self): if self.tags: return ', '.join(t.name for t in self.tags) else: return '-'
class WishListItem(db.Model): __tablename__ = 'wish_list_items' id = db.Column(db.Integer, primary_key=True) authors = db.Column(db.String(256)) title = db.Column(db.String(256), nullable=False) likes = db.relationship('Like', backref='wish_list_item', cascade="all, delete-orphan", lazy=True) pub_year = db.Column(db.Date) item_type = db.Column(db.String(256), nullable=False) def __repr__(self): return '<Wish List Item {}>'.format(self.title) def __lt__(self, other): return (self.likes_count < other.likes_count) def __le__(self, other): return (self.likes_count <= other.likes_count) def __gt__(self, other): return (self.likes_count > other.likes_count) def __ge__(self, other): return (self.likes_count >= other.likes_count) def __eq__(self, other): return (self.likes_count == other.likes_count) def __ne__(self, other): return not (self.__eq__) @classmethod def delete_wish(cls, wish_id): delete_wish_admin = WishListItem.query.get(wish_id) db.session.delete(delete_wish_admin) db.session.commit() @hybrid_property def likes_count(self): return self.likes.count() @likes_count.expression def likes_count(cls): return (select([func.count( Like.id)]).where(Like.wish_item_id == cls.id).label("likes_count")) def serialize(self): return { 'id': self.id, 'item_type': self.item_type, 'title': self.title, 'authors': self.authors, 'pub_year': self.pub_year, 'likes': self.likes, }
class Department(db.Model): __tablename__ = 'department' id_dep = db.Column(db.Integer, primary_key=True) dep_name = db.Column(db.String(30)) description = db.Column(db.String(100)) employees = db.relationship('Employee', backref='department', lazy='dynamic') def __init__(self, dep_name, description): self.dep_name = dep_name self.description = description
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)
class Employee(db.Model): __tablename__ = 'employee' id_emp = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(30)) last_name = db.Column(db.String(30)) birth_date = db.Column(db.Date()) salary = db.Column(db.Integer()) department_id = db.Column(db.Integer, db.ForeignKey("department.id_dep")) def __init__(self, first_name, last_name, birth_date, salary, department_id): self.first_name = first_name self.last_name = last_name self.birth_date = birth_date self.salary = salary self.department_id = department_id
class TaskList(db.Model): __tablename__ = "task-list" id = db.Column(db.Integer, primary_key=True) list_title = db.Column(db.String(256), unique=True, nullable=False) newly_created = db.Column(db.Boolean, default=True) list_accomplished = db.Column(db.Boolean) task_cards = db.relationship('TaskCard')
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) employee_id = db.Column(db.String(64), nullable=False) active = db.Column(db.Boolean) roles = db.relationship( "Role", secondary=user_roles, lazy="select", backref=db.backref("users", lazy="select"), ) rental_logs = db.relationship( "RentalLog", backref=db.backref("user", uselist=False, single_parent=True, lazy="joined"), lazy="dynamic", cascade="all, delete-orphan", ) @property def full_name(self): return "{} {}".format(self.first_name, self.surname) def __init__(self, **kwargs): super(User, self).__init__(**kwargs) role = Role.query.filter_by(name=RoleEnum.USER).first() self.roles.append(role) def __repr__(self): return "<User: {} {} role={}>".format(self.first_name, self.surname, self.roles) def has_role(self, role): if type(role) is str: try: role = RoleEnum[role.upper()] except ValueError: raise ValueError("No role with that name.") if type(role) is not RoleEnum: raise ValueError("No such role.") return role in [r.name for r in self.roles]
class TaskDetails(db.Model): __tablename__ = "details" id = db.Column(db.Integer, primary_key=True) task_description = db.Column(db.String(512)) task_card_id = db.Column(db.Integer, db.ForeignKey('task-card.id'), nullable=False) task_card = db.relationship('TaskCard', back_populates='task_details')
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)
class TaskCard(db.Model): __tablename__ = "task-card" id = db.Column(db.Integer, primary_key=True) card_title = db.Column(db.String(256)) accomplished = db.Column(db.Boolean) task_list_id = db.Column(db.Integer, db.ForeignKey('task-list.id'), nullable=False) task_list = db.relationship('TaskList', back_populates='task_cards') task_details = db.relationship('TaskDetails')
class WishListItem(db.Model): __tablename__ = 'wish_list_items' id = db.Column(db.Integer, primary_key=True) authors = db.Column(db.String(256)) title = db.Column(db.String(256), nullable=False) likes = db.relationship('Like', backref='wish_list_item', cascade="all, delete-orphan", lazy=True) pub_year = db.Column(db.Date) item_type = db.Column(db.String(256), nullable=False) def __repr__(self): return '<Wish List Item {}>'.format(self.title) @classmethod def delete_wish(cls, wish_id): delete_wish_admin = WishListItem.query.get(wish_id) db.session.delete(delete_wish_admin) db.session.commit()
class Book(LibraryItem): __tablename__ = "books" id = db.Column(db.ForeignKey("library_item.id"), primary_key=True) isbn = db.Column(db.String(128), unique=True) authors = db.relationship( "Author", secondary="books_authors", lazy="joined", backref=db.backref("books"), ) original_title = db.Column(db.String(256)) publisher = db.Column(db.String(256)) pub_date = db.Column(db.Date) __mapper_args__ = {"polymorphic_identity": "book"} def columns(self): """Return the actual columns of a SQLAlchemy-mapped object""" return [ prop.key for prop in class_mapper(self.__class__).iterate_properties if isinstance(prop, ColumnProperty) ] def __init__(self, **kwargs): super(Book, self).__init__(**kwargs) def __str__(self): return "'{}' by {}".format(self.title, ", ".join([str(a) for a in self.authors])) def __repr__(self): return "<Book: '{}' tags={} authors={} copies={}>".format( self.title, self.tags, self.authors, self.copies) @property def authors_string(self): if self.authors: return ", ".join(a.full_name for a in self.authors) else: return "-"
class Recipe(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(300)) title = db.Column(db.String(100), nullable=False) total_time_minutes = db.Column(db.Integer) yields = db.Column(db.String(50)) ingredients = db.Column(db.String(2000), nullable=False) instructions = db.Column(db.String(6000), nullable=False) img_url = db.Column(db.String(300)) desc = db.Column(db.String(300)) def __repr__(self): return "<Recipe ID %r>" % self.id