2020-07-31 14:55:26 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
''' (web)forms that can be used elsewhere. '''
|
|
|
|
|
2020-07-28 20:15:34 +02:00
|
|
|
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
|
2020-07-28 20:15:34 +02:00
|
|
|
|
|
|
|
class ExplorerCreationForm(UserCreationForm):
|
2020-08-05 18:57:09 +02:00
|
|
|
class Meta:
|
|
|
|
model = Explorer
|
|
|
|
fields = ('username', 'email')
|
2020-09-10 00:25:44 +02:00
|
|
|
voucher = forms.CharField(
|
|
|
|
max_length=30,
|
|
|
|
help_text='The Voucher you got from an administrator'
|
|
|
|
)
|
2020-08-05 18:57:09 +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:
|
|
|
|
self.add_error('voucher', 'Invalid voucher')
|
|
|
|
return False
|
|
|
|
|
|
|
|
fetched_voucher.delete()
|
|
|
|
return True
|
2020-08-02 23:22:47 +02:00
|
|
|
|
2020-07-28 20:15:34 +02:00
|
|
|
class ExplorerChangeForm(UserChangeForm):
|
2020-08-05 18:57:09 +02:00
|
|
|
class Meta:
|
|
|
|
model = Explorer
|
|
|
|
fields = ('username', 'email')
|
2020-07-29 20:22:04 +02:00
|
|
|
|
|
|
|
class PlaceForm(forms.ModelForm):
|
2020-08-05 18:57:09 +02:00
|
|
|
class Meta:
|
|
|
|
model = Place
|
|
|
|
fields = '__all__'
|
|
|
|
exclude = ['submitted_by']
|
|
|
|
|
2020-07-30 13:09:00 +02:00
|
|
|
class PlaceImageCreateForm(forms.ModelForm):
|
2020-08-05 18:57:09 +02:00
|
|
|
class Meta:
|
|
|
|
model = PlaceImage
|
|
|
|
fields = ['filename']
|
|
|
|
widgets = {
|
|
|
|
'filename': forms.ClearableFileInput(attrs={'multiple': True})
|
|
|
|
}
|
2020-07-30 22:18:55 +02:00
|
|
|
|
2020-08-05 18:57:09 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2020-08-04 20:04:46 +02:00
|
|
|
|
2020-08-05 18:57:09 +02:00
|
|
|
self.fields['filename'].required = False
|
2020-08-30 18:39:45 +02:00
|
|
|
|
|
|
|
class TagSubmitForm(forms.Form):
|
2020-09-02 00:08:00 +02:00
|
|
|
tag_list = forms.CharField(
|
|
|
|
max_length=500,
|
|
|
|
required=False,
|
|
|
|
widget=forms.TextInput(attrs={'autocomplete':'off'})
|
2020-09-10 00:32:56 +02:00
|
|
|
)
|