groups.py 1.3 KB

12345678910111213141516171819202122232425262728293031
  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='PARTNER-SUPPORT')
  16. Group.objects.get_or_create(name='GUEST')
  17. Group.objects.get_or_create(name='CLIENT')
  18. Group.objects.get_or_create(name='PROVIDER')
  19. # Функция позволяющая определить принадлежность к группе, перенаправляет на авторизацию
  20. def group_required(*group_names):
  21. def in_groups(u):
  22. if u.is_authenticated:
  23. if u.groups.filter(name=group_names).exists() or u.is_superuser:
  24. return True
  25. return False
  26. return user_passes_test(in_groups)