class WorkItem(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), nullable=False) description = db.Column(db.String(120)) estimated_time = db.Column(db.Integer, default=0) is_done = db.Column(db.Boolean, default=0) created_by = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) shared_with = db.relationship('User', secondary=shared_with, lazy='subquery', backref=db.backref('user', lazy=True))
class OrderProduct(db.Model): __tablename__ = 'Order_Product' order_id = db.Column(db.Integer, db.ForeignKey('Order.order_id'), primary_key=True) item_id = db.Column(db.Integer, db.ForeignKey('InventoryItem.item_id'), primary_key=True) item_qty = db.Column(db.Integer) def __init__(self, order_id, item_id, qty): self.order_id = order_id self.item_id = item_id self.item_qty = qty def serialize(self): return { 'order_id': self.order_id, 'item_id': self.item_id, 'item_qty': self.item_qty }
class Blog(Bookmark): __tablename__ = 'blog' def __init__(self, **kwargs): super(Blog, self).__init__(**kwargs) self.type = Bookmark.TYPE_BLOG id = db.Column(db.Integer, db.ForeignKey('bookmark.id'), primary_key=True) URL = db.Column(db.String(250)) writer = db.Column(db.String(250)) __mapper_args__ = { 'polymorphic_identity': 'blog', }
class Podcast(Bookmark): __tablename__ = 'podcast' def __init__(self, **kwargs): super(Podcast, self).__init__(**kwargs) self.type = Bookmark.TYPE_PODCAST id = db.Column(db.Integer, db.ForeignKey('bookmark.id'), primary_key=True) name = db.Column(db.String(250)) description = db.Column(db.String(1024)) author = db.Column(db.String(250)) __mapper_args__ = { 'polymorphic_identity': 'podcast', }
class Video(Bookmark): __tablename__ = 'video' def __init__(self, **kwargs): super(Video, self).__init__(**kwargs) self.type = Bookmark.TYPE_VIDEO bookmark_id = db.Column(db.Integer, db.ForeignKey('bookmark.id', ondelete='CASCADE'), primary_key=True) URL = db.Column(db.String(250)) timestamp = db.Column(db.Integer(), default=0) bookmark = db.relationship('Bookmark', foreign_keys="Video.bookmark_id", backref=db.backref('video', lazy='dynamic')) __mapper_args__ = { 'polymorphic_identity': 'video', }
class Book(Bookmark): __tablename__ = 'book' def __init__(self, **kwargs): super(Book, self).__init__(**kwargs) self.type = Bookmark.TYPE_BOOK bookmark_id = db.Column(db.Integer, db.ForeignKey('bookmark.id', ondelete='CASCADE'), primary_key=True) ISBN = db.Column(db.String(17), unique=True) writer = db.Column(db.String(250)) bookmark = db.relationship('Bookmark', foreign_keys="Book.bookmark_id", backref=db.backref('books', lazy='dynamic')) image = db.Column(db.String(500)) __mapper_args__ = { 'polymorphic_identity': 'book', }
class UserBooksModel(db.Model): '''class representing users table''' __tablename__ = 'user_books' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(30), db.ForeignKey('users.username'), nullable=False) book_id = db.Column(db.Integer, nullable=False) date_borrowed = db.Column(db.DateTime) date_returned = db.Column(db.DateTime) return_status = db.Column(db.Boolean) def __init__(self, username, book_id): self.username = username self.book_id = book_id @classmethod def date_format(cls, my_date): '''method to return date format dd-mm-yyyy''' return str(my_date.day) + '-' + str(my_date.month) + '-' + str( my_date.year) @classmethod def find_user_book(cls, username, book_id): '''filter by username and book''' #book = cls.query.filter_by(username=username, book_id=book_id).first() book = cls.query.filter_by(username=username, book_id=book_id, return_status=False).first() return book def borrow_book(self): '''borrow a book''' self.return_status = False self.date_borrowed = date.today() @classmethod def user_history(cls, username): '''return user book history''' all_user_books = cls.query.filter_by(username=username).all() history = [] for book in all_user_books: date_returned = book.date_returned if date_returned is not None: date_returned = cls.date_format(date_returned) if date_returned is None: date_returned = "None" date_borrowed = cls.date_format(book.date_borrowed) details = dict(book_id=book.book_id, date_borrowed=date_borrowed, date_returned=date_returned, returned=book.return_status) history.append(details) return history @classmethod def books_not_returned(cls, username): '''return all user books not returned''' unreturned_books = cls.query.filter_by(username=username).all() unreturned_list = [] for book in unreturned_books: if book.return_status is False: date_borrowed = cls.date_format(book.date_borrowed) details = dict(book_id=book.book_id, date_borrowed=date_borrowed, date_returned="None", returned=book.return_status) unreturned_list.append(details) return {username: unreturned_list} def save(self): '''commit and save data from object''' db.session.add(self) db.session.commit() def __repr__(self): return '<name: %s, username: %s, email: %s>' % ( self.name, self.username, self.email)
projects = relationship("Projects", secondary="user_project", backref='users') def __repr__(self): return (u'<{self.__class__.__name__}: {self.id}>'.format(self=self)) @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) user_project = db.Table( 'user_project', db.Column('project_id', db.Integer, db.ForeignKey('projects.id'), primary_key=True), db.Column('user_id', db.Integer, db.ForeignKey('users.id'), primary_key=True))
class SubTask(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), nullable=False) description = db.Column(db.String(120)) work_item_id = db.Column(db.Integer, db.ForeignKey('work_item.id'), nullable=False)
from application.app import app, db from werkzeug.security import generate_password_hash, check_password_hash import logging class User(db.Model): id = db.Column(db.Integer, primary_key=True) # email = db.Column(db.String(80), unique=True, nullable=False) username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(120)) shared_with = db.Table('shared_with', db.Column('work_item_id', db.Integer, db.ForeignKey('work_item.id'), primary_key=True), db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True) ) class WorkItem(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80), nullable=False) description = db.Column(db.String(120)) estimated_time = db.Column(db.Integer, default=0) is_done = db.Column(db.Boolean, default=0) created_by = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) shared_with = db.relationship('User', secondary=shared_with, lazy='subquery', backref=db.backref('user', lazy=True)) # user - WorkItem # 1 1 # 2 1 # 3 2 # 3 1