44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
from django import forms
|
|
|
|
from .models import Sale, Article, Account
|
|
|
|
class SaleForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Sale
|
|
fields = ['price',]
|
|
|
|
class ChooseArticleForm(forms.Form):
|
|
article = forms.ModelChoiceField(
|
|
Article.objects.all(),
|
|
label="Article"
|
|
)
|
|
quantity = forms.DecimalField(
|
|
min_value=0,
|
|
label="Quantité (g)"
|
|
)
|
|
|
|
def to_sale(self):
|
|
"""Create a sale (with negative price) from the form."""
|
|
sale = Sale()
|
|
article = self.cleaned_data['article']
|
|
quantity = self.cleaned_data['quantity']
|
|
sale.price = - quantity * article.price
|
|
sale.title = "Vente de {}g de {}".format(
|
|
quantity,
|
|
article.name
|
|
)
|
|
return sale
|
|
|
|
class AccountForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Account
|
|
fields = ['firstname', 'lastname', 'email']
|
|
|
|
class SearchForm(forms.Form):
|
|
search = forms.CharField(max_length=255)
|
|
|
|
class ArticleForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Article
|
|
fields = '__all__'
|