From f880fa60811cad6e41d43bdb4c7136ea71842f29 Mon Sep 17 00:00:00 2001 From: Ching L Date: Sun, 29 Sep 2024 15:24:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81?= =?UTF-8?q?=E6=97=B6=E4=BF=AE=E5=A4=8D=E6=9D=A1=E7=A0=81=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E6=95=B0=E9=87=8F=EF=BC=9B=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=89=AB=E7=A0=81=E5=90=8E=E5=95=86=E5=93=81=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E8=BE=A9=E9=A2=98=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=95=86=E5=93=81=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复了创建商品时条码保存有误的问题,并添加了默认的数量为1.0的条码。这样可以确保在添加商品时条码保存正确,并且默认数量为1.0。 --- app.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index 034dbe7..cfc2dc8 100644 --- a/app.py +++ b/app.py @@ -70,7 +70,7 @@ def add_product(dict_good, location): elif "description_cn" in dict_good: good_name = dict_good["description_cn"] if not good_name: - return False + return False, good_name if 'specification' in dict_good: good_name = good_name + " - " + dict_good['specification'] @@ -152,7 +152,7 @@ def add_product(dict_good, location): print("Request error:", err) grocy.add_product_by_barcode(dict_good["gtin"], 1.0, 0.0) - return True + return True, good_name def gpc_best_before_days_and_location(Code, locations): @@ -241,32 +241,34 @@ def handle_add_product(barcode, location): barcode_ = product_barcode break if barcode_: - amount = barcode_.amount + if hasattr(barcode_, "amount") and barcode_.amount: + amount = barcode_.amount grocy.add_product_by_barcode(barcode, amount, 0.0) response_data = {"message": "Item added successfully"} - return response_data, 200 + return response_data, 200, product.name except: spider = BarcodeSpider(x_rapidapi_key=X_RapidAPI_Key) good = spider.get_good(barcode) if not good: response_data = {"message": "Item not found"} - return response_data, 400 + return response_data, 400, None try: - if add_product(good, location): + added, good_name = add_product(good, location) + if added: response_data = {"message": "New item added successfully"} - return response_data, 200 + return response_data, 200, good_name else: response_data = {"message": "Fail to add new item"} - return response_data, 400 + return response_data, 400, None except Exception as e: if hasattr(e, "message"): error_message = e.message else: error_message = str(e) response_data = {"message": error_message} - return response_data, 400 + return response_data, 400, None @app.route("/") def index(): @@ -384,11 +386,13 @@ def consume_stream(): logger.info(f"Skip non-numeric barcode: {msg['data']}") continue - resp, status_code = handle_add_product(msg['data'], '') + resp, status_code, good_name = handle_add_product(msg['data'], '') + if not good_name: + good_name = '' if status_code == 200: # 消费完成后确认消息 r.xack(STREAM_KEY, CONSUMER_GROUP, msg_id) - bark_push('商品添加成功', '条形码 %s' % msg['data']) + bark_push('%s 添加成功' % good_name, '条形码 %s' % msg['data']) else: logger.error(f"Failed to process message: {msg['data']} {resp}") bark_push('商品添加失败', '条形码 %s' % msg['data'])