Browse Source

Implement privacy control on Contrib fields

Jocelyn Delande 9 years ago
parent
commit
082f8444d4
2 changed files with 58 additions and 0 deletions
  1. 30 0
      wifiwithme/apps/contribmap/models.py
  2. 28 0
      wifiwithme/apps/contribmap/tests.py

+ 30 - 0
wifiwithme/apps/contribmap/models.py

@@ -81,5 +81,35 @@ class Contrib(models.Model):
         db_table = 'contribs'
         verbose_name = 'contribution'
 
+    PRIVACY_MAP = {
+        'name': 'privacy_name',
+        'comment': 'privacey_comment',
+        'floor': 'privacy_place_details',
+        'floor_total': 'privacy_place_details',
+        'orientations': 'privacy_place_details',
+        'roof': 'privacy_place_details',
+    }
+    PUBLIC_FIELDS = set(PRIVACY_MAP.keys())
+
     def __str__(self):
         return '#{} {}'.format(self.pk, self.name)
+
+    def is_public(self):
+        return not self.privacy_coordinates
+
+    def _may_be_public(self, field):
+        return field in self.PUBLIC_FIELDS
+
+    def _is_public(self, field):
+        return getattr(self, self.PRIVACY_MAP[field])
+
+    def get_public_field(self, field):
+        """ Gets safely an attribute in its public form (if any)
+
+        :param field: The field name
+        :return: the field value, or None, if the field is private
+        """
+        if self._may_be_public(field) and self._is_public(field):
+            return getattr(self, field)
+        else:
+            return None

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

@@ -14,6 +14,34 @@ class TestContrib(TestCase):
         co.save()
 
 
+class TestContribPrivacy(TestCase):
+    def test_always_private_field(self):
+        c = Contrib.objects.create(
+            name='John',
+            phone='010101010101',
+            contrib_type=Contrib.CONTRIB_CONNECT,
+        )
+        self.assertEqual(c.get_public_field('phone'), None)
+
+    def test_public_field(self):
+        c = Contrib.objects.create(
+            name='John',
+            phone='010101010101',
+            contrib_type=Contrib.CONTRIB_CONNECT,
+            privacy_name=True,
+        )
+        self.assertEqual(c.get_public_field('name'), 'John')
+
+    def test_private_field(self):
+        c = Contrib.objects.create(
+            name='John',
+            phone='010101010101',
+            contrib_type=Contrib.CONTRIB_CONNECT,
+        )
+        self.assertEqual(c.privacy_name, False)
+        self.assertEqual(c.get_public_field('name'), None)
+
+
 class TestDataImport(TestCase):
     fixtures = ['bottle_data.yaml']