forms.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from django import forms
  2. from .models import Contrib
  3. ORIENTATIONS = (
  4. ('N', 'Nord'),
  5. ('NO', 'Nord-Ouest'),
  6. ('O', 'Ouest'),
  7. ('SO', 'Sud-Ouest'),
  8. ('S', 'Sud'),
  9. ('SE', 'Sud-Est'),
  10. ('E', 'Est'),
  11. ('NE', 'Nord-Est'),
  12. )
  13. class PublicContribForm(forms.ModelForm):
  14. class Meta:
  15. model = Contrib
  16. fields = [
  17. 'name', 'contrib_type',
  18. 'latitude', 'longitude',
  19. 'phone', 'email',
  20. 'comment',
  21. 'access_type',
  22. 'connect_local', 'connect_internet',
  23. 'floor', 'floor_total', 'orientations', 'roof',
  24. 'comment',
  25. 'privacy_name', 'privacy_email', 'privacy_coordinates',
  26. 'privacy_place_details', 'privacy_comment',
  27. ]
  28. widgets = {
  29. 'contrib_type': forms.RadioSelect,
  30. 'latitude': forms.HiddenInput,
  31. 'longitude': forms.HiddenInput,
  32. 'access_type': forms.RadioSelect,
  33. 'connect_local': forms.CheckboxInput,
  34. 'connect_internet': forms.CheckboxInput,
  35. 'comment': forms.Textarea({'rows': 3}),
  36. 'floor': forms.TextInput(
  37. attrs={'placeholder': "Étage (0 pour RDC)"}),
  38. 'floor_total': forms.TextInput(
  39. attrs={'placeholder': "Nb. d'étages du bâtiment"}),
  40. }
  41. # Widget rendering is managed by hand in template for orientions.
  42. orientations = forms.MultipleChoiceField(choices=ORIENTATIONS)
  43. _privacy_fieldnames = (
  44. 'privacy_name', 'privacy_email', 'privacy_coordinates',
  45. 'privacy_place_details', 'privacy_comment',
  46. )
  47. def _validate_contact_information(self, data):
  48. if (data.get('phone') == '') and (data.get('email') == ''):
  49. msg = 'Il faut remplir un des deux champs "téléphone" ou "email".'
  50. self.add_error('phone', msg)
  51. self.add_error('email', msg)
  52. def _validate_floors(self, data):
  53. if None in (data.get('floor'), data.get('floor_total')):
  54. return
  55. if (data.get('floor') > data.get('floor_total')):
  56. self.add_error(
  57. 'floor',
  58. "L'étage doit être inférieur ou égal au nombre d'étages",
  59. )
  60. def _validate_share_fields(self, data):
  61. if data.get('contrib_type') == Contrib.CONTRIB_SHARE:
  62. if data.get('access_type') == '':
  63. self.add_error('access_type', 'Ce champ est requis')
  64. def clean(self):
  65. cleaned_data = super().clean()
  66. self._validate_contact_information(cleaned_data)
  67. self._validate_floors(cleaned_data)
  68. self._validate_share_fields(cleaned_data)
  69. return cleaned_data
  70. def privacy_fields(self):
  71. for i in self._privacy_fieldnames:
  72. field = self[i]
  73. # FIXME: What a hack
  74. field.label = field.label\
  75. .replace('public', '')\
  76. .replace('publiques', '')
  77. yield field
  78. def __init__(self, *args, **kwargs):
  79. super(PublicContribForm, self).__init__(*args, **kwargs)
  80. for f in ['latitude', 'longitude']:
  81. self.fields[f].error_messages['required'] = "Veuillez déplacer le curseur à l'endroit où vous voulez partager/accéder au service"