service.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from django_tables2 import SingleTableView
  2. from SharixAdmin.groups import group_required
  3. from SharixAdmin.tables import ServiceTable
  4. from metaservicesynced.models import Service
  5. from django.contrib.auth.decorators import login_required
  6. from SharixAdmin.views.context import get_context
  7. from django.contrib.auth.mixins import UserPassesTestMixin
  8. class ServiceListView(UserPassesTestMixin, SingleTableView):
  9. table_class = ServiceTable
  10. queryset = Service.objects.all()
  11. template_name = 'SharixAdmin/service.html'
  12. def get_context_data(self, **kwargs):
  13. context = super().get_context_data(**kwargs)
  14. context.update(get_context(self.request, {
  15. 'title': 'Тарифы',
  16. 'object_list': context['object_list'],
  17. }))
  18. return context
  19. def test_func(self) -> bool or None:
  20. group_names = ('PROVIDER')
  21. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  22. return True
  23. return False
  24. @login_required
  25. @group_required('PROVIDER')
  26. def change_service_status(request):
  27. if request.method == 'POST':
  28. service_id = request.POST.get('service_id')
  29. new_status = request.POST.get('new_status')
  30. service = Service.objects.get(pk=service_id)
  31. service.status = new_status
  32. service.save()