Guest User

Untitled

a guest
Aug 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. from django.contrib.admin.sites import AdminSite
  2.  
  3.  
  4. #redefining (extend) the admin class for AdminSite2 to let groups enter in the admins or not
  5. class MyAdminSite2(AdminSite):
  6.  
  7. def __init__(self, name=None, app_name='admin', valid_groups=None ):
  8. """
  9. Call the default contructor and add valid_groups for the group checking for the admin site
  10. """
  11. super(MyAdminSite2, self).__init__()
  12. self.valid_groups = valid_groups
  13.  
  14.  
  15.  
  16. def has_permission(self, request):
  17. """
  18. Returns True if the given HttpRequest has permission to view
  19. *at least one* page in the admin site. And checks the groups that are valid for this class
  20. """
  21. # return request.user.is_active and request.user.is_staff and request.user.username != 'admin'
  22. if self.valid_groups != None:
  23. try:
  24. #get the groups the user is in and check if on of the groups allowed for this admin site
  25. #is avaiable in the user groups
  26. groups = request.user.groups.values_list('name',flat=True)
  27. for i in self.valid_groups:
  28. if i in groups:
  29. return True
  30. return False
  31. except AttributeError:
  32. #Wen we logout we don't have user to verify the groups and django throws
  33. #AttributeError at /XXXXXX/ 'NoneType' object has no attribute '_meta'
  34. pass
  35. else:
  36. return request.user.is_active and request.user.is_staff
Add Comment
Please, Sign In to add comment