service_info.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 metaservicesynced.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 Task
  10. from core.config import API_URL
  11. api = AuthAPI("89855703300", "12345")
  12. import requests
  13. from django.urls import reverse_lazy
  14. from datetime import timezone
  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. "task_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 = Task.objects.get(pk=int(jso['id']))
  31. print(form.cleaned_data)
  32. responce = super().form_valid(form)
  33. return responce
  34. def get_context_data(self, **kwargs):
  35. context = super().get_context_data(**kwargs)
  36. context.update(get_context(self.request, {
  37. 'title': _('Information about the service'),
  38. 'object': self.object,
  39. }))
  40. return context
  41. def get_success_url(self):
  42. return reverse('service_tariff')
  43. def test_func(self) -> bool or None:
  44. group_names = ('METASERVICE-ADMIN')
  45. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  46. return True
  47. return False
  48. class ServiceInformationUpdateView(UserPassesTestMixin, UpdateView):
  49. model = Service
  50. form_class = ServiceInformationUpdateForm
  51. template_name = "SharixAdmin/service_information_form.html"
  52. def get_context_data(self, **kwargs):
  53. context = super().get_context_data(**kwargs)
  54. context.update(get_context(self.request, {
  55. 'title': 'Information about the service',
  56. 'object': self.object,
  57. }))
  58. return context
  59. def get_success_url(self):
  60. return reverse('test-page')
  61. def test_func(self) -> bool or None:
  62. group_names = ('METASERVICE-ADMIN')
  63. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  64. return True
  65. return False