signals.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import os
  2. from django.contrib.auth.models import Group
  3. from django.dispatch import receiver
  4. from django.db.models.signals import post_migrate
  5. from django.contrib.auth import get_user_model
  6. from django.contrib.auth.hashers import make_password
  7. from django.conf import settings
  8. from tickets.models import TicketList
  9. User = get_user_model()
  10. @receiver(post_migrate)
  11. def create_initial_groups(sender, **kwargs):
  12. Group.objects.get_or_create(pk=21, name='METASERVICE-ADMIN')
  13. Group.objects.get_or_create(pk=22, name='METASERVICE-SUPERVISOR')
  14. Group.objects.get_or_create(pk=23, name='METASERVICE-SUPPORT')
  15. Group.objects.get_or_create(pk=24, name='METASERVICE-TECHSUPPORT')
  16. Group.objects.get_or_create(pk=31, name='PARTNER-ADMIN')
  17. Group.objects.get_or_create(pk=32, name='PARTNER-SUPERVISOR')
  18. Group.objects.get_or_create(pk=33, name='PARTNER-SUPPORT')
  19. Group.objects.get_or_create(pk=34, name='PARTNER-TECHSUPPORT')
  20. Group.objects.get_or_create(pk=41, name='PROVIDER')
  21. Group.objects.get_or_create(pk=51, name='CLIENT')
  22. Group.objects.get_or_create(pk=61, name='GUEST')
  23. Group.objects.get_or_create(pk=99, name='TEST')
  24. @receiver(post_migrate)
  25. def create_test_users(sender, **kwargs):
  26. """
  27. Создает тестовых пользователей, но только если установочный скрипт был запущен с параметром --test-users
  28. или была задана переменная окружения TEST_USERS=true.
  29. """
  30. if os.getenv('TEST_USERS') == 'true':
  31. password = make_password("sharix-open-test")
  32. test_group = Group.objects.get(pk=99)
  33. # Группа, в которой надо создать пользователя и их количество
  34. test_users = [
  35. ('METASERVICE-ADMIN', 1),
  36. ('METASERVICE-SUPERVISOR', 3),
  37. ('METASERVICE-SUPPORT', 3),
  38. ('METASERVICE-TECHSUPPORT', 3),
  39. ('PARTNER-ADMIN', 1),
  40. ('PARTNER-SUPERVISOR', 3),
  41. ('PARTNER-SUPPORT', 3),
  42. ('PARTNER-TECHSUPPORT', 3),
  43. ('PROVIDER', 3),
  44. ('CLIENT', 3),
  45. ('GUEST', 3)
  46. ]
  47. for test_user in test_users:
  48. group_name = test_user[0]
  49. group = Group.objects.get(name=group_name)
  50. for i in range(1, test_user[1] + 1):
  51. user, created = User.objects.get_or_create(
  52. phone_number=f"{group.pk}0{i}",
  53. defaults={
  54. 'last_name': i,
  55. 'first_name': group_name,
  56. 'email': f"test-{group_name.lower()}-{i}@domain.org",
  57. 'username': f"test-{group_name.lower()}-{i}",
  58. 'middle_name': "Test",
  59. 'password': password
  60. }
  61. )
  62. if created:
  63. user.groups.add(
  64. test_group,
  65. group
  66. )
  67. print(f"Test user created: {user}")
  68. @receiver(post_migrate)
  69. def create_initial_ticket_lists(sender, **kwargs):
  70. if sender.name == 'tickets':
  71. # METASERVICE
  72. ## ADMIN
  73. TicketList.objects.get_or_create(pk=2101, name='Активация партнеров (NEG_REQUEST)', group=Group.objects.get(name='METASERVICE-ADMIN'))
  74. TicketList.objects.get_or_create(pk=2102, name='Права в сервисе (ACCESS_REQUEST)', group=Group.objects.get(name='METASERVICE-ADMIN'))
  75. TicketList.objects.get_or_create(pk=2103, name='Проверка документов (ST_REQUEST)', group=Group.objects.get(name='METASERVICE-ADMIN'))
  76. ## SUPERVISOR
  77. TicketList.objects.get_or_create(pk=2201, name='Активность пользователей (ST_REQUEST)', group=Group.objects.get(name='METASERVICE-SUPERVISOR'))
  78. TicketList.objects.get_or_create(pk=2202, name='Оперативный доступ (ST_REQUEST)', group=Group.objects.get(name='METASERVICE-SUPERVISOR'))
  79. TicketList.objects.get_or_create(pk=2203, name='Оперативный доступ экстра (ACCESS_REQUEST)', group=Group.objects.get(name='METASERVICE-SUPERVISOR'))
  80. TicketList.objects.get_or_create(pk=2204, name='Права сервиса (ST_REQUEST)', group=Group.objects.get(name='METASERVICE-SUPERVISOR'))
  81. TicketList.objects.get_or_create(pk=2205, name='Проверка документов (ST_REQUEST)', group=Group.objects.get(name='METASERVICE-SUPERVISOR'))
  82. ## SUPPORT
  83. TicketList.objects.get_or_create(pk=2301, name='Входящие обычные заявки (ST_REQUEST)', group=Group.objects.get(name='METASERVICE-SUPPORT'))
  84. TicketList.objects.get_or_create(pk=2302, name='Заявки на услуги сервиса (SERVICE_REQUEST)', group=Group.objects.get(name='METASERVICE-SUPPORT'))
  85. ## TECHSUPPORT
  86. TicketList.objects.get_or_create(pk=2401, name='Входящие технические заявки (ST_REQUEST)', group=Group.objects.get(name='METASERVICE-TECHSUPPORT'))
  87. # PARTNER
  88. ## ADMIN
  89. TicketList.objects.get_or_create(pk=3101, name='Документы исполнителей (ST_REQUEST)', group=Group.objects.get(name='PARTNER-ADMIN'))
  90. TicketList.objects.get_or_create(pk=3102, name='Документы ресурсов (ST_REQUEST)', group=Group.objects.get(name='PARTNER-ADMIN'))
  91. TicketList.objects.get_or_create(pk=3103, name='Права в партнерке (ACCESS_REQUEST)', group=Group.objects.get(name='PARTNER-ADMIN'))
  92. ## SUPERVISOR
  93. TicketList.objects.get_or_create(pk=3201, name='Активность внутри партнера (ST_REQUEST)', group=Group.objects.get(name='PARTNER-SUPERVISOR'))
  94. TicketList.objects.get_or_create(pk=3202, name='Документы исполнителей (ST_REQUEST)', group=Group.objects.get(name='PARTNER-SUPERVISOR'))
  95. TicketList.objects.get_or_create(pk=3203, name='Документы ресурсов (ST_REQUEST)', group=Group.objects.get(name='PARTNER-SUPERVISOR'))
  96. TicketList.objects.get_or_create(pk=3204, name='Доступ внутри партнера (ST_REQUEST)', group=Group.objects.get(name='PARTNER-SUPERVISOR'))
  97. TicketList.objects.get_or_create(pk=3205, name='Оперативный доступ (ST_REQUEST)', group=Group.objects.get(name='PARTNER-SUPERVISOR'))
  98. TicketList.objects.get_or_create(pk=3206, name='Оперативный доступ экстра (ST_REQUEST)', group=Group.objects.get(name='PARTNER-SUPERVISOR'))
  99. TicketList.objects.get_or_create(pk=3207, name='Ручное подтверждение заявок (ACCESS-REQUEST)', group=Group.objects.get(name='PARTNER-SUPERVISOR'))
  100. ## TECHSUPPORT
  101. TicketList.objects.get_or_create(pk=3401, name='Входящие технические заявки (ST_REQUEST)', group=Group.objects.get(name='PARTNER-TECHSUPPORT'))
  102. # Откючаем тестовых пользователей, если DEBUG=False и наоброт
  103. test_group = Group.objects.get(name="TEST")
  104. users_in_test_group = User.objects.filter(groups=test_group)
  105. if users_in_test_group:
  106. users_in_test_group.update(is_active=settings.DEBUG)