resource.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 ResourceTable
  5. from django.contrib.auth.decorators import login_required
  6. from dbsynce.models import Resource
  7. from django.http import JsonResponse
  8. from django.utils.translation import gettext as _
  9. class ResourceListView(UserPassesTestMixin, SingleTableView):
  10. table_class = ResourceTable
  11. queryset = Resource.objects.all()
  12. template_name = 'SharixAdmin/resource.html'
  13. def get_context_data(self, **kwargs):
  14. context = super().get_context_data(**kwargs)
  15. context.update({
  16. 'title': _('Resources'),
  17. 'object_list': context['object_list'],
  18. })
  19. return context
  20. def test_func(self) -> bool or None:
  21. group_names = ('PARTNER-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('PARTNER-ADMIN')
  27. def change_resource_status(request):
  28. if request.method == 'POST':
  29. resource_id = request.POST.get('resource_id')
  30. new_status = request.POST.get('new_status')
  31. resource = Resource.objects.get(pk=resource_id)
  32. resource.status = new_status
  33. resource.save()
  34. return JsonResponse({'status': 'success'})
  35. else:
  36. return JsonResponse({'status': 'error'})