partners.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from django_tables2 import SingleTableView
  2. from django.contrib.auth.mixins import UserPassesTestMixin
  3. from sharix_admin.utils import group_required
  4. from sharix_admin.tables import PartnersTable
  5. from django.contrib.auth.decorators import login_required
  6. from dbsynce.models import Company
  7. from django.http import JsonResponse
  8. from django.utils.translation import gettext as _
  9. from .base import BaseView
  10. class PartnersListView(UserPassesTestMixin, BaseView, SingleTableView):
  11. page_title = _('Partners')
  12. page_name = 'partners'
  13. table_class = PartnersTable
  14. queryset = Company.objects.all()
  15. template_name = 'sharix_admin/partners.html'
  16. def get_context_data(self, **kwargs):
  17. context = super().get_context_data(**kwargs)
  18. context.update({
  19. 'object_list': context['object_list'],
  20. })
  21. return context
  22. def test_func(self):
  23. group_names = ('METASERVICE-ADMIN')
  24. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  25. return True
  26. return False
  27. @login_required
  28. @group_required('METASERVICE-ADMIN')
  29. def change_partners_status(request):
  30. if request.method == 'POST':
  31. partners_id = request.POST.get('partners_id')
  32. new_status = request.POST.get('new_status')
  33. partners = Company.objects.get(pk=partners_id)
  34. partners.status = new_status
  35. partners.save()
  36. return JsonResponse({'status': 'success'})
  37. else:
  38. return JsonResponse({'status': 'error'})