class InventoryItem(db.Model): __tablename__ = 'InventoryItem' item_id = db.Column('item_id', db.Integer, primary_key=True) name = db.Column(db.String(100)) description = db.Column(db.String(256)) price = db.Column(db.Numeric(precision=9, scale=2)) quantity = db.Column(db.Integer) def __init__(self, name, description, price, quantity): if not name: raise Exception("Name should not be empty") if price < 0 or quantity < 0: raise Exception("Can't be negative") self.name = name self.description = description self.price = price self.quantity = quantity def __repr__(self): return '<item_id {}>'.format(self.item_id) def serialize(self): return { 'item_id': self.item_id, 'name': self.name, 'description': self.description, 'price': str(self.price), 'quantity': self.quantity }
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 Tag(db.Model): id = db.Column(db.Integer, primary_key=True) tag_name = db.Column(db.String(100)) tag_slug = db.Column(db.String(100)) def __init__(self, *args, **kwargs): super(Tag, self).__init__(*args, **kwargs) self.tag_slug = slugify(self.tag_name) def __repr__(self): return f'<Tag id: {self.id}, name: {self.tag_name} >'
class DbUser(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) last_name = db.Column(db.String(100), nullable=False) first_name = db.Column(db.String(100), nullable=False) cv_url = db.Column(db.String(255)) skill = relationship('DbUserSkillAssociation', back_populates='user', cascade='save-update, merge, delete, delete-orphan') def __repr__(self): return f'{self.first_name} {self.last_name}. CV: {self.cv_url}'
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 UserModel(db.Model): '''class representing users table''' __tablename__ = 'users' username = db.Column(db.String(30), primary_key=True) name = db.Column(db.String(50)) email = db.Column(db.String(70), unique=True) password = db.Column(db.String(200)) admin = db.Column(db.Boolean) reset_password = db.Column(db.String(200)) date_registered = db.Column(db.DateTime) last_login = db.Column(db.DateTime) last_change_password = db.Column(db.DateTime) last_reset_password = db.Column(db.DateTime) user_books = db.relationship("UserBooksModel", cascade="all, delete-orphan") def __init__(self, username, name, email, password, admin=False): self.username = username self.name = name self.email = email self.password = generate_password_hash(password) self.admin = admin self.reset_password = False self.date_registered = datetime.now() @classmethod def get_user_by_username(cls, username): '''query user table using username''' return cls.query.filter_by(username=username).first() @classmethod def get_user_by_email(cls, email): '''query user table using email''' return cls.query.filter_by(email=email).first() def save(self): '''commit and save data from object''' db.session.add(self) db.session.commit() def delete(self): '''delete a user account''' db.session.delete(self) db.session.commit() def __repr__(self): return '<name: %s, username: %s, email: %s>' % ( self.name, self.username, self.email)
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 Order(db.Model): __tablename__ = 'Order' order_id = db.Column('order_id', db.Integer, primary_key=True) customer_email = db.Column(db.String(100)) date_placed = db.Column(db.DateTime) status = db.Column( db.Enum("pending", "shipped", "fulfilled", "canceled", name="status_enum")) order_products = relationship("OrderProduct", backref="order_source") def __init__(self, customer_email, date_placed, status): self.customer_email = customer_email self.date_placed = date_placed self.status = status def __repr__(self): return '<order_id {}>'.format(self.order_id) def serialize(self): return { 'order_id': self.order_id, 'customer_email': self.customer_email, 'date_placed': str(self.date_placed), 'status': self.status }
class Message(db.Model): __tablename__ = "message" __table_args__ = {"useexisting": True} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) time = db.Column(db.DateTime, default=datetime.now()) content = db.Column(db.String(255)) email = db.Column(db.String(100)) title = db.Column(db.String(255)) def __init__(self, name, content, email, title): self.name = name self.content = content self.email = email self.title = title def __repr__(self): return "<User:%s>" % self.name
class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120)) def __init__(self, name): self.name = name def __repr__(self): return "{}".format(self.name)
class Data(db.Model): id = db.Column(db.Integer, primary_key=True) costs = db.Column(db.Float) spending_money_obj = db.Column(db.String(140)) comments = db.Column(db.Text) created_date = db.Column(db.DateTime, default=datetime.now()) slug = db.Column(db.String(140), unique=True) def __init__(self, *args, **kwargs): super(Data, self).__init__(*args, **kwargs) self.generate_slug() def generate_slug(self): if self.spending_money_obj: self.slug = slugify(self.spending_money_obj) def __repr__(self): return f'< Data id: {self.id}, title: {self.spending_money_obj} >'
class Bookmark(Base): __tablename__ = 'bookmark' TYPE_BOOK = 1 TYPE_BLOG = 2 TYPE_PODCAST = 3 TYPE_VIDEO = 4 read_status = db.Column(db.Boolean, default=False) read_date = db.Column(db.DateTime) header = db.Column(db.String(50)) comment = db.Column(db.String(1024)) type = db.Column(db.Integer) polymorphic_type = db.Column(db.String(50)) __mapper_args__ = { 'polymorphic_identity': 'bookmark', "polymorphic_on": polymorphic_type, }
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 RevokedTokenModel(db.Model): '''class representing rovoked tokens table''' __tablename__ = 'revoked_tokens' id = db.Column(db.Integer, primary_key=True) json_token_identifier = db.Column(db.String(120)) def save(self): '''commit and save data from object''' db.session.add(self) db.session.commit() @classmethod def is_jti_blacklisted(cls, json_token_identifier): '''check if token is blacklisted''' query = cls.query.filter_by( json_token_identifier=json_token_identifier).first() return bool(query)
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 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)
class BookModel(db.Model): '''class representing books table''' __tablename__ = 'books' book_id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(70)) title = db.Column(db.String(70), unique=True) edition = db.Column(db.String(10)) status = db.Column(db.String(20)) copies = db.Column(db.Integer) date_created = db.Column(db.DateTime) date_modified = db.Column(db.DateTime) def __init__(self, author, title, edition, copies, status): self.author = author self.title = title self.edition = edition self.copies = copies self.status = status self.date_created = datetime.now() def save(self): '''commit and save data from object''' db.session.add(self) db.session.commit() @classmethod def get_book_by_id(cls, book_id): '''retrieve single book by id''' return cls.query.filter_by(book_id=book_id).first() @classmethod def get_book_by_title(cls, title): '''retrieve single book by title''' return cls.query.filter_by(title=title).first() @classmethod def get_all_books(cls, limit=None): '''retrieve all books''' all_books = {} result = cls.query.all() if limit is not None: '''if user sets limit''' result = cls.query.limit(limit) for book in result: all_books[book.book_id] = { "title": book.title, "author": book.author, "edition": book.edition, "copies": book.copies, "status": book.status } return all_books def delete(self): '''delete a book by id''' db.session.delete(self) db.session.commit() def __repr__(self): return '<title: %s, author: %s, edition: %s, copies: %s, status: %s>' % ( self.title, self.author, self.edition, self.copies, self.status)
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)
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))
class DbSkill(db.Model): __tablename__ = 'skill' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) user = relationship('DbUserSkillAssociation', back_populates='skill')