2016-05-10 20:20:01 +00:00
|
|
|
from django import forms
|
2019-03-27 21:53:23 +00:00
|
|
|
from django.forms import modelformset_factory
|
|
|
|
from shop.models import OrderProductRelation
|
2016-05-12 16:16:25 +00:00
|
|
|
|
2016-05-12 07:51:35 +00:00
|
|
|
|
2019-03-27 21:53:23 +00:00
|
|
|
class OrderProductRelationForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = OrderProductRelation
|
2019-03-29 21:19:49 +00:00
|
|
|
fields = ["quantity"]
|
2019-03-27 21:53:23 +00:00
|
|
|
|
|
|
|
def clean_quantity(self):
|
|
|
|
product = self.instance.product
|
2019-03-29 21:19:49 +00:00
|
|
|
new_quantity = self.cleaned_data["quantity"]
|
2019-03-27 21:53:23 +00:00
|
|
|
|
2019-03-29 14:21:55 +00:00
|
|
|
if product.stock_amount and product.left_in_stock < new_quantity:
|
2019-03-27 21:53:23 +00:00
|
|
|
raise forms.ValidationError(
|
2019-03-29 21:19:49 +00:00
|
|
|
"Only {} left in stock.".format(product.left_in_stock)
|
2019-03-27 21:53:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return new_quantity
|
|
|
|
|
|
|
|
|
|
|
|
OrderProductRelationFormSet = modelformset_factory(
|
2019-03-29 21:19:49 +00:00
|
|
|
OrderProductRelation, form=OrderProductRelationForm, extra=0
|
2019-03-27 21:53:23 +00:00
|
|
|
)
|