class Product(db.Model, CRUDMixin, BaseModel): __tablename__ = 'product' product_name = db.Column('product_name', db.String(200)) description = db.Column('description', db.String(200)) price = db.Column('price', db.DECIMAL) produced_at = db.Column('produced_at', db.DateTime) capacity = db.Column('capacity', db.Integer) category_id = db.Column('category', db.ForeignKey('category.id')) item = relationship('Item', backref="item_product") category = relationship('Category', backref=db.backref("product_category")) reviews = relationship('ProductReview', backref=db.backref("product_review")) def __init__(self, **kwargs): db.Model.__init__( self, product_name=kwargs.get('product_name'), description=kwargs.get('description'), price=kwargs.get('price'), capacity=kwargs.get('capacity'), category=kwargs.get('category'), ) @classmethod def add_amount(cls, product_id, amount): product = cls.get(product_id) product.capacity = product.capacity + amount product.save() @classmethod def reduce_amount(cls, product_id, amount): product = cls.get(product_id) product.capacity = product.capacity - amount product.save()
class User(db.Model, CRUDMixin, BaseModel): __tablename__ = 'user' name = db.Column('name', db.String(200)) salt = db.Column('salt', db.String(200)) password = db.Column('password', db.String(200)) email = db.Column('email', db.String(200)) avatar = db.Column('avatar', db.String(256)) last_login = db.Column('last_login', db.DateTime, default=datetime.utcnow) status = db.Column('status', db.Enum(AccountStatus)) cart = db.relationship("Cart", backref=db.backref("cart_user")) address = db.relationship("Address", backref=db.backref("user_address")) def __init__(self, **kwargs): db.Model.__init__(self, name=kwargs.get('name'), salt=kwargs.get('salt'), password=kwargs.get('password'), email=kwargs.get('email'), status=kwargs.get('status') or AccountStatus.inactive, last_login=kwargs.get('last_login') or datetime.utcnow(), ) def __str__(self): return
class Category(db.Model, CRUDMixin, BaseModel): __tablename__ = 'category' category_name = db.Column('category_name', db.String(20)) product = db.relationship("Product", backref="category_product") def __init__(self, **kwargs): db.Model.__init__(self, category_name=kwargs.get('category_name')) def __repr__(self): return '<Category %r>' % self.__category_name
class Address(db.Model, CRUDMixin, BaseModel): __tablename__ = 'address' street = db.Column('street', db.String(200)) city = db.Column('city', db.String(128)) state = db.Column('state', db.String(200)) zip_code = db.Column('zip_code', db.String(10)) user_id = db.Column('user_id', db.ForeignKey('user.id')) user = relationship('User', backref=db.backref('user_address')) def __init__(self, **kwargs): db.Model.__init__(self, street=kwargs.get('street'), city=kwargs.get('city'), state=kwargs.get('state'), zip_code=kwargs.get('zip_code'), user_id=kwargs.get('user_id') or None ) def __str__(self): return "ID=%d, City=%s, State=%d" % (self.id, self.city, self.state)
class ShipmentTrack(db.Model, CRUDMixin, BaseModel): __tablename__ = 'shipmenttrack' shipment_date = db.Column('shipment_date', db.Enum(ShipmentStatus)) esitmated_arrival = db.Column('estimated_arrival', db.DateTime) track_number = db.Column('track_number', db.String(200)) shipment_id = db.Column('shipment_id', db.ForeignKey('shipment.id')) shipment = relationship("Shipment", backref="shipmenttrack") def __init__(self, **kwargs): db.Model.__init__(self, shipment_date=kwargs.get('shipment_date'), estimated_arrival=kwargs.get('esitmated_arrival'), track_number=kwargs.get('track_number') )
class Tag(db.Model, BaseModel): __tablename__ = 'tag' tag_name = db.Column('tag_name', db.String(20)) def __init__(self, **kwargs): db.Model.__init__(self, name=kwargs.get('tag_name'))