groups.py 1.2 KB

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