views.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from django.urls import reverse_lazy, reverse
  2. import requests
  3. from django.http import HttpResponse
  4. from webservice_running.apps import api
  5. from core.settings import API_URL
  6. from webservice_running.forms import OrderRegForm, RegisterUserForm, OrderLocalForm
  7. from webservice_running.models import OrdersLocal
  8. from django.shortcuts import redirect, render
  9. from django.views.generic.edit import CreateView
  10. from django.utils.translation import gettext as _
  11. #from django.contrib.auth.mixins import UserPassesTestMixin
  12. def order_reg(request):
  13. if request.method == 'POST':
  14. form = OrderRegForm(request.POST)
  15. if form.is_valid():
  16. data = request.POST
  17. responce = requests.post(f'{API_URL}/webservice/api/webservice/', data = data, headers=api.headers)
  18. return HttpResponse(responce.text)
  19. else:
  20. form = OrderRegForm()
  21. return render(request, 'webservice_running/order_reg.html', {'form': form})
  22. def registration(request):
  23. if request.method == 'POST':
  24. form = RegisterUserForm(request.POST)
  25. if form.is_valid():
  26. form.save()
  27. return redirect('authweb')
  28. else:
  29. form = RegisterUserForm()
  30. return render(request, 'webservice_running/registration.html', {'form': form})
  31. #class order_new(UserPassesTestMixin, CreateView):
  32. class order_new(CreateView):
  33. model = OrdersLocal
  34. form_class = OrderLocalForm
  35. template_name = "webservice_running/order_local.html"
  36. def get_context_data(self, **kwargs):
  37. page_context = super().get_context_data(**kwargs)
  38. base_context = {
  39. 'title': _('Форма создания заказа'),
  40. 'object': self.object,
  41. 'current_page': 'order_new'
  42. }
  43. context = dict(list(base_context.items()) + list(page_context.items()))
  44. return context
  45. def testing(self, queryset, is_descending):
  46. queryset = queryset.annotate.order_by("-" if is_descending else "")
  47. return (queryset, True)
  48. def get_success_url(self):
  49. return reverse('webservice_running:order_new')
  50. def test_func(self) -> bool or None:
  51. group_names = ('METASERVICE-ADMIN')
  52. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  53. return True
  54. return False