def ajax_delete(): """ 库存删除 :return: """ ajax_success_msg = AJAX_SUCCESS_MSG.copy() ajax_failure_msg = AJAX_FAILURE_MSG.copy() # 检查删除权限 if not permission_inventory_section_del.can(): ext_msg = _('Permission Denied') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查请求方法 if not (request.method == 'GET' and request.is_xhr): ext_msg = _('Method Not Allowed') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查请求参数 inventory_id = request.args.get('inventory_id', 0, type=int) if not inventory_id: ext_msg = _('ID does not exist') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) inventory_info = get_inventory_row_by_id(inventory_id) # 检查资源是否存在 if not inventory_info: ext_msg = _('ID does not exist') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) # 检查资源是否删除 if inventory_info.status_delete == STATUS_DEL_OK: ext_msg = _('Already deleted') ajax_failure_msg['msg'] = _('Del Failure, %(ext_msg)s', ext_msg=ext_msg) return jsonify(ajax_failure_msg) current_time = datetime.utcnow() inventory_data = { 'status_delete': STATUS_DEL_OK, 'delete_time': current_time, 'update_time': current_time, } result = edit_inventory(inventory_id, inventory_data) if result: ajax_success_msg['msg'] = _('Del Success') return jsonify(ajax_success_msg) else: ajax_failure_msg['msg'] = _('Del Failure') return jsonify(ajax_failure_msg)
def lists(): """ 库存列表 :return: """ template_name = 'inventory/lists.html' # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('inventory lists') # 搜索条件 form = InventorySearchForm(request.form) form.warehouse_id.choices = get_warehouse_choices() form.rack_id.choices = get_rack_choices(form.warehouse_id.data) # app.logger.info('') inventory_brand_choices = [ (brand, brand) for brand in get_distinct_inventory_brand(status_delete=STATUS_DEL_NO) if brand != '' ] form.production_brand.choices = DEFAULT_SEARCH_CHOICES_STR + inventory_brand_choices search_condition = [ Inventory.status_delete == STATUS_DEL_NO, Inventory.stock_qty_current > 0, ] if request.method == 'POST': # 表单校验失败 if not form.validate_on_submit(): flash(_('Search Failure'), 'danger') # 单独处理csrf_token if hasattr(form, 'csrf_token') and getattr(form, 'csrf_token').errors: map(lambda x: flash(x, 'danger'), form.csrf_token.errors) else: if form.warehouse_id.data != DEFAULT_SEARCH_CHOICES_INT_OPTION: search_condition.append( Inventory.warehouse_id == form.warehouse_id.data) if form.rack_id.data != DEFAULT_SEARCH_CHOICES_INT_OPTION: search_condition.append(Inventory.rack_id == form.rack_id.data) if form.production_brand.data != DEFAULT_SEARCH_CHOICES_STR_OPTION: search_condition.append( Inventory.production_brand == form.production_brand.data) if form.production_model.data: search_condition.append( Inventory.production_model.like( '%%%s%%' % form.production_model.data)) # 处理导出 if form.op.data == OPERATION_EXPORT: # 检查导出权限 if not permission_inventory_section_export.can(): abort(403) column_names = Inventory.__table__.columns.keys() query_sets = get_inventory_rows(*search_condition) return excel.make_response_from_query_sets( query_sets=query_sets, column_names=column_names, file_type='csv', file_name='%s.csv' % _('inventory lists')) # 批量删除 if form.op.data == OPERATION_DELETE: # 检查删除权限 if not permission_inventory_section_del.can(): abort(403) inventory_ids = request.form.getlist('inventory_id') result_total = True for inventory_id in inventory_ids: current_time = datetime.utcnow() inventory_data = { 'status_delete': STATUS_DEL_OK, 'delete_time': current_time, 'update_time': current_time, } result = edit_inventory(inventory_id, inventory_data) result_total = result_total and result if result_total: flash(_('Del Success'), 'success') else: flash(_('Del Failure'), 'danger') # 翻页数据 pagination = get_inventory_pagination(form.page.data, PER_PAGE_BACKEND, *search_condition) # 渲染模板 return render_template(template_name, form=form, pagination=pagination, **document_info)
def edit(inventory_id): """ 库存编辑 """ inventory_info = get_inventory_row_by_id(inventory_id) # type: Inventory # 检查资源是否存在 if not inventory_info: abort(404) # 检查资源是否删除 if inventory_info.status_delete == STATUS_DEL_OK: abort(410) template_name = 'inventory/edit.html' # 加载编辑表单 form = InventoryEditForm(request.form) form.warehouse_id.choices = get_warehouse_choices(option_type='update') form.rack_id.choices = get_rack_choices(form.warehouse_id.data or inventory_info.warehouse_id, option_type='update') # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('inventory edit') # 进入编辑页面 if request.method == 'GET': # 表单赋值 form.production_id.data = inventory_info.production_id form.production_brand.data = inventory_info.production_brand form.production_model.data = inventory_info.production_model form.production_sku.data = inventory_info.production_sku form.warehouse_id.data = inventory_info.warehouse_id form.rack_id.data = inventory_info.rack_id form.stock_qty.data = inventory_info.stock_qty_current form.note.data = inventory_info.note # 渲染页面 return render_template(template_name, inventory_id=inventory_id, form=form, **document_info) # 处理编辑请求 if request.method == 'POST': # 修改仓库 - 不做校验 if form.warehouse_changed.data: form.warehouse_changed.data = '' return render_template(template_name, form=form, **document_info) # 表单校验失败 if not form.validate_on_submit(): flash(_('Edit Failure'), 'danger') return render_template(template_name, inventory_id=inventory_id, form=form, **document_info) # 表单校验成功 current_time = datetime.utcnow() # 获取产品信息 production_info = get_production_row_by_id(form.production_id.data) if not production_info: abort(404, exceptions.NotFound(description='production')) # 获取仓库信息 warehouse_info = get_warehouse_row_by_id(form.warehouse_id.data) if not warehouse_info: abort(404, exceptions.NotFound(description='warehouse')) # 获取货架信息 rack_info = get_rack_row_by_id(form.rack_id.data) if not rack_info: abort(404, exceptions.NotFound(description='rack')) inventory_data = { 'production_id': form.production_id.data, 'production_brand': production_info.production_brand, 'production_model': production_info.production_model, 'production_sku': production_info.production_sku, 'warehouse_id': form.warehouse_id.data, 'warehouse_name': warehouse_info.name, 'rack_id': form.rack_id.data, 'rack_name': rack_info.name, 'stock_qty_current': form.stock_qty.data, 'note': form.note.data, 'create_time': current_time, 'update_time': current_time, } result = edit_inventory(inventory_id, inventory_data) # 编辑操作成功 if result: flash(_('Edit Success'), 'success') return redirect( request.args.get('next') or url_for('inventory.lists')) # 编辑操作失败 else: flash(_('Edit Failure'), 'danger') return render_template(template_name, inventory_id=inventory_id, form=form, **document_info)