partner_info.py 6.9 KB

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