#!/usr/bin/env python # -*- coding: utf-8 -*- ''' (web)forms that can be used elsewhere. ''' from django import forms from django.db import models from django.contrib.auth.forms import UserCreationForm, UserChangeForm from django.contrib.auth.models import User from django.utils.translation import gettext as _ from lostplaces import widgets from lostplaces.models import Place, PlaceImage, Voucher, Explorer class SignupVoucherForm(UserCreationForm): class Meta: model = User fields = ('username', 'email', 'first_name', 'last_name') voucher = forms.CharField( max_length=30, help_text=_('The Voucher you got from an administrator') ) def is_valid(self): super_result = super().is_valid() submitted_voucher = self.cleaned_data.get('voucher') try: fetched_voucher = Voucher.objects.get(code=submitted_voucher) except Voucher.DoesNotExist: self.add_error('voucher', _('Invalid voucher')) return False if not fetched_voucher.valid: self.add_error('voucher', _('Expired voucher')) return False fetched_voucher.delete() return super_result class ExplorerUserChangeForm(UserChangeForm): class Meta: model = User fields = [ 'username', 'first_name', 'last_name', 'email' ] ''' Hide password hint.''' password = None ''' Display username, but display it non-editable and not required. ''' 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' 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) class ExplorerChangeForm(forms.ModelForm): class Meta: model = Explorer fields = '__all__' exclude = ['user', 'favorite_places', 'level'] class PlaceForm(forms.ModelForm): class Meta: model = Place fields = '__all__' exclude = ['submitted_by', 'level', 'mode'] 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' latitude = forms.DecimalField( widget=forms.NumberInput(attrs={'min':-90,'max': 90,'type': 'number', 'step': 'any'}) ) longitude = forms.DecimalField( widget=forms.NumberInput(attrs={'min':-180,'max': 180,'type': 'number', 'step': 'any'}) ) draft = forms.BooleanField( label=_('Save Place as draft'), required=False ) class PlaceImageForm(forms.ModelForm): class Meta: model = PlaceImage fields = ['filename'] widgets = { 'filename': forms.ClearableFileInput(attrs={'multiple': True}) } def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['filename'].required = False 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' class TagSubmitForm(forms.Form): tag_list = forms.CharField( max_length=500, required=False, widget=forms.TextInput(attrs={'autocomplete':'off'}) )