def classification_modify(request): """添加和更改相应的物品分类""" class_name = request.GET.get("className", '') form = forms.ClassificationForm() if class_name: classification = get_object_or_404(models.Classification, class_name=class_name) form = forms.ClassificationForm(instance=classification) if request.method == "POST": pk_id = None origin_classification = None action = History.CREATE # 记录操作动作, 初始默认是创建 origin_class_name = request.POST.get("origin_class_name", '') # 如果有origin_class_name, 那么是修改操作 if origin_class_name: try: action = History.MODIFY change_class_name = request.POST.get("class_name") if origin_class_name != change_class_name: is_exist_classification = models.Classification.objects.filter( class_name=change_class_name).exists() if is_exist_classification: raise AssertionError( "classification objects with %s has aready existed." % change_class_name) origin_classification = models.Classification.objects.get( class_name=origin_class_name) pk_id = origin_classification.id # 保留对象的pk, 以免关联的表出错 origin_classification.delete() except AssertionError: return JsonResponse( {"back_msg": "%s 种类名已存在." % change_class_name}) except: return JsonResponse({"back_msg": "源数据取出失败."}) # 传递表单数据, 并开始验证保存 form = forms.ClassificationForm(request.POST) if form.is_valid(): new_classification = form.save(commit=False) if pk_id: new_classification.id = pk_id new_classification.save() # NOTE: 向history中存入记录 new_history = History.set_record(cur_user=request.user, model=new_classification, act=action) new_history.save() return JsonResponse({"back_msg": OK}) else: if origin_classification: origin_classification.save() # 恢复原始数据删除 full_msg = form_errors(form) return JsonResponse({"back_msg": full_msg}) return render(request, "app_warehouse/classification_change.html", { "head_title": "编辑物品分类", "active_navbar": "classification", "form": form })
def supplier_modify(request): """供应商添加和修改""" supplier_id = request.GET.get("supplierId", '') form = forms.SupplierForm() if supplier_id: supplier = get_object_or_404(models.Supplier, supplier_id=supplier_id) form = forms.SupplierForm(instance=supplier) if request.method == "POST": pk_id = None origin_supplier = None action = History.CREATE # 记录操作动作, 初始默认是创建 origin_supplier_id = request.POST.get("origin_supplier_id", '') # 如果有origin_supplier_id, 那么是修改操作 if origin_supplier_id: try: action = History.MODIFY change_supplier_id = request.POST.get("supplier_id") if origin_supplier_id != change_supplier_id: is_exsit_supplier = models.Supplier.objects.filter( supplier_id=change_supplier_id).exists() # 取保更改的唯一性 if is_exsit_supplier: raise AssertionError( "supplier objects with %s has already existed." % change_supplier_id) origin_supplier = models.Supplier.objects.get( supplier_id=origin_supplier_id) pk_id = origin_supplier.id # 保留对象的pk, 以免关联的表出错 origin_supplier.delete() except AssertionError: return JsonResponse( {"back_msg": "%s 供应商编号已存在." % change_supplier_id}) except: return JsonResponse({"back_msg": "源数据取出失败."}) # 传递表单数据, 并开始验证保存 form = forms.SupplierForm(request.POST) if form.is_valid(): new_supplier = form.save(commit=False) if pk_id: new_supplier.id = pk_id new_supplier.save() # NOTE: 向history中存入记录 new_history = History.set_record(cur_user=request.user, model=new_supplier, act=action) new_history.save() return JsonResponse({"back_msg": OK}) else: if origin_supplier: origin_supplier.save() # 恢复原始数据的删除 full_msg = form_errors(form) return JsonResponse({"back_msg": full_msg}) return render(request, "app_warehouse/supplier_change.html", { "head_title": "编辑供应商", "active_navbar": "supplier", "form": form })
def warehouse_modify(request): """在库货物添加和修改""" good_id = request.GET.get("goodId", '') form = forms.WarehouseForm() if good_id: warehouse = get_object_or_404(models.Warehouse, good_id=good_id) form = forms.WarehouseForm(instance=warehouse) if request.method == "POST": origin_good_id = request.POST.get("origin_good_id", '') # 只能对现有的库存进行修改, 不能创建, 这里必须要有origin_good_id if origin_good_id: try: action = History.MODIFY change_good_id = request.POST.get("good_id") if origin_good_id != change_good_id: is_exist_warehouse = models.Warehouse.objects.filter( good_id=change_good_id).exists() # 保证不和数据库中已有唯一冲突 if is_exist_warehouse: raise AssertionError( "warehouse objetcts with %s has aready existed." % change_good_id) origin_warehouse = models.Warehouse.objects.get( good_id=origin_good_id) except AssertionError: return JsonResponse( {"back_msg": "%s 储物编号已经存在." % change_good_id}) except Exception: return JsonResponse({"back_msg": "源数据取出失败."}) else: return JsonResponse({"back_msg": "未获取到储物编号."}) # 确保必须要有原始的储物数据 pk_id = origin_warehouse.id # 保留更改对象的pk, 以免关联的表出错 origin_warehouse.delete() # 开始验证并保存表单 form = forms.WarehouseForm(request.POST) if form.is_valid(): new_warehouse = form.save(commit=False) new_warehouse.id = pk_id new_warehouse.amount = int( new_warehouse.amount) if int(new_warehouse.amount) >= 0 else 0 new_warehouse.save() # NOTE: 向history中存入记录 new_history = History.set_record(cur_user=request.user, model=new_warehouse, act=action) new_history.save() return JsonResponse({"back_msg": OK}) else: origin_warehouse.save() # 恢复原先删除 full_msg = form_errors(form) return JsonResponse({"back_msg": full_msg}) return render(request, "app_warehouse/warehouse_change.html", { "head_title": "编辑在库货物", "active_navbar": "warehouse", "form": form })
def in_warehouse_modify(request): """入库单添加和修改""" the_id = request.GET.get("id", '') form = forms.InWarehouseForm() if the_id: inwarehouse = get_object_or_404(models.InWarehouse, id=the_id) form = forms.InWarehouseForm(instance=inwarehouse) if request.method == "POST": pk_id = None origin_inwarehouse = None origin_id = request.POST.get("origin_id", '') # 如果有origin_id, 那么是修改 if origin_id: try: origin_inwarehouse = models.InWarehouse.objects.get( id=origin_id) if origin_inwarehouse.is_finished: raise AssertionError("order have finished, %s" % origin_id) # 已经完成的订单不能修改 pk_id = origin_inwarehouse.id # 保留更改对象的pk, 以免关联的表出错 origin_inwarehouse.delete() except AssertionError: return JsonResponse( {"back_msg": "%s 入库单已完成, 不能更改." % origin_inwarehouse}) except Exception: return JsonResponse({"back_msg": "源数据取出失败."}) # 开始接受表单数据, 并验证, 保存 form = forms.InWarehouseForm(request.POST) if form.is_valid(): new_inwarehouse = form.save(commit=False) if pk_id: new_inwarehouse.id = pk_id new_inwarehouse.in_amount = int(new_inwarehouse.in_amount) if int( new_inwarehouse.in_amount) >= 0 else 0 new_inwarehouse.save() # NOTE: 向history中存入记录 new_history = History.set_record(cur_user=request.user, model=new_inwarehouse, act=History.MODIFY) new_history.save() return JsonResponse({"back_msg": OK}) else: if origin_inwarehouse: origin_inwarehouse.save() # 恢复原先删除 full_msg = form_errors(form) return JsonResponse({"back_msg": full_msg}) return render(request, "app_warehouse/in_warehouse_change.html", { "head_title": "编辑入库单", "active_navbar": "inwarehouse", "form": form })
def warehouse_delete(request): """储物删除""" good_id = request.POST.get("goodId", '') try: warehouse = models.Warehouse.objects.get(good_id=good_id) except: return JsonResponse({"back_msg": "数据库出错, 未能正确删除内容."}) warehouse.is_deleted = True warehouse.good_id = str( warehouse.supplier_id) + "(DELETED %s)" % datetime.datetime.now( TZ).strftime("%Y-%m-%d %H:%M:%S %f") # 避免删除后对添加或修改会有唯一性冲突 warehouse.save() # NOTE: history中存入记录 new_history = History.set_record(cur_user=request.user, model=warehouse, act=History.DELETE) new_history.save() return JsonResponse({"back_msg": OK})
def supplier_delete(request): """供应商删除""" supplier_id = request.POST.get("supplierId", '') try: supplier = models.Supplier.objects.get(supplier_id=supplier_id) except: return JsonResponse({"back_msg": "数据库出错, 未能正确删除内容."}) supplier.is_deleted = True supplier.supplier_id = str( supplier.supplier_id) + "(DELETED %s)" % datetime.datetime.now( TZ).strftime("%Y-%m-%d %H:%M:%S %f") # 避免删除后对添加或修改会有唯一性冲突 supplier.save() # NOTE: 向history中存入记录 new_history = History.set_record(cur_user=request.user, model=supplier, act=History.DELETE) new_history.save() return JsonResponse({"back_msg": OK})
def classification_delete(request): """物品分类删除""" class_name = request.POST.get("className", '') try: classification = models.Classification.objects.get( class_name=class_name) except Exception as er: logging.error(er) return JsonResponse({"back_msg": "数据库出错, 未能正确删除内容."}) classification.is_deleted = True classification.class_name = str( classification.class_name) + "(DELETED %s)" % datetime.datetime.now( TZ).strftime("%Y-%m-%d %H:%M:%S %f") # 避免删除后对添加或修改内容时会有唯一性冲突 classification.save() # NOTE: 向history中存入记录 new_history = History.set_record(cur_user=request.user, model=classification, act=History.DELETE) new_history.save() return JsonResponse({"back_msg": OK})