1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- from django import forms
- from .models import Contrib
- ORIENTATIONS = (
- ('N', 'Nord'),
- ('NO', 'Nord-Ouest'),
- ('O', 'Ouest'),
- ('SO', 'Sud-Ouest'),
- ('S', 'Sud'),
- ('SE', 'Sud-Est'),
- ('E', 'Est'),
- ('NE', 'Nord-Est'),
- )
- class PublicContribForm(forms.ModelForm):
- class Meta:
- model = Contrib
- fields = [
- 'name', 'contrib_type',
- 'latitude', 'longitude',
- 'phone', 'email',
- 'comment',
- 'access_type',
- 'connect_local', 'connect_internet',
- 'bandwidth', 'share_part',
- 'floor', 'floor_total', 'orientations', 'roof',
- 'comment',
- 'privacy_name', 'privacy_email', 'privacy_coordinates',
- 'privacy_place_details', 'privacy_comment',
- ]
- widgets = {
- 'contrib_type': forms.RadioSelect,
- 'latitude': forms.HiddenInput,
- 'longitude': forms.HiddenInput,
- 'access_type': forms.RadioSelect,
- 'connect_local': forms.CheckboxInput,
- 'connect_internet': forms.CheckboxInput,
- 'comment': forms.Textarea({'rows': 3}),
- 'floor': forms.TextInput(
- attrs={'placeholder': "Étage (0 pour RDC)"}),
- 'floor_total': forms.TextInput(
- attrs={'placeholder': "Nb. d'étages du bâtiment"}),
- }
- # Widget rendering is managed by hand in template for orientions.
- orientations = forms.MultipleChoiceField(choices=ORIENTATIONS)
- _privacy_fieldnames = (
- 'privacy_name', 'privacy_email', 'privacy_coordinates',
- 'privacy_place_details', 'privacy_comment',
- )
- def _validate_contact_information(self, data):
- if (data.get('phone') == '') and (data.get('email') == ''):
- msg = 'Il faut remplir un des deux champs "téléphone" ou "email".'
- self.add_error('phone', msg)
- self.add_error('email', msg)
- def _validate_floors(self, data):
- if None in (data.get('floor'), data.get('floor_total')):
- return
- if (data.get('floor') > data.get('floor_total')):
- self.add_error(
- 'floor',
- "L'étage doit être inférieur ou égal au nombre d'étages",
- )
- def clean(self):
- cleaned_data = super().clean()
- self._validate_contact_information(cleaned_data)
- self._validate_floors(cleaned_data)
- return cleaned_data
- def privacy_fields(self):
- for i in self._privacy_fieldnames:
- field = self[i]
- # FIXME: What a hack
- field.label = field.label\
- .replace('public', '')\
- .replace('publiques', '')
- yield field
- def __init__(self, *args, **kwargs):
- super(PublicContribForm, self).__init__(*args, **kwargs)
- for f in ['latitude', 'longitude']:
- self.fields[f].error_messages['required'] = "Veuillez déplacer le curseur à l'endroit où vous voulez partager/accéder au service"
|