groups.py 1.4 KB

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