partner_info.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from django.shortcuts import render
  2. from sharix_admin.forms import PartnerInformationCreateForm, PartnerInformationUpdateForm
  3. from sharix_admin.utils 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 = "sharix_admin/partner_information_form.html"
  22. success_url = reverse_lazy("sharix_admin: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 = "sharix_admin/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