Repository: akvo/akvo-rsr

Issue:
Dimension name and dimension value models don't have permissions defined



Discussion:

Resolving patch:
diff --git a/akvo/rsr/models/__init__.py b/akvo/rsr/models/__init__.py
--- a/akvo/rsr/models/__init__.py
+++ b/akvo/rsr/models/__init__.py
@@ -242,6 +242,16 @@
     is_rsr_admin | is_org_admin | is_org_me_manager_or_project_editor | is_org_enumerator
 )
 
+rules.add_perm('rsr.add_indicatordimensionname', is_rsr_admin | is_org_admin | is_org_me_manager_or_project_editor)
+rules.add_perm('rsr.change_indicatordimensionname', is_rsr_admin | is_org_admin | is_org_me_manager_or_project_editor)
+rules.add_perm('rsr.delete_indicatordimensionname', is_rsr_admin | is_org_admin | is_org_me_manager_or_project_editor)
+rules.add_perm('rsr.view_indicatordimensionname', is_org_enumerator)
+
+rules.add_perm('rsr.add_indicatordimensionvalue', is_rsr_admin | is_org_admin | is_org_me_manager_or_project_editor)
+rules.add_perm('rsr.change_indicatordimensionvalue', is_rsr_admin | is_org_admin | is_org_me_manager_or_project_editor)
+rules.add_perm('rsr.delete_indicatordimensionvalue', is_rsr_admin | is_org_admin | is_org_me_manager_or_project_editor)
+rules.add_perm('rsr.view_indicatordimensionvalue', is_org_enumerator)
+
 rules.add_perm('rsr.add_disaggregation', is_rsr_admin | is_org_admin | is_org_me_manager_or_project_editor)
 rules.add_perm('rsr.change_disaggregation', is_rsr_admin | is_org_admin | is_org_me_manager_or_project_editor)
 rules.add_perm('rsr.delete_disaggregation', is_rsr_admin | is_org_admin | is_org_me_manager_or_project_editor)
diff --git a/akvo/rsr/models/result/indicator_dimension.py b/akvo/rsr/models/result/indicator_dimension.py
--- a/akvo/rsr/models/result/indicator_dimension.py
+++ b/akvo/rsr/models/result/indicator_dimension.py
@@ -54,6 +54,7 @@ def delete(self, *args, **kwargs):
 
 
 class IndicatorDimensionValue(models.Model):
+    project_relation = 'dimension_names__dimension_values__in'
     name = models.ForeignKey(IndicatorDimensionName, verbose_name=u'dimension name',
                              related_name='dimension_values')
     value = ValidXMLCharField(


Test patch:
diff --git a/akvo/rsr/tests/rest/test_permissions.py b/akvo/rsr/tests/rest/test_permissions.py
--- a/akvo/rsr/tests/rest/test_permissions.py
+++ b/akvo/rsr/tests/rest/test_permissions.py
@@ -470,13 +470,13 @@ def populate_model_map():
 
         # one indicator dimension name per project
         model_map[M.IndicatorDimensionName] = {
-            'group_count': group_count(8, 2, 4, 4),
+            'group_count': group_count(8, 2, 6, 4),
             'project_relation': 'project__'
         }
 
         # one indicator dimension value per dimension name
         model_map[M.IndicatorDimensionValue] = {
-            'group_count': group_count(8, 2, 4, 4),
+            'group_count': group_count(8, 2, 6, 4),
             'project_relation': 'name__project__'
         }
 
diff --git a/akvo/rsr/tests/test_permissions.py b/akvo/rsr/tests/test_permissions.py
--- a/akvo/rsr/tests/test_permissions.py
+++ b/akvo/rsr/tests/test_permissions.py
@@ -10,7 +10,7 @@
     Project, Organisation, Employment, Partnership, ProjectUpdate, PartnerSite, IatiExport,
     Result, Indicator, IndicatorPeriod, IndicatorPeriodData, IndicatorPeriodDataComment,
     AdministrativeLocation, ProjectLocation, OrganisationLocation, UserProjects,
-    ProjectHierarchy
+    ProjectHierarchy, IndicatorDimensionName, IndicatorDimensionValue
 )
 from akvo.utils import check_auth_groups
 from akvo.rsr.tests.base import BaseTestCase
@@ -59,6 +59,16 @@ def setUp(self):
             for (project_, user_) in zip(self.projects, self.users)
         ]
 
+        # Indicator Dimension Names
+        self.dimension_names = [
+            IndicatorDimensionName.objects.create(project=project_) for project_ in self.projects
+        ]
+
+        # Indicator Dimension Values
+        self.dimension_values = [
+            IndicatorDimensionValue.objects.create(name=name) for name in self.dimension_names
+        ]
+
         # Results
         self.results = [
             Result.objects.create(project=project_) for project_ in self.projects
@@ -142,6 +152,16 @@ def test_org_admin_permissions(self):
             test = self.assertTrue if i == 0 else self.assertFalse
             test(user.has_perm('rsr.change_projectupdate', project_update))
 
+        # Indicator dimension names
+        for i, dimension_name in enumerate(self.dimension_names):
+            test = self.assertTrue if i == 0 else self.assertFalse
+            test(user.has_perm('rsr.change_indicatordimensionname', dimension_name))
+
+        # Indicator dimension values
+        for i, dimension_value in enumerate(self.dimension_values):
+            test = self.assertTrue if i == 0 else self.assertFalse
+            test(user.has_perm('rsr.change_indicatordimensionvalue', dimension_value))
+
         # Partner Site permissions
         for i, partner_site in enumerate(self.partner_sites):
             test = self.assertTrue if i == 0 else self.assertFalse
@@ -230,6 +250,16 @@ def test_org_project_editor(self):
             test = self.assertTrue if i == 0 else self.assertFalse
             test(user.has_perm('rsr.change_projectupdate', project_update))
 
+        # Indicator dimension names
+        for i, dimension_name in enumerate(self.dimension_names):
+            test = self.assertTrue if i == 0 else self.assertFalse
+            test(user.has_perm('rsr.change_indicatordimensionname', dimension_name))
+
+        # Indicator dimension values
+        for i, dimension_value in enumerate(self.dimension_values):
+            test = self.assertTrue if i == 0 else self.assertFalse
+            test(user.has_perm('rsr.change_indicatordimensionvalue', dimension_value))
+
         # Partner Site permissions
         for i, partner_site in enumerate(self.partner_sites):
             self.assertFalse(user.has_perm('rsr.change_partnersite', partner_site))
@@ -317,6 +347,16 @@ def test_org_me_manager(self):
             test = self.assertTrue if i == 0 else self.assertFalse
             test(user.has_perm('rsr.change_projectupdate', project_update))
 
+        # Indicator dimension names
+        for i, dimension_name in enumerate(self.dimension_names):
+            test = self.assertTrue if i == 0 else self.assertFalse
+            test(user.has_perm('rsr.change_indicatordimensionname', dimension_name))
+
+        # Indicator dimension values
+        for i, dimension_value in enumerate(self.dimension_values):
+            test = self.assertTrue if i == 0 else self.assertFalse
+            test(user.has_perm('rsr.change_indicatordimensionvalue', dimension_value))
+
         # Partner Site permissions
         for i, partner_site in enumerate(self.partner_sites):
             self.assertFalse(user.has_perm('rsr.change_partnersite', partner_site))
@@ -402,6 +442,14 @@ def test_org_user_manager(self):
             test = self.assertTrue if i == 0 else self.assertFalse
             test(user.has_perm('rsr.change_projectupdate', project_update))
 
+        # Indicator dimension names
+        for dimension_name in self.dimension_names:
+            self.assertFalse(user.has_perm('rsr.change_indicatordimensionname', dimension_name))
+
+        # Indicator dimension values
+        for dimension_value in self.dimension_values:
+            self.assertFalse(user.has_perm('rsr.change_indicatordimensionvalue', dimension_value))
+
         # Partner Site permissions
         for i, partner_site in enumerate(self.partner_sites):
             self.assertFalse(user.has_perm('rsr.change_partnersite', partner_site))
@@ -496,6 +544,16 @@ def test_org_enumerator_permissions(self):
         self.assertTrue(user.has_perm('rsr.add_indicatorperioddata', project))
         self.assertTrue(user.has_perm('rsr.change_indicatorperioddata', project))
 
+        # Indicator dimension names
+        for i, dimension_name in enumerate(self.dimension_names):
+            test = self.assertTrue if i == 0 else self.assertFalse
+            test(user.has_perm('rsr.view_indicatordimensionname', dimension_name))
+
+        # Indicator dimension values
+        for i, dimension_value in enumerate(self.dimension_values):
+            test = self.assertTrue if i == 0 else self.assertFalse
+            test(user.has_perm('rsr.view_indicatordimensionvalue', dimension_value))
+
 
 class UserPermissionedProjectsTestCase(BaseTestCase):
     """Testing that restricting permissions to projects per user works."""