1
0

service_info.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 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. class ServiceInformationCreate(UserPassesTestMixin, CreateView):
  16. model = Service
  17. form_class = ServiceInformationCreateForm
  18. template_name = "SharixAdmin/service_information_form.html"
  19. success_url = reverse_lazy("SharixAdmin:service_tariff")
  20. def form_valid(self, form):
  21. new_ticket = {
  22. "ticket_list": 1,
  23. "created_by": self.request.user.pk,
  24. "type": 1,
  25. "title": "service_create",
  26. "note": str(form.data),
  27. }
  28. resp = requests.post(f"{API_URL}/tickets/api/tickets/", data=new_ticket, headers=api.headers)
  29. jso = resp.json()
  30. form.instance.ticket_status = Ticket.objects.get(pk=int(jso['id']))
  31. print(form.cleaned_data)
  32. responce = super().form_valid(form)
  33. cli.send_message("open_tickets_backend@ej.sharix-app.org", "eb177b1c9f99a7a13798928318d7a72c", "open_strequest_new@ej.sharix-app.org", str(jso))
  34. return responce
  35. def get_context_data(self, **kwargs):
  36. context = super().get_context_data(**kwargs)
  37. context.update({
  38. 'title': _('Information about the service'),
  39. 'object': self.object,
  40. 'current_page': 'service_info'
  41. })
  42. return context
  43. def get_success_url(self):
  44. return reverse('service_tariff')
  45. def test_func(self) -> bool or None:
  46. group_names = ('METASERVICE-ADMIN')
  47. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user:
  48. return True
  49. return False
  50. class ServiceInformationUpdateView(UserPassesTestMixin, UpdateView):
  51. model = Service
  52. form_class = ServiceInformationUpdateForm
  53. template_name = "SharixAdmin/service_information_form.html"
  54. def get_context_data(self, **kwargs):
  55. context = super().get_context_data(**kwargs)
  56. context.update({
  57. 'title': 'Information about the service',
  58. 'object': self.object,
  59. 'current_page': 'service_info'
  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