groups.py 1.4 KB

123456789101112131415161718192021222324252627282930313233
  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(pk=21, name='METASERVICE-ADMIN')
  9. Group.objects.get_or_create(pk=22, name='METASERVICE-SUPERVISOR')
  10. Group.objects.get_or_create(pk=23, name='METASERVICE-SUPPORT')
  11. Group.objects.get_or_create(pk=24, name='METASERVICE-TECHSUPPORT')
  12. Group.objects.get_or_create(pk=31, name='PARTNER-ADMIN')
  13. Group.objects.get_or_create(pk=32, name='PARTNER-SUPERVISOR')
  14. Group.objects.get_or_create(pk=33, name='PARTNER-SUPPORT')
  15. Group.objects.get_or_create(pk=34, name='PARTNER-TECHSUPPORT')
  16. Group.objects.get_or_create(pk=41, name='PROVIDER')
  17. Group.objects.get_or_create(pk=51, name='CLIENT')
  18. Group.objects.get_or_create(pk=61, name='GUEST')
  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)