def get_product_with_id(index: int) -> Product: product = Product.query.filter_by(id=index).first() if product is None: raise TargetNotExists(Product, [index]) return product
def get_pub_lunchbox_full_with_id(index: int) -> tuple: box = get_lunchbox_full_with_id(index) if box[0].archived or not box[0].locked: raise TargetNotExists(Lunchbox, [index]) return box
def get_pub_lunchbox_products_with_id(index: int) -> List[Product]: products = get_lunchbox_products_with_id(index) box = get_lunchbox_simple(index) if box.archived or not box.locked: raise TargetNotExists(Lunchbox, [index]) return products
def get_order_for_user(user_id: int, order_id: int) -> Order: order = Order.query.filter_by(client_id=user_id).filter_by( id=order_id).first() if order is None: raise TargetNotExists(Order, [order_id]) return order
def get_lunchbox_products_with_id(index: int) -> List[Product]: raw = de.db.session.query(LunchboxProduct, Product).filter( LunchboxProduct.lunchbox_id == index, LunchboxProduct.product_id == Product.id).all() if len(raw) == 0: raise TargetNotExists(Lunchbox, [index]) return [x[1] for x in raw]
def get_user_with_id_roles(index: int) -> List[Role]: if User.query.filter_by(id=index).first() is None: raise TargetNotExists(User, [index]) user_roles = UserRole.query.filter_by(user_id=index).all() indexes = [x.role_id for x in user_roles] roles = Role.query.filter(Role.id.in_(indexes)).all() return roles
def confirm_order(order_id: int) -> None: order = Order.query.filter_by(id=order_id).first() if order is None: raise TargetNotExists(Order, [order_id]) if order.status_id != 1: raise BaseLBSException('Некорректный статус у целевого заказа!', 400) order.status_id = 3 order.timeslot_id = None de.db.session.add(order) de.db.session.commit()
def get_order_items(order_id: int) -> List[Tuple]: items = OrderItem.query.filter_by(order_id=order_id).all() if len(items) == 0: raise TargetNotExists(Order, [order_id]) boxes_raw = {x.lunchbox_id: x.quantity for x in items} boxes = { x.id: x for x in Lunchbox.query.filter(Lunchbox.id.in_( boxes_raw.keys())).all() } return [(boxes[x[0]], x[1]) for x in boxes_raw.items()]
def _get_product_from_db(index: int, archived: bool = False, locked: bool = False) -> Product: product = Product.query.filter_by(id=index).first() if product is None: raise TargetNotExists(Product, index) if not archived and product.archived: raise TargetArchived(Product, product.id) if not locked and product.locked: raise TargetLocked(Product, product.id) return product
def edit_user_with_id_roles(index, roles: List[int]) -> List[Role]: if User.query.filter_by(id=index).first() is None: raise TargetNotExists(User, [index]) target = set(roles) available = set([x.id for x in Role.query.all()]) if not target <= available: raise TargetNotExists(Role, list(target - available)[0]) user_roles = UserRole.query.filter_by(user_id=index) existing = set([x.role_id for x in user_roles.all()]) leftover = existing - target target = target - existing for item in user_roles.filter(UserRole.role_id.in_(leftover)).all(): de.db.session.delete(item) for role_id in target: ur = UserRole(user_id=index, role_id=role_id) de.db.session.add(ur) de.db.session.commit() return get_user_with_id_roles(index)
def get_lunchbox_full_with_id(index: int) -> Tuple: raw = de.db.session.query(Lunchbox, LunchboxProduct, Product).filter( Lunchbox.id == index, Lunchbox.id == LunchboxProduct.lunchbox_id, LunchboxProduct.product_id == Product.id).all() if len(raw) == 0: raise TargetNotExists(Lunchbox, [index]) # Группируем в формате ланчбокс: список продуктов filtered = { key: [x[2] for x in list(value)] for key, value in groupby(raw, lambda x: x[0]) } box = filtered.popitem() return box
def add_lunchbox(model: LunchboxEditModel) -> tuple: missing_products = get_missing_products(model.products) if len(missing_products) != 0: raise TargetNotExists(Product, missing_products) target_products = Product.query.filter(Product.id.in_( model.products)).all() for item in target_products: if item.archived: raise TargetArchived(Product, item.id) lunchbox = Lunchbox(name=model.name, price=model.price, stock=False) de.db.session.add(lunchbox) de.db.session.flush() for item in model.products: box_product = LunchboxProduct(lunchbox_id=lunchbox.id, product_id=item) de.db.session.add(box_product) de.db.session.commit() return get_lunchbox_with_id(lunchbox.id)
def edit_lunchbox_with_id(model: LunchboxEditModel) -> tuple: lunchbox = get_lunchbox_simple(model.index) if lunchbox.locked: raise TargetLocked(Lunchbox, model.index) if lunchbox.archived: raise TargetArchived(Lunchbox, model.index) missing_products = get_missing_products(model.products) if len(missing_products) != 0: raise TargetNotExists(Product, missing_products) lunchbox.name = model.name lunchbox.price = model.price de.db.session.add(lunchbox) target_products = Product.query.filter(Product.id.in_( model.products)).all() for item in target_products: if item.archived: raise TargetArchived(Product, item.id) box_products = LunchboxProduct.query.filter_by( lunchbox_id=model.index).all() box_products_ids = [x.product_id for x in box_products] for item in box_products: if item.product_id not in model.products: de.db.session.delete(item) for item in model.products: if item not in box_products_ids: box_product = LunchboxProduct(lunchbox_id=model.index, product_id=item) de.db.session.add(box_product) de.db.session.commit() return get_lunchbox_with_id(model.index)
def get_lunchbox_simple(index: int) -> Lunchbox: lunchbox = Lunchbox.query.filter_by(id=index).first() if lunchbox is None: raise TargetNotExists(Lunchbox, [index]) return lunchbox
def get_user_with_id(index: int) -> User: user = User.query.filter_by(id=index).first() if user is None: raise TargetNotExists(User, [index]) return user