partner_info.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from django.shortcuts import render
  2. from SharixAdmin.forms import PartnerInformationCreateForm, PartnerInformationUpdateForm
  3. from SharixAdmin.groups import group_required
  4. from metaservicesynced.models import Company
  5. from django.contrib.auth.mixins import UserPassesTestMixin
  6. from django.views.generic.edit import UpdateView, CreateView
  7. from SharixAdmin.views.context import get_context
  8. from django.urls import reverse
  9. from django.utils.translation import gettext as _
  10. from core.utils.AuthAPI import AuthAPI
  11. from tickets.models import Task
  12. from core.config import API_URL
  13. api = AuthAPI("89855703300", "12345")
  14. import requests
  15. from django.urls import reverse_lazy
  16. class PartnerInformationCreate(UserPassesTestMixin, CreateView):
  17. model = Company
  18. form_class = PartnerInformationCreateForm
  19. template_name = "SharixAdmin/partner_information_form.html"
  20. success_url = reverse_lazy("SharixAdmin:partners")
  21. def form_valid(self, form):
  22. form.instance.representative_id = self.request.user
  23. new_ticket = {
  24. "task_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. print(resp.content)
  33. print(resp.json())
  34. print(resp)
  35. form.instance.ticket_status = Task.objects.get(pk=int(jso['id']))
  36. print(form.cleaned_data)
  37. responce = super().form_valid(form)
  38. return responce
  39. def get_context_data(self, **kwargs):
  40. context = super().get_context_data(**kwargs)
  41. context.update(get_context(self.request, {
  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 = "SharixAdmin/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 get_success_url(self):
  65. return reverse('test-page')
  66. def test_func(self) -> bool or None:
  67. group_names = ('PARTNER-ADMIN')
  68. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  69. return True
  70. return False
  71. def partner_information(request):
  72. context = get_context(request, {
  73. 'title':_('Partner Information'),
  74. })
  75. return render(request, 'SharixAdmin/partner_information.html', context)