Skip to content

Commit

Permalink
Fix #183: project page ellipsis menu visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Ross committed Jul 21, 2016
1 parent 043c642 commit 375ead4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
22 changes: 18 additions & 4 deletions cadasta/organization/tests/test_views_default_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,16 @@ 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)
context['object'] = 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',
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion cadasta/organization/views/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
40 changes: 39 additions & 1 deletion cadasta/organization/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from tutelary.models import Role

from ..models import Organization, Project
from ..models import Organization, Project, OrganizationRole, ProjectRole


class OrganizationMixin:
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion cadasta/templates/organization/project_wrapper.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
<h1>
<a href="{% url 'organization:dashboard' slug=object.organization.slug %}" class="org-name">
{{ object.organization.name }}
</a>
</a>
{{ object.name }}
</h1>
<div class="top-btn pull-right">

<!-- More options menu -->
{% if is_administrator %}
<div class="dropdown pull-right btn-more">
<a data-target="#" data-toggle="dropdown" class="dropdown-toggle" role="button">
<span class="more-menu glyphicon glyphicon-option-vertical"></span>
Expand All @@ -33,6 +34,7 @@ <h1>
<li><a class="edit" href="{% url 'organization:project-edit-permissions' object.organization.slug object.slug %}">{% trans "Edit member permissions" %}</a></li>
<li role="separator" class="divider"></li>
<li><a href="{% url 'organization:project-download' object.organization.slug object.slug %}" data-toggle="modal">{% trans "Download data" %}</a></li>
{% if is_superuser %}
<li role="separator" class="divider"></li>
<li>
{% if object.archived %}
Expand All @@ -41,8 +43,10 @@ <h1>
<a class="archive" href="#archive_confirm" data-toggle="modal">{% trans "Archive project" %}</a>
{% endif %}
</li>
{% endif %}
</ul>
</div>
{% endif %}

<div class="btn-group pull-right btn-add visible-sm-inline visible-md-inline visible-lg-block">
<a class="btn btn-primary" href="{% url 'locations:add' object.organization.slug object.slug %}">
Expand Down

0 comments on commit 375ead4

Please sign in to comment.