cooperation.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from django_tables2 import SingleTableView
  2. from django.contrib.auth.mixins import UserPassesTestMixin
  3. from SharixAdmin.groups import group_required
  4. from SharixAdmin.tables import CooperationTable
  5. from django.contrib.auth.decorators import login_required
  6. from metaservicesynced.models import Company
  7. from SharixAdmin.views.context import get_context
  8. from django.http import JsonResponse
  9. class CooperationListView(UserPassesTestMixin, SingleTableView):
  10. table_class = CooperationTable
  11. queryset = Company.objects.all()
  12. template_name = 'SharixAdmin/cooperation.html'
  13. def get_context_data(self, **kwargs):
  14. context = super().get_context_data(**kwargs)
  15. context.update(get_context(self.request, {
  16. 'title': 'Сотрудничество',
  17. 'object_list': context['object_list'],
  18. }))
  19. return context
  20. def test_func(self) -> bool or None:
  21. group_names = ('METASERVICE-ADMIN')
  22. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  23. return True
  24. return False
  25. @login_required
  26. @group_required('METASERVICE-ADMIN')
  27. def change_cooperation_status(request):
  28. if request.method == 'POST':
  29. cooperation_id = request.POST.get('cooperation_id')
  30. new_status = request.POST.get('new_status')
  31. cooperation = Company.objects.get(pk=cooperation_id)
  32. cooperation.status = new_status
  33. coopertion.save()
  34. return JsonResponse({'status': 'success'})
  35. else:
  36. return JsonResponse({'status': 'error'})