partner_info.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from django.shortcuts import render
  2. from SharixAdmin.forms import PartnerInformationCreateForm, PartnerInformationUpdateForm
  3. from SharixAdmin.groups import group_required
  4. from metaservicesynced.models import Company
  5. from django.contrib.auth.mixins import UserPassesTestMixin
  6. from django.views.generic.edit import UpdateView, CreateView
  7. from SharixAdmin.views.context import get_context
  8. from django.urls import reverse
  9. from django.utils.translation import gettext as _
  10. from core.utils.AuthAPI import AuthAPI
  11. from tickets.models import Task
  12. from core.config import API_URL
  13. api = AuthAPI("79999999999", "Pa$$w0rd1")
  14. import requests
  15. from django.urls import reverse_lazy
  16. from django.contrib.auth.hashers import check_password
  17. import xmpp
  18. from xmpp import cli
  19. class PartnerInformationCreate(UserPassesTestMixin, CreateView):
  20. model = Company
  21. form_class = PartnerInformationCreateForm
  22. template_name = "SharixAdmin/partner_information_form.html"
  23. success_url = reverse_lazy("SharixAdmin:partners")
  24. def form_valid(self, form):
  25. form.instance.representative_id = self.request.user
  26. new_ticket = {
  27. "task_list": 1,
  28. "created_by": self.request.user.pk,
  29. "type": 1,
  30. "title": "service_create",
  31. "note": str(form.data),
  32. }
  33. resp = requests.post(f"{API_URL}/tickets/api/tickets/", data=new_ticket, headers=api.headers)
  34. jso = resp.json()
  35. form.instance.ticket_status = Task.objects.get(pk=int(jso['id']))
  36. print(form.cleaned_data)
  37. responce = super().form_valid(form)
  38. cli.send_message("open_tickets_backend@ej.sharix-app.org", "eb177b1c9f99a7a13798928318d7a72c", "open_strequest_new@ej.sharix-app.org", str(jso))
  39. return responce
  40. def get_context_data(self, **kwargs):
  41. context = super().get_context_data(**kwargs)
  42. context.update(get_context(self.request, {
  43. 'title': _('Partner Information'),
  44. 'object': self.object,
  45. }))
  46. return context
  47. def get_success_url(self):
  48. return reverse('partners')
  49. def test_func(self) -> bool or None:
  50. group_names = ('PARTNER-ADMIN')
  51. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  52. return True
  53. return False
  54. class PartnerInformationUpdateView(UserPassesTestMixin, UpdateView):
  55. model = Company
  56. form_class = PartnerInformationUpdateForm
  57. template_name = "SharixAdmin/partner_information_form.html"
  58. def get_context_data(self, **kwargs):
  59. context = super().get_context_data(**kwargs)
  60. context.update(get_context(self.request, {
  61. 'title': _('Partner Information'),
  62. 'object': self.object,
  63. }))
  64. return context
  65. def form_valid(self, form):
  66. # Получаем текущий объект Company
  67. self.object = form.save()
  68. # Проверяем, изменились ли поля inn, ogrn, kpp, ответственный, название
  69. if (self.object.inn != form.initial['inn'] or
  70. self.object.ogrn != form.initial['ogrn'] or
  71. self.object.kpp != form.initial['kpp'] or
  72. self.object.repr_id != form.initial['repr_id'] or
  73. self.object.legal_name != form.initial['legal_name']):
  74. new_ticket = {
  75. "task_list": 1,
  76. "created_by": self.request.user.pk,
  77. "type": 1,
  78. "title": "service_update",
  79. "note": str(form.data),
  80. }
  81. resp = requests.post(f"{API_URL}/tickets/api/tickets/", data=new_ticket, headers=api.headers)
  82. jso = resp.json()
  83. form.instance.ticket_status = Task.objects.get(pk=int(jso['id']))
  84. print(form.cleaned_data)
  85. cli.send_message("open_tickets_backend@ej.sharix-app.org", "eb177b1c9f99a7a13798928318d7a72c", "open_strequest_new@ej.sharix-app.org", str(jso))
  86. self.object.status = "deactivate"
  87. responce = super().form_valid(form)
  88. return responce
  89. else:
  90. return super().form_valid(form)
  91. def get_success_url(self):
  92. return reverse('test-page')
  93. def test_func(self) -> bool or None:
  94. group_names = ('PARTNER-ADMIN')
  95. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  96. return True
  97. return False
  98. class PartnerInfoView(UserPassesTestMixin, UpdateView):
  99. model = Company
  100. form_class = PartnerInformationUpdateForm
  101. template_name = "SharixAdmin/partner_information_form.html"
  102. def get_context_data(self, **kwargs):
  103. # Проверяем, был ли ранее создан объект Company текущим пользователем
  104. if self.request.user.is_authenticated:
  105. company = Company.objects.filter(repr_id=self.request.user).first()
  106. if company:
  107. context['object'] = company
  108. context = super().get_context_data(**kwargs)
  109. context.update(get_context(self.request, {
  110. 'title': _('Partner Information'),
  111. 'object': self.object,
  112. }))
  113. return context
  114. def form_valid(self, form):
  115. # Получаем текущий объект Company
  116. self.object = form.save()
  117. # Проверяем, изменились ли поля inn, ogrn, kpp, ответственный, название
  118. if (self.object.inn != form.initial['inn'] or
  119. self.object.ogrn != form.initial['ogrn'] or
  120. self.object.kpp != form.initial['kpp'] or
  121. self.object.repr_id != form.initial['repr_id'] or
  122. self.object.legal_name != form.initial['legal_name']):
  123. new_ticket = {
  124. "task_list": 1,
  125. "created_by": self.request.user.pk,
  126. "type": 1,
  127. "title": "service_update",
  128. "note": str(form.data),
  129. }
  130. resp = requests.post(f"{API_URL}/tickets/api/tickets/", data=new_ticket, headers=api.headers)
  131. jso = resp.json()
  132. form.instance.ticket_status = Task.objects.get(pk=int(jso['id']))
  133. print(form.cleaned_data)
  134. cli.send_message("open_tickets_backend@ej.sharix-app.org", "eb177b1c9f99a7a13798928318d7a72c", "open_strequest_new@ej.sharix-app.org", str(jso))
  135. self.object.status = "deactivate"
  136. responce = super().form_valid(form)
  137. return responce
  138. else:
  139. return super().form_valid(form)
  140. def get_success_url(self):
  141. return reverse('test-page')
  142. def test_func(self) -> bool or None:
  143. group_names = ('PARTNER-ADMIN')
  144. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  145. return True
  146. return False
  147. def partner_information(request):
  148. context = get_context(request, {
  149. 'title':_('Partner Information'),
  150. })
  151. return render(request, 'SharixAdmin/partner_information.html', context)