Skip to content

Instantly share code, notes, and snippets.

@squarepegsys
Created November 13, 2012 19:52
Show Gist options
  • Save squarepegsys/4067972 to your computer and use it in GitHub Desktop.
Save squarepegsys/4067972 to your computer and use it in GitHub Desktop.
ReadOnlyAdmin
## adopted from http://gremu.net/blog/2010/django-admin-read-only-permission/
class ReadOnlyAdmin(admin.ModelAdmin):
"""
"""
def has_add_permission(self, request,obj=None):
"""
Arguments:
- `request`:
- `obj`:
"""
return self.__user_is_readonly(request)
def has_delete_permission(self, request,obj=None):
"""
Arguments:
- `request`:
- `obj`:
"""
return self.__user_is_readonly(request)
def get_actions(self, request):
actions = super(ReadOnlyAdmin, self).get_actions(request)
if self.__user_is_readonly(request):
if 'delete_selected' in actions:
del actions['delete_selected']
return actions
def change_view(self, request, object_id, extra_context=None):
if self.__user_is_readonly(request):
self.readonly_fields=self.user_readonly
self.inlines = self.user_readonly_inlines
try:
return super(ReadOnlyAdmin, self).change_view(
request, object_id, extra_context=extra_context)
except PermissionDenied:
pass
if request.method == 'POST':
raise PermissionDenied
request.readonly = True
return super(ReadOnlyAdmin, self).change_view(
request, object_id, extra_context=extra_context)
def __user_is_readonly(self, request):
groups = [x.name for x in request.user.groups.all() ]
return "readonly" in groups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment