68 lines
2.5 KiB
Python
68 lines
2.5 KiB
Python
"""File containing webapp forms."""
|
|
|
|
from django import forms
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from webapp.models import CustomKaehmyRole, PresetKaehmyRole
|
|
from webapp.models import OhlhafvChallenge, KaehmyForm, KaehmyMessage
|
|
|
|
|
|
class KaehmyForm_Form(forms.ModelForm):
|
|
"""Class representing Kaehmy form."""
|
|
|
|
class Meta:
|
|
"""Meta for class KaehmyForm."""
|
|
|
|
model = KaehmyForm
|
|
fields = ['name', 'email', 'phone_number', 'year',
|
|
'preset_roles', 'custom_roles', 'custom_role_name',
|
|
'custom_role_is_board', 'text']
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(KaehmyForm_Form, self).__init__(*args, **kwargs)
|
|
|
|
custom_roles_exist = CustomKaehmyRole.objects.all().exists()
|
|
self.fields["custom_roles"].widget = forms.widgets.CheckboxSelectMultiple() if custom_roles_exist else forms.HiddenInput()
|
|
self.fields["custom_roles"].help_text = ""
|
|
self.fields["custom_roles"].label = _('Custom roles')
|
|
self.fields["custom_roles"].queryset = CustomKaehmyRole.objects.all()
|
|
self.fields["preset_roles"].widget = forms.widgets.CheckboxSelectMultiple(attrs={'title': _('Preset roles')})
|
|
self.fields["preset_roles"].help_text = ""
|
|
self.fields["preset_roles"].queryset = PresetKaehmyRole.objects.order_by('category')
|
|
self.fields["preset_roles"].label = _('Preset roles')
|
|
|
|
def clean_phone_number(self):
|
|
"""Clean phone number field."""
|
|
number = self.cleaned_data.get('phone_number')
|
|
if number.isdigit():
|
|
return number
|
|
else:
|
|
raise ValidationError(_('Invalid phone number'))
|
|
|
|
def clean_custom_role_name(self):
|
|
"""Check that no other custom role with same name exists."""
|
|
custom_name = self.cleaned_data.get('custom_role_name')
|
|
if not CustomKaehmyRole.objects.filter(name=custom_name).exists():
|
|
return custom_name
|
|
else:
|
|
raise ValidationError(_('Custom role with the same name already exists.'))
|
|
|
|
|
|
class KaehmyCommentForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
model = KaehmyMessage
|
|
fields = ['name', 'email', 'message', 'parent']
|
|
|
|
|
|
class OhlhafvForm(forms.ModelForm):
|
|
"""Class representing Ohlhafv form."""
|
|
|
|
class Meta:
|
|
"""Meta class for Ohlhafv form."""
|
|
|
|
model = OhlhafvChallenge
|
|
fields = ['challenger', 'challenger_email',
|
|
'victim', 'victim_email', 'series', 'message']
|