forms.py 3.4 KB

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