Parcourir la source

Validate the existence of at least phone or email

Jocelyn Delande il y a 9 ans
Parent
commit
a3b349fe19

+ 11 - 0
wifiwithme/apps/contribmap/forms.py

@@ -53,6 +53,17 @@ class PublicContribForm(forms.ModelForm):
         '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 clean(self):
+        cleaned_data = super().clean()
+        self._validate_contact_information(cleaned_data)
+        return cleaned_data
+
     def privacy_fields(self):
         for i in self._privacy_fieldnames:
             field = self[i]

+ 36 - 0
wifiwithme/apps/contribmap/tests.py

@@ -5,6 +5,7 @@ from django.contrib.auth.models import User
 from django.test import TestCase, Client, override_settings
 
 from contribmap.models import Contrib
+from contribmap.forms import PublicContribForm
 
 
 class APITestClient(Client):
@@ -132,6 +133,41 @@ class TestViews(APITestCase):
         self.assertIn('JohnCleese', mail.outbox[0].subject)
         self.assertIn('JohnCleese', mail.outbox[0].body)
 
+class TestForms(TestCase):
+    valid_data = {
+        'roof': True,
+        'privacy_place_details': True,
+        'privacy_coordinates': True,
+        'orientations': ['N'],
+        'orientation': 'all',
+        'name': 'JohnCleese',
+        'longitude': -1.553621,
+        'email': 'foo@example.com',
+        'phone': '0202020202',
+        'latitude': 47.218371,
+        'floor_total': '2',
+        'floor': 1,
+        'contrib_type': 'connect',
+        'connect_local': 'on',
+    }
+
+    def test_contact_validation(self):
+        no_contact, phone_contact, email_contact, both_contact = [
+            self.valid_data.copy() for i in range(4)]
+
+        del phone_contact['email']
+        del email_contact['phone']
+        del no_contact['phone']
+        del no_contact['email']
+
+        both_contact.update(phone_contact)
+        both_contact.update(email_contact)
+
+        self.assertFalse(PublicContribForm(no_contact).is_valid())
+        self.assertTrue(PublicContribForm(phone_contact).is_valid())
+        self.assertTrue(PublicContribForm(email_contact).is_valid())
+        self.assertTrue(PublicContribForm(both_contact).is_valid())
+
 
 class TestDataImport(TestCase):
     fixtures = ['bottle_data.yaml']

+ 8 - 2
wifiwithme/static/main.css

@@ -81,14 +81,20 @@ form > h2:first-child {
     border: 1px solid #a94442;
     border-radius: 4px;
 }
+
+/* non-field errorfields */
+#errors ul.errorlist {
+  border: 0;
+}
+
 #errors ul {
     margin: 0 0 0 1em;
     padding: 0;
 }
 
-.form-group .errorlist{
+ .errorlist {
   list-style-type: none;
-}
+ }
 
 /** Results **/
 #map.results {