service_info.py 2.9 KB

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