class RentPeriodModel(db.Model): __tablename__ = 'rentperiod' id = db.Column(db.Integer, primary_key=True) start_date = db.Column(db.Date) end_date = db.Column(db.Date) user_name = db.Column(db.String) rentableitem = db.relationship("RentableItemModel", back_populates="rent_periods") rentableitem_id = db.Column(db.Integer, db.ForeignKey('rentableitem.id')) def __init__(self, data): self.start_date = data.get("start_date") self.end_date = data.get("end_date") self.user_name = data.get("user_name") def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() @staticmethod def get_all_rent_periods_with_rentableitem_id(value): return RentPeriodModel.query.filter_by(rentableitem_id=value).all()
class Task(db.Model, BaseModel): __tablename__ = 'task' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128)) status = db.Column(db.Boolean, default=False) create_time = db.Column(db.DateTime, default=datetime.datetime.utcnow) finish_time = db.Column(db.DateTime, default=None)
class Movie(db.Model): __tablename__ = "movies" title = db.Column(db.String(50), primary_key=True, unique=True) genre = db.Column(db.String(25)) release_year = db.Column(db.Integer) expiry_date = db.Column(db.DateTime) def __init__(self, title, genre, release_year, expiry_date): self.title = title self.genre = genre self.release_year = release_year self.expiry_date = expiry_date
class Users(db.Model): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(20)) last_name = db.Column(db.String(20)) email = db.Column(db.String(20)) username = db.Column(db.String(20), unique=True) password = db.Column(db.String(20)) cookie_consent = db.Column(db.String(10))
class User(db.Model): """ Create user table """ __tablename__ = 'users' username = db.Column(db.String(50), primary_key=True) password = db.Column(db.String(100)) firstname = db.Column(db.String(50)) lastname = db.Column(db.String(50)) email = db.Column(db.String(100)) address = db.Column(db.String(100)) def __init__(self, data): self.username = data.get('username') self.password = self.__generate_hash(data.get('password')) self.firstname = data.get('firstname') self.lastname = data.get('lastname') self.email = data.get('email') self.address = data.get('address') def __generate_hash(self, password): return bcrypt.generate_password_hash(password, rounds=10).decode("utf-8") def __repr__(self): return 'User: {}'.format(self.username)
class Variants(db.Model): """ Create a product variant table """ __tablename__ = 'variants' id = db.Column(db.String(10), primary_key=True) productId = db.Column(db.String(60)) sellingPrice = db.Column(db.Float) costPrice = db.Column(db.Float) quantity = db.Column(db.Integer) def __init__(self, data): self.id = data.get('id') self.productId = data.get('productId') self.sellingPrice = data.get('sellingPrice') self.costPrice = data.get('costPrice') self.quantity = data.get('quantity') def __repr__(self): return 'Product Variant: {}'.format(self.id)
class Cat(db.Model): __tablename__ = "cats" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), nullable=False) def as_dict(self): return dict(id=self.id, name=self.name) @classmethod def find_by_id(cls, id): return cls.query.filter_by(id=id).first() @classmethod def get_all(cls): return cls.query.all() def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit()
class Product(db.Model): """ Create product table """ __tablename__ = 'products' id = db.Column(db.String(10), primary_key=True) branchId = db.Column(db.String(10)) name = db.Column(db.String(100), unique=True) brand = db.Column(db.String(20)) category = db.Column(db.String(20)) productCode = db.Column(db.String(200), unique=True) def __init__(self, data): self.id = data.get('id') self.branchId = data.get('branchId') self.name = data.get('name') self.brand = data.get('brand') self.category = data.get('category') self.productCode = data.get('productCode') def __repr__(self): return 'Product: {}'.format(self.id)
class ActivityLog(db.Model): """ Create branch table """ __tablename__ = 'activity_log' id = db.Column(db.Integer, primary_key=True) user = db.Column(db.String(50)) oldValue = db.Column(db.String(20)) newValue = db.Column(db.String(20)) action = db.Column(db.String(20)) ontable = db.Column(db.String(20)) def __init__(self, data): self.id = data.get('id') self.name = data.get('name') self.district = data.get('district') self.state = data.get('state') self.country = data.get('country') self.location = data.get('location') def __repr__(self): return 'Branch: {}'.format(self.id)
class Branch(db.Model): """ Create branch table """ __tablename__ = 'branches' id = db.Column(db.String(10), primary_key=True) name = db.Column(db.String(50), unique=True) district = db.Column(db.String(20)) state = db.Column(db.String(20)) country = db.Column(db.String(20)) location = db.Column(db.String(100)) def __init__(self, data): self.id = data.get('id') self.name = data.get('name') self.district = data.get('district') self.state = data.get('state') self.country = data.get('country') self.location = data.get('location') def __repr__(self): return 'Branch: {}'.format(self.id)
class Device(db.Model): """ Device class """ class Category(object): SIMULATOR = 'simulator' HARDWARE = 'hardware' class Status(object): INACTIVE = 'inactive' AVAILABLE = 'available' USED = 'used' __tablename__ = 'device' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) category = db.Column(db.Enum(Category.SIMULATOR, Category.HARDWARE, name='category'), nullable=False) type = db.Column(db.String(50), nullable=False) version = db.Column(db.String(10), nullable=False) ip = db.Column(db.String(25), nullable=False) port = db.Column(db.String(10), nullable=False) status = db.Column(db.Enum(Status.INACTIVE, Status.AVAILABLE, Status.USED, name='status'), nullable=False) def __init__(self, name: str, category: str, type: str, version: str, ip: str, port: str, status: str): """ Constructor :param name: Device name :param category: Device category :param type: Device Type :param version: Device version :param ip: Ip address of the device :param port: Port number of the device :param status: Device status """ self.name = name self.category = category self.type = type self.version = version self.ip = ip self.port = port self.status = status def update(self, id: int = None, name: str = None, category: str = None, type: str = None, version: str = None, ip: str = None, port: str = None, status: str = None): if name: self.name = name if category: self.category = category if type: self.type = type if version: self.version = version if ip: self.ip = ip if port: self.port = port if status: self.status = status def to_dict(self) -> dict: """ Convert device object to dict :return: device """ return { 'id': self.id, 'name': self.name, 'category': self.category, 'type': self.type, 'version': self.version, 'ip': self.ip, 'port': self.port, 'status': self.status } def __repr__(self): """ Convert device object to string :return: device """ return '<Device(id={id}, name={name}, category={category}, type={type}, version={v}, ip={ip}, port={port}, ' \ 'status={status})'. \ format(id=self.id, name=self.name, category=self.category, type=self.type, v=self.version, ip=self.ip, port=self.port, status=self.status)