Error message in the form when the voucher is invalid

This commit is contained in:
reverend 2020-08-05 10:15:51 +02:00
parent 28d09c968a
commit 8f9e1b5877

View File

@ -8,42 +8,43 @@ from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import Explorer, Place, PlaceImage, Voucher from .models import Explorer, Place, PlaceImage, Voucher
class ExplorerCreationForm(UserCreationForm): class ExplorerCreationForm(UserCreationForm):
class Meta: class Meta:
model = Explorer model = Explorer
fields = ('username', 'email') fields = ('username', 'email')
voucher = forms.CharField(max_length=10, help_text='The Voucher you got from an administrator') voucher = forms.CharField(max_length=10, help_text='The Voucher you got from an administrator')
def is_valid(self): def is_valid(self):
super().is_valid() super().is_valid()
sumitted_voucher = self.cleaned_data.get('voucher') sumitted_voucher = self.cleaned_data.get('voucher')
try: try:
fetched_voucher = Voucher.objects.get(code=sumitted_voucher) fetched_voucher = Voucher.objects.get(code=sumitted_voucher)
except Voucher.DoesNotExist: except Voucher.DoesNotExist:
return False self.add_error('voucher', 'Invalid voucher')
return False
fetched_voucher.delete() fetched_voucher.delete()
return True return True
class ExplorerChangeForm(UserChangeForm): class ExplorerChangeForm(UserChangeForm):
class Meta: class Meta:
model = Explorer model = Explorer
fields = ('username', 'email') fields = ('username', 'email')
class PlaceForm(forms.ModelForm): class PlaceForm(forms.ModelForm):
class Meta: class Meta:
model = Place model = Place
fields = '__all__' fields = '__all__'
exclude = ['submitted_by'] exclude = ['submitted_by']
class PlaceImageCreateForm(forms.ModelForm): class PlaceImageCreateForm(forms.ModelForm):
class Meta: class Meta:
model = PlaceImage model = PlaceImage
fields = ['filename'] fields = ['filename']
widgets = { widgets = {
'filename': forms.ClearableFileInput(attrs={'multiple': True}) 'filename': forms.ClearableFileInput(attrs={'multiple': True})
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fields['filename'].required = False self.fields['filename'].required = False