forms.py 3.4 KB

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