partners.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. from django.http import HttpResponse
  10. class PartnersListView(UserPassesTestMixin, SingleTableView):
  11. table_class = PartnersTable
  12. queryset = Company.objects.all()
  13. template_name = 'SharixAdmin/partners.html'
  14. def get_context_data(self, **kwargs):
  15. context = super().get_context_data(**kwargs)
  16. context.update(get_context(self.request, {
  17. 'title': 'Партнеры',
  18. 'object_list': context['object_list'],
  19. }))
  20. return context
  21. def test_func(self) -> bool or None:
  22. group_names = ('METASERVICE-ADMIN')
  23. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  24. return True
  25. return False
  26. @login_required
  27. @group_required('METASERVICE-ADMIN')
  28. def change_partners_status(request):
  29. if request.method == 'POST':
  30. partners_id = request.POST.get('partners_id')
  31. new_status = request.POST.get('new_status')
  32. partners = Company.objects.get(pk=partners_id)
  33. partners.status = new_status
  34. partners.save()
  35. return JsonResponse({'status': 'success'})
  36. else:
  37. return JsonResponse({'status': 'error'})