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
|
2020-10-06 17:01:29 +02:00
|
|
|
from django.db import models
|
2020-07-28 20:15:34 +02:00
|
|
|
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
|
2020-09-10 22:30:29 +02:00
|
|
|
from django.contrib.auth.models import User
|
2020-10-06 17:01:29 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2021-04-10 08:23:36 +02:00
|
|
|
|
|
|
|
from lostplaces import widgets
|
2020-12-24 20:31:52 +01:00
|
|
|
from lostplaces.models import Place, PlaceImage, Voucher, Explorer
|
2020-07-28 20:15:34 +02:00
|
|
|
|
2020-12-24 20:25:05 +01:00
|
|
|
class SignupVoucherForm(UserCreationForm):
|
2020-08-05 18:57:09 +02:00
|
|
|
class Meta:
|
2020-09-10 22:30:29 +02:00
|
|
|
model = User
|
2020-12-24 19:36:41 +01:00
|
|
|
fields = ('username', 'email', 'first_name', 'last_name')
|
2020-09-10 00:25:44 +02:00
|
|
|
voucher = forms.CharField(
|
|
|
|
max_length=30,
|
2020-10-06 17:01:29 +02:00
|
|
|
help_text=_('The Voucher you got from an administrator')
|
2020-10-11 07:13:16 +02:00
|
|
|
)
|
2020-08-05 18:57:09 +02:00
|
|
|
|
|
|
|
def is_valid(self):
|
|
|
|
super().is_valid()
|
2020-09-21 21:37:28 +02:00
|
|
|
submitted_voucher = self.cleaned_data.get('voucher')
|
2020-08-05 18:57:09 +02:00
|
|
|
try:
|
2020-09-21 21:37:28 +02:00
|
|
|
fetched_voucher = Voucher.objects.get(code=submitted_voucher)
|
2020-08-05 18:57:09 +02:00
|
|
|
except Voucher.DoesNotExist:
|
2020-10-11 01:39:38 +02:00
|
|
|
self.add_error('voucher', _('Invalid voucher'))
|
2020-08-05 18:57:09 +02:00
|
|
|
return False
|
|
|
|
|
2020-10-04 15:42:06 +02:00
|
|
|
if not fetched_voucher.valid:
|
2020-10-11 07:15:33 +02:00
|
|
|
self.add_error('voucher', _('Expired voucher'))
|
2020-09-18 21:02:45 +02:00
|
|
|
return False
|
|
|
|
|
2020-08-05 18:57:09 +02:00
|
|
|
fetched_voucher.delete()
|
|
|
|
return True
|
2020-08-02 23:22:47 +02:00
|
|
|
|
2020-12-25 01:31:01 +01:00
|
|
|
class ExplorerUserChangeForm(UserChangeForm):
|
2020-08-05 18:57:09 +02:00
|
|
|
class Meta:
|
2020-09-10 22:30:29 +02:00
|
|
|
model = User
|
2020-12-25 01:31:01 +01:00
|
|
|
fields = [ 'username', 'first_name', 'last_name', 'email' ]
|
2020-12-25 01:36:14 +01:00
|
|
|
''' Hide password hint.'''
|
2020-12-25 01:31:01 +01:00
|
|
|
password = None
|
|
|
|
|
2020-12-25 11:14:54 +01:00
|
|
|
''' Display username, but display it non-editable and not required. '''
|
2020-12-25 01:31:01 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields['username'].required = False
|
|
|
|
self.fields['username'].help_text = None
|
|
|
|
self.fields['username'].widget.attrs['disabled'] = 'disabled'
|
2020-12-25 12:24:09 +01:00
|
|
|
|
|
|
|
def clean_username(self):
|
|
|
|
# As shown in the above answer.
|
|
|
|
instance = getattr(self, 'instance', None)
|
|
|
|
if instance:
|
|
|
|
return instance.username
|
|
|
|
else:
|
|
|
|
return self.cleaned_data.get('username', None)
|
2020-12-25 01:31:01 +01:00
|
|
|
|
|
|
|
class ExplorerChangeForm(forms.ModelForm):
|
2020-12-24 20:31:52 +01:00
|
|
|
class Meta:
|
|
|
|
model = Explorer
|
|
|
|
fields = '__all__'
|
2021-10-01 21:14:53 +02:00
|
|
|
exclude = ['user', 'favorite_places', 'level']
|
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__'
|
2021-10-01 21:14:53 +02:00
|
|
|
exclude = ['submitted_by', 'level']
|
2021-04-10 08:23:36 +02:00
|
|
|
widgets = {
|
|
|
|
'hero': widgets.SelectContent()
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if 'instance' in kwargs:
|
|
|
|
self.fields['hero'].queryset = PlaceImage.objects.filter(place=kwargs['instance'])
|
|
|
|
self.fields['hero'].widget.attrs['item_template'] = 'partials/select_place_image_item.html'
|
2020-12-24 19:47:17 +01:00
|
|
|
|
2021-04-04 16:56:18 +02:00
|
|
|
latitude = forms.DecimalField(
|
|
|
|
widget=forms.NumberInput(attrs={'min':-90,'max': 90,'type': 'number', 'step': 'any'})
|
2020-12-24 19:47:17 +01:00
|
|
|
)
|
|
|
|
|
2021-04-04 16:56:18 +02:00
|
|
|
longitude = forms.DecimalField(
|
|
|
|
widget=forms.NumberInput(attrs={'min':-180,'max': 180,'type': 'number', 'step': 'any'})
|
|
|
|
)
|
|
|
|
|
2020-09-18 23:50:25 +02:00
|
|
|
class PlaceImageForm(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
|
2021-04-10 08:23:36 +02:00
|
|
|
|
|
|
|
class PlaceSetHeroForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Place
|
|
|
|
fields = ['hero']
|
|
|
|
widgets = {
|
|
|
|
'hero': widgets.SelectContent()
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields['hero'].queryset = PlaceImage.objects.filter(place=kwargs['instance'])
|
|
|
|
self.fields['hero'].widget.attrs['item_template'] = 'partials/select_place_image_item.html'
|
|
|
|
|
|
|
|
|
2020-08-30 18:39:45 +02:00
|
|
|
|
|
|
|
class TagSubmitForm(forms.Form):
|
2021-04-10 08:23:36 +02:00
|
|
|
tag_list = forms.CharField(
|
2020-09-02 00:08:00 +02:00
|
|
|
max_length=500,
|
|
|
|
required=False,
|
|
|
|
widget=forms.TextInput(attrs={'autocomplete':'off'})
|
2020-09-10 00:32:56 +02:00
|
|
|
)
|