class Like(db.Model): __tablename__ = 'wish_list_likes' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) wish_item_id = db.Column(db.Integer, db.ForeignKey('wish_list_items.id'), nullable=False) def __repr__(self): return '<Like {}>'.format(self.id, ) @classmethod def like_exists(cls, wish_id, user): return db.session.query(Like.id)\ .filter_by(user_id=user.id, wish_item_id=wish_id)\ .scalar() is not None @classmethod def like(cls, wish_id, user): new_like = Like(user_id=user.id, wish_item_id=wish_id) db.session.add(new_like) db.session.commit() @classmethod def unlike(cls, wish_id, user): unlike = Like.query\ .filter_by(user_id=user.id, wish_item_id=wish_id).first() db.session.delete(unlike) db.session.commit()
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 customer(user): __tablename__ = 'customer' id = db.Column(db.String, db.ForeignKey('user.id'), primary_key=True) project = db.Column('project', db.String) __mapper_args__ = { 'polymorphic_identity': 'customer', }
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 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 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 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 RentalLog(db.Model): id = db.Column(db.Integer, primary_key=True) copy_id = db.Column(db.Integer, db.ForeignKey('copy.id'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) _borrow_time = db.Column(db.DateTime) _return_time = db.Column(db.DateTime) book_status = db.Column(ChoiceType(BookStatus, impl=db.Integer())) _reservation_begin = db.Column(db.DateTime) _reservation_end = db.Column(db.DateTime) @property def borrow_time(self): return self._borrow_time.replace(tzinfo=pytz.utc).\ astimezone(tz=pytz.timezone('Europe/Warsaw')) @borrow_time.setter def borrow_time(self, dt): if dt.tzinfo is None: raise ValueError("borrow_time has to be timezone aware") self._borrow_time = dt.astimezone(tz=pytz.utc) @property def return_time(self): return self._return_time.replace(tzinfo=pytz.utc). \ astimezone(tz=pytz.timezone('Europe/Warsaw')) @return_time.setter def return_time(self, dt): if dt.tzinfo is None: raise ValueError("return_time has to be timezone aware") self._return_time = dt.astimezone(tz=pytz.utc) @property def reservation_begin(self): return self._reservation_begin.replace(tzinfo=pytz.utc). \ astimezone(tz=pytz.timezone('Europe/Warsaw')) @reservation_begin.setter def reservation_begin(self, dt): if dt.tzinfo is None: raise ValueError("reservation_timestamp has to be timezone aware") self._reservation_begin = dt.astimezone(tz=pytz.utc) @property def reservation_end(self): return self._reservation_end.replace(tzinfo=pytz.utc). \ astimezone(tz=pytz.timezone('Europe/Warsaw')) @reservation_end.setter def reservation_end(self, dt): if dt.tzinfo is None: raise ValueError("reservation_timestamp has to be timezone aware") self._reservation_end = dt.astimezone(tz=pytz.utc) def __str__(self): return "RENTAL LOG: user: {} copy: {}".format(self.user.full_name, self.copy.asset_code) def __repr__(self): return "<RentalLog: ID: {} user_id={} copy_id={}>".format( self.id, self.user_id, self.copy_id)
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)
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 admin(user): __tablename__ = 'admin' id = db.Column(db.String, db.ForeignKey('user.id'), primary_key=True) __mapper_args__ = { 'polymorphic_identity': 'admin', }
@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")), )