Skip to content

Commit

Permalink
Fix #184: add project organisation filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Ross committed Jul 21, 2016
1 parent 375ead4 commit b3bd6fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
24 changes: 20 additions & 4 deletions cadasta/organization/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.forms.utils import ErrorDict

from leaflet.forms.widgets import LeafletWidget
from tutelary.models import Role
from tutelary.models import Role, check_perms
from buckets.widgets import S3FileUploadWidget

from accounts.models import User
Expand Down Expand Up @@ -226,12 +226,28 @@ class ProjectAddDetails(forms.Form):
accepted_types=QUESTIONNAIRE_TYPES))
contacts = ContactsField(form=ContactsForm, required=False)

def check_admin(self, user):
if not hasattr(self, 'su_role'):
self.su_role = Role.objects.get(name='superuser')

is_superuser = any([isinstance(pol, Role) and pol == self.su_role
for pol in user.assigned_policies()])
return is_superuser

def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)

self.fields['organization'].choices = [
(o.slug, o.name) for o in Organization.objects.order_by('name')
]
if self.check_admin(self.user):
self.fields['organization'].choices = [
(o.slug, o.name) for o in Organization.objects.order_by('name')
]
else:
qs = self.user.organizations.all()
self.fields['organization'].choices = [
(o.slug, o.name) for o in qs.order_by('name')
if check_perms(self.user, ('project.create',), (o,))
]

def clean_name(self):
name = self.cleaned_data['name']
Expand Down
6 changes: 5 additions & 1 deletion cadasta/organization/views/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,11 @@ def process_step(self, form):
return result

def get_form_kwargs(self, step=None):
if step == 'permissions':
if step == 'details':
return {
'user': self.request.user
}
elif step == 'permissions':
return {
'organization': self.get_cleaned_data_for_step(
'details').get('organization')
Expand Down

0 comments on commit b3bd6fd

Please sign in to comment.