forms.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. human_field = forms.CharField(required=False, widget=forms.HiddenInput)
  15. class Meta:
  16. model = Contrib
  17. fields = [
  18. 'name', 'contrib_type',
  19. 'latitude', 'longitude',
  20. 'phone', 'email',
  21. 'comment',
  22. 'access_type',
  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. 'comment': forms.Textarea({'rows': 3}),
  34. 'floor': forms.TextInput(
  35. attrs={'placeholder': "Étage (0 pour RDC)"}),
  36. 'floor_total': forms.TextInput(
  37. attrs={'placeholder': "Nb. d'étages du bâtiment"}),
  38. }
  39. # Widget rendering is managed by hand in template for orientions.
  40. orientations = forms.MultipleChoiceField(choices=ORIENTATIONS)
  41. _privacy_fieldnames = (
  42. 'privacy_name', 'privacy_email', 'privacy_coordinates',
  43. 'privacy_place_details', 'privacy_comment',
  44. )
  45. def _validate_contact_information(self, data):
  46. if (data.get('phone') == '') and (data.get('email') == ''):
  47. msg = 'Il faut remplir un des deux champs "téléphone" ou "email".'
  48. self.add_error('phone', msg)
  49. self.add_error('email', msg)
  50. def _validate_floors(self, data):
  51. if None in (data.get('floor'), data.get('floor_total')):
  52. return
  53. if (data.get('floor') > data.get('floor_total')):
  54. self.add_error(
  55. 'floor',
  56. "L'étage doit être inférieur ou égal au nombre d'étages",
  57. )
  58. def _validate_share_fields(self, data):
  59. if data.get('contrib_type') == Contrib.CONTRIB_SHARE:
  60. if data.get('access_type') == '':
  61. self.add_error('access_type', 'Ce champ est requis')
  62. def clean(self):
  63. cleaned_data = super().clean()
  64. self._validate_contact_information(cleaned_data)
  65. self._validate_floors(cleaned_data)
  66. self._validate_share_fields(cleaned_data)
  67. return cleaned_data
  68. def privacy_fields(self):
  69. for i in self._privacy_fieldnames:
  70. field = self[i]
  71. # FIXME: What a hack
  72. field.label = field.label\
  73. .replace('public', '')\
  74. .replace('publiques', '')
  75. yield field
  76. def __init__(self, *args, **kwargs):
  77. super(PublicContribForm, self).__init__(*args, **kwargs)
  78. for f in ['latitude', 'longitude']:
  79. self.fields[f].error_messages['required'] = "Veuillez déplacer le curseur à l'endroit où vous voulez partager/accéder au service"