partners.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 PartnersTable
  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 PartnersListView(UserPassesTestMixin, SingleTableView):
  10. table_class = PartnersTable
  11. queryset = Company.objects.all()
  12. template_name = 'SharixAdmin/partners.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_partners_status(request):
  28. if request.method == 'POST':
  29. partners_id = request.POST.get('partners_id')
  30. new_status = request.POST.get('new_status')
  31. partners = Company.objects.get(pk=partners_id)
  32. partners.status = new_status
  33. partners.save()
  34. return JsonResponse({'status': 'success'})
  35. else:
  36. return JsonResponse({'status': 'error'})