def clean_db(): meta = get_db().metadata for table in reversed(meta.sorted_tables): print('Clear table %s' % table) get_db().session.execute(table.delete()) get_db().session.commit() create_root(root_dict['email'], root_dict['password'])
def add_cart_item(cart_item: CartItem, user_id: int): try: get_db().session.commit() except Exception as e: logging.warning('Problem with add new cart item: ' + str(cart_item.product_id)) traceback.print_exc() # TODO: set id of product and user id raise CustomError(Error.CART_ITEM_ADD_ERROR, 400)
def add_product(product: Product, user_id: int): product.added_by = user_id try: get_db().session.add(product) get_db().session.commit() except Exception as e: logging.warning('Problem with add new product: ' + product.name) traceback.print_exc() raise CustomError(Error.PRODUCT_ADD_ERROR, 400)
def put_favourite(product_id, user_id): favourite = Favourite.query.filter_by(product_id=product_id).filter_by( user_id=user_id).first() if favourite is None: favourite = Favourite() favourite.product_id = product_id favourite.user_id = user_id get_db().session.add(favourite) else: raise CustomError(Error.FAVOURITE_CHANGE_ERROR, HTTPStatus.CONFLICT)
def delete_cart_items(cart_item_id: int, user_id: int): cart_item = CartItem.query.filter_by(user_id).filter_by( cart_item_id).delete() try: get_db().session.commit() except Exception as e: # TODO: set id of product and user id logging.warning('Problem with delete new cart item: ' + str(cart_item.product_id)) traceback.print_exc() # TODO: set id of product and user id raise CustomError(Error.CART_ITEM_ADD_ERROR, 400)
def put_cart_items(quantity: int, cart_item_id: int, user_id: int): cart_item = CartItem.query.filter_by(user_id).filter_by( cart_item_id).first() cart_item.quantity = quantity try: get_db().session.commit() except Exception as e: # TODO: set id of product and user id logging.warning('Problem with update cart item: ' + str(cart_item.product_id)) traceback.print_exc() # TODO: set id of product and user id raise CustomError(Error.CART_ITEM_UPDATE_ERROR, 400)
def update_product(product_data: dict, product_id: int, user_id: int): product = Product.query.filter_by(deleted=False).filter_by( id=product_id).first() if product is not None: for key, value in product_data.items(): if hasattr(product, key): setattr(product, key, value) else: # TODO: raise error pass product.added_by = user_id #TODO except get_db().session.commit() else: raise CustomError(Error.PRODUCT_NOT_FOUND, 404)
class Inventory(Base, get_db().Model): __tablename__ = 'inventory_table' quantity = Column(Integer, default=0) product_id = Column(Integer, ForeignKey(Product.id)) product = relationship('Product', foreign_keys='Inventory.product_id')
class User(UserMixin, Base, get_db().Model): __tablename__ = 'user_table' email = Column(String(60), index=True, unique=True) first_name = Column(String(60)) last_name = Column(String(60)) is_admin = Column(Boolean, default=False) is_active = Column(Boolean, default=False) last_login = Column(DateTime) password_hash = Column(String(128)) blocked = Column(Boolean, default=False) root = Column(Boolean, default=False) @property def password(self): raise AttributeError('password is not a readable attribute.') @password.setter def password(self, password): # TODO: validate if password is in proper format self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def from_json(self, data): self.email = data['email'] self.first_name = data['firstName'] if 'firstName' in data else None self.last_name = data['lastName'] if 'lastName' in data else None self.password = data['password']
class Product(Base, get_db().Model): __tablename__ = 'product_table' name = Column(String(32), nullable=False, unique=True) code = Column(String(32), nullable=False, unique=True) price = Column(Float, nullable=True) description = Column(Text) added_by = Column(Integer, ForeignKey(User.id)) group_id = Column(Integer, ForeignKey(Group.id)) group = relationship('Group', foreign_keys='Product.group_id') def from_json(self, data): self.name = data['name'] self.code = data['code'] self.price = data['price'] self.description = data['description'] self.group = data['groupId'] if 'groupId' in data else None
def add_product(product: Product, user_id: int): product.added_by = user_id get_db().session.add(product)
class Magazine(Base, get_db().Model): __tablename__ = 'magazine_table' product_id = Column(Integer, ForeignKey(Product.id)) quantity = Column(Integer, default=0)
def add_user(user_to_create: User): user = User.query.filter_by(email=user_to_create.email).first() if user is None: get_db().session.add(user_to_create) else: raise CustomError(Error.USER_ALREADY_EXISTS, 409)
class Group(Base, get_db().Model): __tablename__ = 'group_table' name = Column(String(32), nullable=False, unique=True) description = Column(Text) added_by = Column(Integer, ForeignKey(User.id))
def add_inventory(inventory: Inventory): get_db().session.add(inventory)
def process_commit(error_enum): try: get_db().session.commit() except Exception as e: logging.warning('traceback: ' + traceback.print_exc()) raise CustomError(error_enum, 400)
class Favourite(Base, get_db().Model): __tablename__ = 'favourite_table' product_id = Column(Integer, ForeignKey(Product.id)) user_id = Column(Integer, ForeignKey(User.id))