Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.contrib.admin.sites import AdminSite
- #redefining (extend) the admin class for AdminSite2 to let groups enter in the admins or not
- class MyAdminSite2(AdminSite):
- def __init__(self, name=None, app_name='admin', valid_groups=None ):
- """
- Call the default contructor and add valid_groups for the group checking for the admin site
- """
- super(MyAdminSite2, self).__init__()
- self.valid_groups = valid_groups
- def has_permission(self, request):
- """
- Returns True if the given HttpRequest has permission to view
- *at least one* page in the admin site. And checks the groups that are valid for this class
- """
- # return request.user.is_active and request.user.is_staff and request.user.username != 'admin'
- if self.valid_groups != None:
- try:
- #get the groups the user is in and check if on of the groups allowed for this admin site
- #is avaiable in the user groups
- groups = request.user.groups.values_list('name',flat=True)
- for i in self.valid_groups:
- if i in groups:
- return True
- return False
- except AttributeError:
- #Wen we logout we don't have user to verify the groups and django throws
- #AttributeError at /XXXXXX/ 'NoneType' object has no attribute '_meta'
- pass
- else:
- return request.user.is_active and request.user.is_staff
Add Comment
Please, Sign In to add comment