class User(db.Model, UserMixin): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), index=True) password_hash = db.Column(db.String(128)) email = db.Column(db.String(128)) description = db.Column(db.String(1000)) following_users = db.relationship( 'User', secondary=association_table2, primaryjoin=id == association_table2.c.left_id, secondaryjoin=id == association_table2.c.right_id, backref='followers') following_charities = db.relationship('Charity', secondary=association_table, backref='followers') def __init__(self, username=None, description=None, password=None): if username: self.username = username if password: self.hash_password(password) if description: self.description = description def hash_password(self, password): self.password_hash = pwd_context.encrypt(password) def verify_password(self, password): return pwd_context.verify(password, self.password_hash)
class CartItem(db.Model, SavableMixin): def __init__(self, **kwargs): super(CartItem, self).__init__(**kwargs) # self.updateTotal() # print('cart item created') cart_item_id = db.Column( db.Integer, primary_key=True, nullable=False, autoincrement=True ) cart_id = db.Column( db.Integer, db.ForeignKey('cart.cart_id'), nullable=False ) product_id = db.Column( db.Integer, db.ForeignKey('product.product_id'), nullable=False ) product = db.relationship( 'Product', uselist=False ) cart = db.relationship( 'Cart', backref=db.backref('items', lazy=True) ) quantity = db.Column( db.Integer, nullable=False, default=1 ) total = db.Column( db.Float, nullable=False, default=0, ) def calculateTotal(self): return self.product.price*self.quantity def updateTotal(self): self.total = self.calculateTotal() self.save() return True def delete(self): db.session.delete(self)
class OrderItem(db.Model): order_item_id = db.Column( db.Integer, primary_key=True, nullable=False, autoincrement=True ) order_id = db.Column( db.Integer, db.ForeignKey('order.order_id'), nullable=False ) order = db.relationship( 'Order', backref=db.backref('orderitems', lazy=True) ) product_id = db.Column( db.Integer, db.ForeignKey('product.product_id'), nullable=False, ) product = db.relationship( 'Product', uselist=False ) quantity = db.Column( db.Integer, nullable=False, default=1, ) total = db.Column( db.Float, nullable=False, default=0.00 ) def calculateTotal(self): return self.product.price*self.quantity def updateTotal(self): self.total = self.calculateTotal() self.save() return True
class ProductImage(db.Model): image_id = db.Column( db.Integer, primary_key=True, nullable=False, autoincrement=True ) product_id = db.Column( db.Integer, db.ForeignKey('product.product_id'), nullable=False ) name = db.Column( db.String(100) ) product = db.relationship( 'Product', backref=db.backref('images', lazy=True), ) def delete(self): from karma.admin.views import Image, CATEGORY_URL, PRODUCT_URL image = Image(None, PRODUCT_URL, self.name) image.deleteImage() db.session.delete(self)
class Post(db.Model): __tablename__ = 'post' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(32), index=True) content = db.Column(db.String(1000), index=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) karma = db.relationship('User', secondary=karma_posts, backref='liked_posts')
class Location(db.Model): location_id = db.Column( db.Integer, primary_key=True, nullable=False, autoincrement=True ) order_id = db.Column( db.Integer, db.ForeignKey('order.order_id'), nullable=False ) latitude = db.Column( db.Float, nullable=False, ) longitude = db.Column( db.Float, nullable=False ) country = db.Column( db.String(200), nullable=True ) address = db.Column( db.String(200), nullable=True ) order = db.relationship( 'Order', backref=db.backref('location', lazy=True) )
class Product(db.Model): product_id = db.Column( db.Integer, primary_key=True, nullable=False, autoincrement=True ) title = db.Column( db.String(100) ) description = db.Column( db.String(500), nullable=True ) price = db.Column( db.Float, nullable=False, default=0.00 ) times_bought = db.Column( db.Integer, nullable=False, default=0 ) views = db.Column( db.Integer, nullable= False, default = 0 ) in_stock = db.Column( db.Integer, nullable=False, default=3 ) category_id = db.Column( db.Integer, db.ForeignKey('category.category_id'), nullable=False ) # images = db.relationship('ProductImage', backref=db.backref('product', lazy=True)) category = db.relationship( 'Category', backref=db.backref('product', lazy=True)) def addImages(self, images): for image in images: image_name = self.storeImage(image) img = ProductImage(name=image_name) self.images.append(img) def storeImage(self, image): from karma.admin.views import Image, CATEGORY_URL, PRODUCT_URL image = Image(image, PRODUCT_URL) return image.create() def delete(self,): self.deleteImages() db.session.delete(self) db.session.commit() def deleteImages(self,): for image in self.images: image.delete() return True def incrementTimesViewed(self): self.views+=1 db.session.add(self) db.session.commit() def incrementTimesBought(self, quantity = 1): self.times_bought+=quantity db.session.add(self) db.session.commit()
class Cart(db.Model, SavableMixin): def __init__(self, **kwargs): super(Cart, self).__init__(**kwargs) self.updateTotal() print('cart created') cart_id = db.Column( db.Integer, primary_key=True, nullable=False, autoincrement=True ) user_id = db.Column( db.Integer, db.ForeignKey('user.user_id'), nullable=False, ) user = db.relationship( 'User', backref='cart', uselist=True, ) total = db.Column( db.Float, nullable=False, default=0.00 ) def itemExists(self, item): for product in self.items: if str(product.product_id) == str(item.product_id): return True return False def retrieveItem(self, item): for product in self.items: if str(product.product_id) == str(item.product_id): return product def addItem(self, item): if(self.itemExists(item)): existingItem = self.retrieveItem(item) existingItem.quantity = int( existingItem.quantity) + int(item.quantity) print( """ This item already exists """ ) else: self.items.append(item) self.save() return True def calculateTotal(self): total = 0 for item in self.items: total += item.total return total def updateTotal(self): self.total = self.calculateTotal() self.save() return True def clear(self): self.total = 0 for item in self.items: item.delete() self.save()
class Order(db.Model): order_id = db.Column( db.Integer, primary_key=True, nullable=False, autoincrement=True ) status = db.Column( db.String(15), nullable=False, default=OrderStatus['PENDING'] ) date_created = db.Column( db.DateTime, nullable=False, default=datetime.now ) user_id = db.Column( db.Integer, db.ForeignKey('user.user_id'), nullable=False ) user = db.relationship( 'User', backref=db.backref('orders', lazy=True) ) total = db.Column( db.Float, nullable=False, default=0.00 ) @staticmethod def count(): return len(Order.query.all()) @staticmethod def pendingOrders(): return len(Order.query.filter_by(status=OrderStatus['PENDING']).all()) @staticmethod def deliveredOrders(): return len(Order.query.filter_by(status=OrderStatus['DELIVERED']).all()) @staticmethod def transitOrders(): return len(Order.query.filter_by(status=OrderStatus['TRANSIT']).all()) def setLocation(self, location): self.location.append(location) def setUpItems(self, ): for item in current_user.cart[0].items: newItem = OrderItem( product_id=item.product_id, quantity=item.quantity, total=item.total ) item.product.incrementTimesBought(item.quantity) self.orderitems.append(newItem) self.updateTotal() def updateTotal(self): self.total = current_user.cart[0].total