feat: 添加商品时修复条码保存问题,并添加默认数量;
All checks were successful
continuous-integration/drone/push Build is passing

修改扫码后商品添加通知辩题,增加商品名称

修复了创建商品时条码保存有误的问题,并添加了默认的数量为1.0的条码。这样可以确保在添加商品时条码保存正确,并且默认数量为1.0。
This commit is contained in:
Ching L 2024-09-29 15:24:32 +08:00
parent 08ede35dff
commit f880fa6081

24
app.py
View File

@ -70,7 +70,7 @@ def add_product(dict_good, location):
elif "description_cn" in dict_good: elif "description_cn" in dict_good:
good_name = dict_good["description_cn"] good_name = dict_good["description_cn"]
if not good_name: if not good_name:
return False return False, good_name
if 'specification' in dict_good: if 'specification' in dict_good:
good_name = good_name + " - " + dict_good['specification'] good_name = good_name + " - " + dict_good['specification']
@ -152,7 +152,7 @@ def add_product(dict_good, location):
print("Request error:", err) print("Request error:", err)
grocy.add_product_by_barcode(dict_good["gtin"], 1.0, 0.0) 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): def gpc_best_before_days_and_location(Code, locations):
@ -241,32 +241,34 @@ def handle_add_product(barcode, location):
barcode_ = product_barcode barcode_ = product_barcode
break break
if barcode_: if barcode_:
if hasattr(barcode_, "amount") and barcode_.amount:
amount = barcode_.amount amount = barcode_.amount
grocy.add_product_by_barcode(barcode, amount, 0.0) grocy.add_product_by_barcode(barcode, amount, 0.0)
response_data = {"message": "Item added successfully"} response_data = {"message": "Item added successfully"}
return response_data, 200 return response_data, 200, product.name
except: except:
spider = BarcodeSpider(x_rapidapi_key=X_RapidAPI_Key) spider = BarcodeSpider(x_rapidapi_key=X_RapidAPI_Key)
good = spider.get_good(barcode) good = spider.get_good(barcode)
if not good: if not good:
response_data = {"message": "Item not found"} response_data = {"message": "Item not found"}
return response_data, 400 return response_data, 400, None
try: try:
if add_product(good, location): added, good_name = add_product(good, location)
if added:
response_data = {"message": "New item added successfully"} response_data = {"message": "New item added successfully"}
return response_data, 200 return response_data, 200, good_name
else: else:
response_data = {"message": "Fail to add new item"} response_data = {"message": "Fail to add new item"}
return response_data, 400 return response_data, 400, None
except Exception as e: except Exception as e:
if hasattr(e, "message"): if hasattr(e, "message"):
error_message = e.message error_message = e.message
else: else:
error_message = str(e) error_message = str(e)
response_data = {"message": error_message} response_data = {"message": error_message}
return response_data, 400 return response_data, 400, None
@app.route("/") @app.route("/")
def index(): def index():
@ -384,11 +386,13 @@ def consume_stream():
logger.info(f"Skip non-numeric barcode: {msg['data']}") logger.info(f"Skip non-numeric barcode: {msg['data']}")
continue 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: if status_code == 200:
# 消费完成后确认消息 # 消费完成后确认消息
r.xack(STREAM_KEY, CONSUMER_GROUP, msg_id) r.xack(STREAM_KEY, CONSUMER_GROUP, msg_id)
bark_push('商品添加成功', '条形码 %s' % msg['data']) bark_push('%s 添加成功' % good_name, '条形码 %s' % msg['data'])
else: else:
logger.error(f"Failed to process message: {msg['data']} {resp}") logger.error(f"Failed to process message: {msg['data']} {resp}")
bark_push('商品添加失败', '条形码 %s' % msg['data']) bark_push('商品添加失败', '条形码 %s' % msg['data'])