107 lines
3.3 KiB
Python
107 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
:author: luffmims
|
|
:url: http://yum2.cc
|
|
:copyright: © 2025 luffmims <luffmims@hotmaill.com>
|
|
:license: MIT, see LICENSE for more details.
|
|
"""
|
|
from flask import flash, jsonify, redirect, request, url_for, render_template
|
|
|
|
from inventory_check import app, db
|
|
from inventory_check.models import import_from_xlsx, Inventory
|
|
from werkzeug.exceptions import BadRequest
|
|
|
|
# @app.route('/', methods=['GET', 'POST'])
|
|
# def index():
|
|
# messages = Message.query.order_by(Message.timestamp.desc()).all()
|
|
# form = HelloForm()
|
|
# if form.validate_on_submit():
|
|
# name = form.name.data
|
|
# body = form.body.data
|
|
# message = Message(body=body, name=name)
|
|
# db.session.add(message)
|
|
# db.session.commit()
|
|
# flash('Your message have been sent to the world!')
|
|
# return redirect(url_for('index'))
|
|
# return render_template('index.html', form=form, messages=messages)
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/check', methods=['GET'])
|
|
def check():
|
|
inventory = Inventory.query.filter_by(checked=False).first()
|
|
print(inventory)
|
|
if not inventory:
|
|
return "All checked"
|
|
return render_template('check.html',inventory=inventory)
|
|
|
|
@app.route('/get_inventory', methods=['POST'])
|
|
def update_inventory():
|
|
try:
|
|
# 获取并验证表单数据
|
|
data = request.get_json()
|
|
inventory_id = data['id']
|
|
amount = data['amount']
|
|
name = data['name']
|
|
|
|
if not inventory_id or not amount:
|
|
raise BadRequest('Missing inventory id or amount')
|
|
|
|
# 查询并更新库存对象
|
|
inventory = Inventory.query.get(inventory_id)
|
|
if not inventory:
|
|
raise BadRequest('Inventory not found')
|
|
checkedone = inventory.tag
|
|
inventory.amount = amount
|
|
inventory.name = name
|
|
inventory.checked = True
|
|
|
|
# 提交更改
|
|
db.session.commit()
|
|
|
|
# 显示成功消息
|
|
# flash(f'{inventory.tag} checked and updated')
|
|
id = inventory_id + 1
|
|
inventory = Inventory.query.get(id)
|
|
if not inventory:
|
|
raise BadRequest('Inventory not found')
|
|
# 返回更新后的库存信息
|
|
return jsonify({
|
|
'id': inventory.id,
|
|
'tag': inventory.tag,
|
|
'amount': inventory.amount,
|
|
'checkedone': checkedone,
|
|
'checkedamount': amount
|
|
})
|
|
|
|
except BadRequest as e:
|
|
# 处理错误情况
|
|
flash(str(e))
|
|
return jsonify({'error': str(e)}), 400
|
|
|
|
except Exception as e:
|
|
# 处理其他可能的错误
|
|
db.session.rollback()
|
|
flash('An error occurred while updating the inventory')
|
|
return jsonify({'error': 'Internal server error'}), 500
|
|
|
|
@app.route('/upload', methods=['GET', 'POST'])
|
|
def upload_form():
|
|
if request.method == 'POST':
|
|
# 文件上传逻辑
|
|
pass
|
|
return render_template('upload.html')
|
|
|
|
@app.route('/import', methods=['POST'])
|
|
def import_file():
|
|
if request.method == 'POST':
|
|
file = request.files['file']
|
|
if file:
|
|
file_path = 'data.xlsx'
|
|
file.save(file_path)
|
|
import_from_xlsx(file_path)
|
|
return '文件导入成功!'
|
|
|
|
return redirect(url_for('upload_form'))
|