service_info.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from SharixAdmin.forms import ServiceInformationCreateForm, ServiceInformationUpdateForm
  2. from django.contrib.auth.mixins import UserPassesTestMixin
  3. from django.views.generic.edit import CreateView, UpdateView
  4. from dbsynce.models import Service
  5. from SharixAdmin.views.context import get_context
  6. from django.urls import reverse
  7. from django.utils.translation import gettext as _
  8. from core.utils.AuthAPI import AuthAPI
  9. from tickets.models import Ticket
  10. from core.settings import API_URL
  11. api = AuthAPI("89855703300", "12345")
  12. import requests
  13. from django.urls import reverse_lazy
  14. from datetime import timezone
  15. import xmpp
  16. from xmpp import cli
  17. class ServiceInformationCreate(UserPassesTestMixin, CreateView):
  18. model = Service
  19. form_class = ServiceInformationCreateForm
  20. template_name = "SharixAdmin/service_information_form.html"
  21. success_url = reverse_lazy("SharixAdmin:service_tariff")
  22. def form_valid(self, form):
  23. new_ticket = {
  24. "ticket_list": 1,
  25. "created_by": self.request.user.pk,
  26. "type": 1,
  27. "title": "service_create",
  28. "note": str(form.data),
  29. }
  30. resp = requests.post(f"{API_URL}/tickets/api/tickets/", data=new_ticket, headers=api.headers)
  31. jso = resp.json()
  32. form.instance.ticket_status = Ticket.objects.get(pk=int(jso['id']))
  33. print(form.cleaned_data)
  34. responce = super().form_valid(form)
  35. cli.send_message("open_tickets_backend@ej.sharix-app.org", "eb177b1c9f99a7a13798928318d7a72c", "open_strequest_new@ej.sharix-app.org", str(jso))
  36. return responce
  37. def get_context_data(self, **kwargs):
  38. context = super().get_context_data(**kwargs)
  39. context.update(get_context(self.request, {
  40. 'title': _('Information about the service'),
  41. 'object': self.object,
  42. }))
  43. return context
  44. def get_success_url(self):
  45. return reverse('service_tariff')
  46. def test_func(self) -> bool or None:
  47. group_names = ('METASERVICE-ADMIN')
  48. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  49. return True
  50. return False
  51. class ServiceInformationUpdateView(UserPassesTestMixin, UpdateView):
  52. model = Service
  53. form_class = ServiceInformationUpdateForm
  54. template_name = "SharixAdmin/service_information_form.html"
  55. def get_context_data(self, **kwargs):
  56. context = super().get_context_data(**kwargs)
  57. context.update(get_context(self.request, {
  58. 'title': 'Information about the service',
  59. 'object': self.object,
  60. }))
  61. return context
  62. def get_success_url(self):
  63. return reverse('test-page')
  64. def test_func(self) -> bool or None:
  65. group_names = ('METASERVICE-ADMIN')
  66. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  67. return True
  68. return False