web_AI-5/django/didgeridoo/webshop/forms.py

88 lines
2.7 KiB
Python
Raw Normal View History

2017-12-29 16:46:21 +01:00
from django import forms
2018-02-11 19:58:49 +01:00
from webshop.models import (
Salutation,
City,
Picture,
Article,
Option,
2018-02-18 19:31:03 +01:00
CartPosition
2018-02-11 19:58:49 +01:00
)
2017-12-29 16:46:21 +01:00
class RegistrationForm(forms.Form):
email = forms.EmailField()
salutation = forms.ModelChoiceField(queryset=Salutation.objects.all())
first_name = forms.CharField()
last_name = forms.CharField()
street_name = forms.CharField()
2018-02-04 20:47:18 +01:00
street_number = forms.CharField(max_length=4)
2017-12-29 16:46:21 +01:00
zip_code = forms.IntegerField(min_value=1000, max_value=9999)
city = forms.CharField()
def clean_city(self):
# Check that the two password entries match
city = self.cleaned_data['city']
zip_code = self.cleaned_data['zip_code']
try:
City.objects.get(name=city, zip_code=zip_code)
except City.DoesNotExist:
raise forms.ValidationError(
"The zip code and the city don't match.")
return city
class PictureForm(forms.ModelForm):
def max_pictures(self):
try:
option = Option.objects.get(name='max_pictures')
if option.enabled:
return option.value
else:
return False
except:
return False
def count_pictures(self, _article):
count = Picture.objects.filter(article=_article.id).count()
return count
def clean(self):
article = self.cleaned_data.get('article')
print(self.max_pictures())
if self.max_pictures():
if (self.count_pictures(article) >= self.max_pictures()):
raise forms.ValidationError("Only " + str(self.max_pictures())
+ " pictures per article allowed.")
return self.cleaned_data
class Meta:
model = Picture
fields = ['name', 'article', 'image']
2018-02-01 17:02:08 +01:00
class AddToCartForm(forms.Form):
amount = forms.IntegerField(
label='Amount in piece.',
help_text="Enter a Value between 1 and 99.",
initial=1)
2018-02-11 19:58:49 +01:00
class CartForm(forms.Form):
2018-02-17 10:51:32 +01:00
print('CartForm')
2018-02-18 21:58:46 +01:00
def ChangeAmount(self, _cart_id, _article_id):
2018-02-17 10:51:32 +01:00
print('CartForm.ChangeAmount')
2018-02-18 21:58:46 +01:00
article = CartPosition.objects.get(cart=_cart_id, article=_article_id)
2018-02-11 19:58:49 +01:00
amountfield = forms.IntegerField(
label='pce',
help_text='Enter a Value between 1 and 99.',
initial=article.amount
)
return amountfield
2018-02-17 10:51:32 +01:00
class CheckoutForm(forms.Form):
checkout = forms.BooleanField(
required=True,
label='Yes. I have read the General Terms and Conditions.')