From 375ead4de29873c7f25944d5ef59478f7c57c4fd Mon Sep 17 00:00:00 2001 From: Ian Ross Date: Thu, 21 Jul 2016 18:09:21 +0200 Subject: [PATCH] Fix #183: project page ellipsis menu visibility --- .../tests/test_views_default_projects.py | 22 ++++++++-- cadasta/organization/views/default.py | 4 +- cadasta/organization/views/mixins.py | 40 ++++++++++++++++++- .../organization/project_wrapper.html | 6 ++- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/cadasta/organization/tests/test_views_default_projects.py b/cadasta/organization/tests/test_views_default_projects.py index dffb9807d..14e296307 100644 --- a/cadasta/organization/tests/test_views_default_projects.py +++ b/cadasta/organization/tests/test_views_default_projects.py @@ -230,8 +230,8 @@ def _get_private(self, status=None, user=None, make_org_member=False, OrganizationRole.objects.create(organization=other_org, user=user) return self._get(prj, user=user, status=status), prj - def _check_render(self, response, project, - assign_context=False, is_superuser=False): + def _check_render(self, response, project, assign_context=False, + is_superuser=False, is_administrator=False): content = response.render().content.decode('utf-8') context = RequestContext(self.request) @@ -239,6 +239,7 @@ def _check_render(self, response, project, context['project'] = project context['geojson'] = '{"type": "FeatureCollection", "features": []}' context['is_superuser'] = is_superuser + context['is_administrator'] = is_administrator expected = render_to_string( 'organization/project_dashboard.html', @@ -265,7 +266,19 @@ def test_get_with_superuser(self): self.superuser_role = Role.objects.get(name='superuser') superuser.assign_policies(self.superuser_role) response = self._get(self.project1, user=superuser, status=200) - self._check_render(response, self.project1, is_superuser=True) + self._check_render(response, self.project1, + is_superuser=True, is_administrator=True) + + def test_get_with_org_admin(self): + org_admin = UserFactory.create() + OrganizationRole.objects.create( + organization=self.project1.organization, + user=org_admin, + admin=True + ) + response = self._get(self.project1, user=org_admin, status=200) + self._check_render(response, self.project1, + is_superuser=False, is_administrator=True) def test_get_non_existent_project(self): setattr(self.request, 'user', self.user) @@ -316,7 +329,8 @@ def test_get_private_project_with_superuser(self): response, prj = self._get_private( user=superuser, status=200 ) - self._check_render(response, prj, is_superuser=True) + self._check_render(response, prj, + is_superuser=True, is_administrator=True) class ProjectAddTest(UserTestCase): diff --git a/cadasta/organization/views/default.py b/cadasta/organization/views/default.py index 2b0d2e8ad..0e477b711 100644 --- a/cadasta/organization/views/default.py +++ b/cadasta/organization/views/default.py @@ -287,7 +287,9 @@ def get(self, request, *args, **kwargs): return super(generic.ListView, self).render_to_response(context) -class ProjectDashboard(PermissionRequiredMixin, generic.DetailView): +class ProjectDashboard(PermissionRequiredMixin, + mixins.ProjectAdminCheckMixin, + generic.DetailView): def get_actions(view, request): if view.get_object().public(): return 'project.view' diff --git a/cadasta/organization/views/mixins.py b/cadasta/organization/views/mixins.py index 884b5000e..045aacb9d 100644 --- a/cadasta/organization/views/mixins.py +++ b/cadasta/organization/views/mixins.py @@ -6,7 +6,7 @@ from tutelary.models import Role -from ..models import Organization, Project +from ..models import Organization, Project, OrganizationRole, ProjectRole class OrganizationMixin: @@ -83,3 +83,41 @@ def get_queryset(self): ) return Project.objects.filter(access='public') + + +class ProjectAdminCheckMixin: + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.is_admin = None + + @property + def is_administrator(self): + if self.is_admin is None: + su_role = Role.objects.filter(name='superuser') + org_admins = [ + role.user for role in OrganizationRole.objects.filter( + organization=self.get_object().organization, + admin=True + ) + ] + proj_managers = [ + role.user for role in ProjectRole.objects.filter( + project=self.get_object(), + role='PM' + ) + + ] + self.is_admin = False + if len(su_role) > 0: + if hasattr(self.request.user, 'assigned_policies'): + if su_role[0] in self.request.user.assigned_policies(): + self.is_admin = True + if (self.request.user in org_admins or + self.request.user in proj_managers): + self.is_admin = True + return self.is_admin + + def get_context_data(self, *args, **kwargs): + context = super().get_context_data(*args, **kwargs) + context['is_administrator'] = self.is_administrator + return context diff --git a/cadasta/templates/organization/project_wrapper.html b/cadasta/templates/organization/project_wrapper.html index bd051f487..0d8943221 100644 --- a/cadasta/templates/organization/project_wrapper.html +++ b/cadasta/templates/organization/project_wrapper.html @@ -17,12 +17,13 @@

{{ object.organization.name }} - + {{ object.name }}

+ {% if is_administrator %} + {% endif %}