Files
inventory_check/inventory_check/views.py
2025-02-27 08:50:46 +00:00

123 lines
3.9 KiB
Python

# -*- coding: utf-8 -*-
"""
:author: luffmims
:url: http://yum2.cc
:copyright: © 2025 luffmims <luffmims@hotmaill.com>
:license: MIT, see LICENSE for more details.
"""
import time
from flask import jsonify, redirect, request, url_for, render_template, send_file
from flask import make_response
from io import BytesIO
from openpyxl import Workbook
from inventory_check import app, db
from inventory_check.models import Inventory, import_from_xlsx
from werkzeug.exceptions import BadRequest
@app.route('/')
def index():
return render_template('index.html')
@app.route('/check', methods=['GET'])
def check():
inventory = Inventory.query.filter_by(checked=False).first()
if not inventory:
return render_template('after.html',msg="未导入或已盘完")
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']
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.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,
'location': inventory.location,
'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 render_template('after.html',msg="成功")
return redirect(url_for('upload_form'))
@app.route('/download-xlsx')
def download_xlsx():
filename = "盘点表" + time.strftime("%Y%m%d.xlsx", time.localtime())
# 创建一个 Excel 工作簿
inventorys = Inventory.query.all() # 查询所有数据, and sort data by sort
# 创建一个工作簿
wb = Workbook()
# 创建一个工作表
ws = wb.active
# 设置工作表名称
ws.title = "Inventory"
# 设置工作表列名
ws.append(["序号", "位置", "标签", "数量", "已确认"])
# 写入数据
for inventory in inventorys:
ws.append([inventory.sort, inventory.location, inventory.tag, inventory.amount, inventory.checked])
output = BytesIO()
wb.save(output)
output.seek(0) # 移动到字节流的开头
# 设置响应头,提供下载功能
response = make_response(send_file(output, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', as_attachment=True, download_name=filename))
response.headers["Content-Disposition"] = "attachment; filename=" + filename
return response