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):
|
2019-03-27 22:34:44 +00:00
|
|
|
|
2019-03-27 21:53:23 +00:00
|
|
|
class Meta:
|
|
|
|
model = OrderProductRelation
|
|
|
|
fields = ['quantity']
|
|
|
|
|
|
|
|
def clean_quantity(self):
|
|
|
|
product = self.instance.product
|
|
|
|
new_quantity = self.cleaned_data['quantity']
|
|
|
|
|
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(
|
|
|
|
"Only {} left in stock.".format(
|
|
|
|
product.left_in_stock,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return new_quantity
|
|
|
|
|
|
|
|
|
|
|
|
OrderProductRelationFormSet = modelformset_factory(
|
|
|
|
OrderProductRelation,
|
|
|
|
form=OrderProductRelationForm,
|
|
|
|
extra=0
|
|
|
|
)
|