lostplaces-backend/lostplaces/lostplaces_app/forms.py

50 lines
1.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
''' (web)forms that can be used elsewhere. '''
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
2020-08-01 13:11:07 +02:00
from .models import Explorer, Place, PlaceImage, Voucher
class ExplorerCreationForm(UserCreationForm):
class Meta:
model = Explorer
fields = ('username', 'email')
2020-08-03 19:14:48 +02:00
voucher = forms.CharField(max_length=10, help_text='The Voucher you got from an administrator')
2020-08-02 23:22:47 +02:00
def is_valid(self):
super().is_valid()
sumitted_voucher = self.cleaned_data.get('voucher')
try:
fetched_voucher = Voucher.objects.get(code=sumitted_voucher)
except Voucher.DoesNotExist:
return False
fetched_voucher.delete()
return True
class ExplorerChangeForm(UserChangeForm):
class Meta:
model = Explorer
fields = ('username', 'email')
class PlaceForm(forms.ModelForm):
class Meta:
model = Place
fields = '__all__'
exclude = ['submitted_by']
class PlaceImageCreateForm(forms.ModelForm):
class Meta:
model = PlaceImage
fields = ['filename']
widgets = {
2020-07-30 22:33:41 +02:00
'filename': forms.ClearableFileInput(attrs={'multiple': True})
}
2020-07-30 22:18:55 +02:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
print(self.fields)
self.fields['filename'].required = False