-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
159 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,50 @@ | ||
from __future__ import absolute_import | ||
|
||
from django.http import Http404 | ||
|
||
|
||
def get_object_owner_or_admin(self): | ||
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field | ||
|
||
assert lookup_url_kwarg in self.kwargs, ( | ||
'Expected view %s to be called with a URL keyword argument ' | ||
'named "%s". Fix your URL conf, or set the `.lookup_field` ' | ||
'attribute on the view correctly.' % | ||
(self.__class__.__name__, lookup_url_kwarg) | ||
) | ||
|
||
query = {self.lookup_field: self.kwargs[lookup_url_kwarg]} | ||
|
||
if not (self.request.user.groups.filter(name='NWCC_ADMIN').exists() or | ||
self.request.user.is_superuser): | ||
query['user'] = self.request.user | ||
try: | ||
return self.model.objects.get(**query) | ||
except self.model.DoesNotExist: | ||
raise Http404 | ||
|
||
|
||
def owner_or_admin(queryset, request, | ||
user_field='user', restrict_to_admin=True): | ||
""" | ||
Filter a queryset. Default is to show only records where user field | ||
is the current user, unless admin and explicitly requesting all | ||
matching records. THe user field is by default 'user', but that can | ||
matching records. The user field is by default 'user', but that can | ||
be changed as required. | ||
""" | ||
|
||
user = request.user | ||
|
||
if "show_all" in request.query_params and \ | ||
(not restrict_to_admin or user.is_staff or user.is_admin): | ||
if user.is_anonymous: | ||
# we don't know who they are, so we give them nothing | ||
return queryset.none() | ||
elif ('show_all' in request.query_params and | ||
(not restrict_to_admin or | ||
user.groups.filter(name='NWCC_ADMIN').exists() or | ||
user.is_superuser)): | ||
# the user wants to see all records, | ||
# so don't filter by user | ||
return queryset | ||
elif user.is_anonymous: | ||
# we don't know who they are, so we give them nothing | ||
return queryset.none() | ||
else: | ||
# normal behavior is to filter by user | ||
return queryset.filter(**{user_field: user}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.