def get(self, ids): """List Order""" id_list = ids.split(",") # Validate input for index, id in enumerate(id_list): if not validate_uuid4(id): abort(400, f"ID {index+1} is not valid") if len(id_list) > 10: abort(400, "Max 10 orders") # Build response items = [] for id in id_list: item = load(Order, id, allow_404=True) if item: item.table_name = item.table.name items.append(item) for item in items: if item.shop_id != items[0].shop_id: abort(400, "All ID's should belong to one shop") return items, 200
def put(self, id): args = file_upload.parse_args() logger.warning("Ignoring files via args! (using JSON body)", args=args) item = load(Kind, id) # todo: raise 404 o abort data = request.get_json() kind_update = {} image_cols = [ "image_1", "image_2", "image_3", "image_4", "image_5", "image_6" ] for image_col in image_cols: if data.get(image_col) and type(data[image_col]) == dict: name = name_file(image_col, item.name, getattr(item, image_col)) upload_file(data[image_col]["src"], name) # todo: use mime-type in first part of kind_update[image_col] = name if kind_update: kind_update["complete"] = (True if data.get("image_1") and item.description_nl and item.description_en else False) kind_update["modified_at"] = datetime.utcnow() item = update(item, kind_update) return item, 201
def post(self): """New Order""" payload = api.payload if payload.get("customer_order_id"): del payload["customer_order_id"] shop_id = payload.get("shop_id") if not shop_id: abort(400, "shop_id not in payload") # 5 gram check total_cannabis = get_price_rules_total(payload["order_info"]) logger.info("Checked order weight", weight=total_cannabis) if total_cannabis > 5: abort(400, "MAX_5_GRAMS_ALLOWED") # Availability check unavailable_product_name = get_first_unavailable_product_name( payload["order_info"], shop_id) if unavailable_product_name: abort(400, f"{unavailable_product_name}, OUT_OF_STOCK") shop = load(Shop, str(shop_id)) # also handles 404 when shop can't be found payload["customer_order_id"] = Order.query.filter_by( shop_id=str(shop.id)).count() + 1 # Todo: recalculate total and use it as a checksum for the payload order = Order(id=str(uuid.uuid4()), **payload) save(order) return order, 201
def patch(self, id): item = load(Order, id) if ("complete" not in item.status and api.payload.get("status") and (api.payload["status"] == "complete" or api.payload["status"] == "cancelled") and not item.completed_at): item.completed_at = datetime.datetime.utcnow() item.completed_by = current_user.id _ = update(item, api.payload) return "", 204
def put(self, id): """Edit Order""" item = load(Order, id) if (api.payload.get("status") and (api.payload["status"] == "completed" or api.payload["status"] == "cancelled") and not item.completed_at): item.completed_at = datetime.datetime.utcnow() item.completed_by = current_user.id item = update(item, api.payload) return item, 201
def get(self, id): """List Kind""" args = detail_parser.parse_args() item = load(Kind, id) shop = args.get("shop") if shop: item.prices = [] for price_relation in item.shop_to_price: if str(price_relation.shop_id) == shop: item.prices.append( { "id": price_relation.price.id, "internal_product_id": price_relation.price.internal_product_id, "active": price_relation.active, "new": price_relation.new, "half": price_relation.price.half if price_relation.use_half else None, "one": price_relation.price.one if price_relation.use_one else None, "two_five": price_relation.price.two_five if price_relation.use_two_five else None, "five": price_relation.price.five if price_relation.use_five else None, "joint": price_relation.price.joint if price_relation.use_joint else None, "piece": price_relation.price.piece if price_relation.use_piece else None, "created_at": price_relation.created_at, "modified_at": price_relation.modified_at, } ) else: item.prices = [] item.tags = [ {"id": tag.id, "name": tag.tag.name, "amount": tag.amount} for tag in sorted(item.kind_to_tags, key=lambda i: i.amount, reverse=True) ] item.tags_amount = len(item.tags) item.flavors = [ {"id": flavor.id, "name": flavor.flavor.name, "icon": flavor.flavor.icon, "color": flavor.flavor.color} for flavor in sorted(item.kind_to_flavors, key=lambda i: i.flavor.name) ] item.flavors_amount = len(item.flavors) item.strains = [ {"id": strain.id, "name": strain.strain.name} for strain in sorted(item.kind_to_strains, key=lambda i: i.strain.name) ] item.strains_amount = len(item.strains) item.images_amount = 0 for i in [1, 2, 3, 4, 5, 6]: if getattr(item, f"image_{i}"): item.images_amount += 1 return item, 200
def get(self, id): """List ShopToPrice""" item = load(ShopToPrice, id) price = Price.query.filter(Price.id == item.price_id).first() item.half = price.half if price.half else None item.one = price.one if price.one else None item.two_five = price.two_five if price.two_five else None item.five = price.five if price.five else None item.joint = price.joint if price.joint else None item.piece = price.piece if price.piece else None return item, 200
def put(self, id): image_cols = [ "image_1", "image_2", "image_3", "image_4", "image_5", "image_6" ] item = load(Kind, id) image = api.payload["image"] if image in image_cols: setattr(item, image, "") save(item) return item, 201
def get(self, id): """List Product""" args = detail_parser.parse_args() item = load(Product, id) shop = args.get("shop") if shop: item.prices = [] for price_relation in item.shop_to_price: if str(price_relation.shop_id) == shop: item.prices.append({ "id": price_relation.price.id, "internal_product_id": price_relation.price.internal_product_id, "active": price_relation.active, "new": price_relation.new, "half": price_relation.price.half if price_relation.use_half else None, "one": price_relation.price.one if price_relation.use_one else None, "two_five": price_relation.price.two_five if price_relation.use_two_five else None, "five": price_relation.price.five if price_relation.use_five else None, "joint": price_relation.price.joint if price_relation.use_joint else None, "piece": price_relation.price.piece if price_relation.use_piece else None, "created_at": price_relation.created_at, "modified_at": price_relation.modified_at, }) else: item.prices = [] item.images_amount = 0 for i in [1, 2, 3, 4, 5, 6]: if getattr(item, f"image_{i}"): item.images_amount += 1 return item, 200
def put(self, id): """Edit Kind""" item = load(Kind, id) if api.payload.get("approved"): if api.payload["approved"] and not item.approved: api.payload["approved_at"] = datetime.utcnow() if not api.payload["approved"] and item.approved: api.payload["approved_at"] = None api.payload["complete"] = ( True if item.image_1 and api.payload.get("description_nl") and api.payload.get("description_en") else False ) item = update(item, api.payload) return item, 201
def put(self, id): """Edit ShopToPrice""" item = load(ShopToPrice, id) # Todo increase validation -> if the update is correct price = Price.query.filter(Price.id == api.payload["price_id"]).first() shop = Shop.query.filter(Shop.id == api.payload["shop_id"]).first() kind = Kind.query.filter(Kind.id == api.payload["kind_id"]).first() product = Product.query.filter( Product.id == api.payload["product_id"]).first() if not price or not shop: abort(400, "Price or Shop not found") if (product and kind) or not product and not kind: abort(400, "One Cannabis or one Horeca product has to be provided") # Ok we survived all that: let's save it: item = update(item, api.payload) invalidateShopCache(item.shop_id) return item, 201
def put(self, id): args = file_upload.parse_args() logger.warning("Ignoring files via args! (using JSON body)", args=args) item = load(BackingTrack, id) api.payload["modified_at"] = datetime.datetime.utcnow() if api.payload.get("approved"): if api.payload["approved"] and not item.approved: api.payload["approved_at"] = datetime.datetime.utcnow() if not api.payload["approved"] and item.approved: api.payload["approved_at"] = None data = request.get_json() backing_track_update = {} file_cols = ["file"] for file_col in file_cols: if data.get(file_col) and type(data[file_col]) == dict: name = f"{uuid.uuid4()}.mp3" upload_file(data[file_col]["src"], name) backing_track_update[file_col] = name item = update(item, {**api.payload, **backing_track_update}) return item, 201
def put(self, id): """Edit Flavor""" item = load(Flavor, id) item = update(item, api.payload) return item, 201
def get(self, id): """List Flavor""" item = load(Flavor, id) return item, 200
def put(self, id): """Edit MainCategory""" item = load(MainCategory, id) item = update(item, api.payload) return item, 201
def get(self, id): """List KindToStrain""" item = load(KindToStrain, id) return item, 200
def delete(self, id): """Edit Tag""" item = load(Riff, id) delete(item) return "", 204
def delete(self, id): """Kind Delete """ item = load(Kind, id) delete(item) return "", 204
def put(self, id): """Edit Strain""" item = load(Strain, id) item = update(item, api.payload) return item, 201
def get(self, id): """List Strain""" item = load(Strain, id) return item, 200
def delete(self, id): """Delete Price""" item = load(Price, id) delete(item) return "", 204
def put(self, id): """Edit Price""" item = load(Price, id) item = update(item, api.payload) return item, 201
def get(self, id): """List Price""" item = load(Price, id) return item, 200
def delete(self, id): """Delete Flavor""" item = load(Flavor, id) delete(item) return "", 204
def put(self, id): """Edit Tag""" item = load(Riff, id) item = update(item, api.payload) return item, 201
def get(self, id): """List Order""" item = load(Order, id) item.shop_name = item.shop.name return item, 200
def get(self, id): """List Image""" item = load(Kind, id) return item, 200
def delete(self, id): """Delete Order""" item = load(Order, id) delete(item) return "", 204
def delete(self, id): """Delete Strain""" item = load(Strain, id) delete(item) return "", 204
def delete(self, id): """Edit Tag""" item = load(KindToStrain, id) delete(item) return "", 204