groups.py 1.1 KB

12345678910111213141516171819202122232425262728
  1. from django.contrib.auth.models import Group
  2. from django.dispatch import receiver
  3. from django.db.models.signals import post_migrate
  4. from django.contrib.auth.decorators import user_passes_test
  5. #Создание групп
  6. @receiver(post_migrate)
  7. def create_groups(sender, **kwargs):
  8. Group.objects.get_or_create(name='METASERVICE-ADMIN')
  9. Group.objects.get_or_create(name='METASERVICE-SUPERVISOR')
  10. Group.objects.get_or_create(name='METASERVICE-SUPPORT')
  11. Group.objects.get_or_create(name='METASERVICE-TECHSUPPORT')
  12. Group.objects.get_or_create(name='PARTNER-ADMIN')
  13. Group.objects.get_or_create(name='PARTNER-SUPERVISOR')
  14. Group.objects.get_or_create(name='PARTNER-TECHSUPPORT')
  15. Group.objects.get_or_create(name='GUEST')
  16. # Функция позволяющая определить принадлежность к группе, перенаправляет на авторизацию
  17. def group_required(*group_names):
  18. def in_groups(u):
  19. if u.is_authenticated:
  20. if u.groups.filter(name=group_names).exists() or u.is_superuser:
  21. return True
  22. return False
  23. return user_passes_test(in_groups)