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 Cart(db.Model, CRUDMixin): """ Contains a list of items, which indicates the items that the customer want to proceed. """ __tablename__ = 'cart' id = db.Column('id', db.Integer, autoincrement=True, nullable=False, unique=True, primary_key=True) user_id = db.Column('user_id', db.ForeignKey('user.id')) items = db.relationship('Item', backref=db.backref("cart_items")) user = db.relationship("User", backref=db.backref("user_cart")) def __init__(self, **kwargs): db.Model.__init__(self, user_id=kwargs.get('user_id') )
class ProductReview(db.Model, CRUDMixin, BaseModel): __tablename__ = 'productreview' rating = db.Column('rating', db.Integer) review = db.Column('review', db.Text) product_id = db.Column('product_id', db.ForeignKey('product.id')) product = relationship('Product', backref=db.backref('productreview')) def __init__(self, **kwargs): db.Model.__init__(self, rating=kwargs.get('rating'), review=kwargs.get('review'), product_id=kwargs.get('product_id'))
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)