def test_adjustment_form(rf, admin_user): """ Test StockAdjustmentForm. """ partial_sales_unit = SalesUnit.objects.create( identifier="test-sales-partial", decimals=2, name="Partial unit") form = StockAdjustmentForm( data={ "purchase_price": 10, "delta": 1.2, }, sales_unit=partial_sales_unit, ) assert form.is_valid() integer_sales_unit = SalesUnit.objects.create( identifier="test-sales-integer", decimals=0, name="Integer unit") form = StockAdjustmentForm( data={ "purchase_price": 10, "delta": 1.2, }, sales_unit=integer_sales_unit, ) assert not form.is_valid()
def process_stock_adjustment(request, supplier_id, product_id): try: if request.method != "POST": return JsonResponse({}, status=405) product = Product.objects.select_related("sales_unit").filter( pk=product_id).first() form = StockAdjustmentForm(data=request.POST, sales_unit=product.sales_unit) if form.is_valid(): return _process_stock_adjustment(form, request, supplier_id, product_id) errors = form.non_field_errors() if form.errors: for field, field_errors in form.errors.items(): errors.append(f"{field}: {','.join(field_errors)}") error_message = ugettext( "Please check submitted values and try again ({}).").format( ", ".join(errors)) return JsonResponse({"message": error_message}, status=400) except Exception as exc: error_message = ugettext( "Please check submitted values and try again (%(error)s).") % { "error": exc } return JsonResponse({"message": error_message}, status=400)
def process_stock_adjustment(request, supplier_id, product_id): try: if request.method != "POST": raise Exception(_("Not allowed")) form = StockAdjustmentForm(request.POST) if form.is_valid(): data = form.cleaned_data supplier = Supplier.objects.get(id=supplier_id) stock_adjustment = supplier.module.adjust_stock( product_id, delta=data.get("delta"), purchase_price=data.get("purchase_price"), created_by=request.user ) success_message = { "stockInformationDiv": "#%s" % get_stock_information_div_id( stock_adjustment.supplier, stock_adjustment.product), "updatedStockInformation": get_stock_information_html( stock_adjustment.supplier, stock_adjustment.product), "message": get_adjustment_success_message(stock_adjustment) } return JsonResponse(success_message, status=200) error_message = ugettext("Error, please check submitted values and try again.") return JsonResponse({"message": error_message}, status=400) except Exception as exc: error_message = ugettext( "Error, please check submitted values and try again (%(error)s).") % {"error": exc} return JsonResponse({"message": error_message}, status=400)
def process_stock_adjustment(request, supplier_id, product_id): try: if request.method != "POST": raise Exception(_("Not allowed")) form = StockAdjustmentForm(request.POST) if form.is_valid(): data = form.cleaned_data supplier = Supplier.objects.get(id=supplier_id) stock_adjustment = supplier.module.adjust_stock( product_id, delta=data.get("delta"), purchase_price=data.get("purchase_price"), created_by=request.user) success_message = { "stockInformationDiv": "#%s" % get_stock_information_div_id(stock_adjustment.supplier, stock_adjustment.product), "updatedStockInformation": get_stock_information_html(stock_adjustment.supplier, stock_adjustment.product), "message": get_adjustment_success_message(stock_adjustment) } return JsonResponse(success_message, status=200) error_message = ugettext( "Error, please check submitted values and try again.") return JsonResponse({"message": error_message}, status=400) except Exception as exc: error_message = ugettext( "Error, please check submitted values and try again (%(error)s)." ) % { "error": exc } return JsonResponse({"message": error_message}, status=400)