user_info.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from django.contrib.auth import get_user_model
  2. from django.views.generic.edit import DeleteView
  3. from django_tables2 import SingleTableView
  4. from SharixAdmin.tables import UserInfoTable
  5. from django.contrib.auth.mixins import UserPassesTestMixin
  6. from django.contrib.auth.models import Group
  7. from django.urls import reverse
  8. from django.utils.translation import gettext as _
  9. class UserListView(UserPassesTestMixin, SingleTableView):
  10. table_class = UserInfoTable
  11. queryset = get_user_model().objects.all()
  12. template_name = 'SharixAdmin/user_information.html'
  13. def get_context_data(self, **kwargs):
  14. context = super().get_context_data(**kwargs)
  15. context.update({
  16. 'title': _('Управление правами'),
  17. 'object_list': context['object_list'],
  18. 'groups': Group.objects.all(),
  19. 'current_page': "user_information"
  20. })
  21. return context
  22. def testing(self, queryset, is_descending):
  23. queryset = queryset.annotate.order_by("-" if is_descending else "")
  24. return (queryset, True)
  25. def test_func(self) -> bool or None:
  26. group_names = ('METASERVICE-ADMIN')
  27. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  28. return True
  29. return False
  30. class UserDelete(UserPassesTestMixin, DeleteView):
  31. model = get_user_model()
  32. template_name = "SharixAdmin/user_information_delete.html"
  33. def get_context_data(self, **kwargs):
  34. context = super().get_context_data(**kwargs)
  35. context.update({
  36. 'title': 'Управление правами',
  37. 'object': self.object,
  38. "current_page": "user_information"
  39. })
  40. return context
  41. def get_success_url(self):
  42. return reverse('user_information')
  43. def test_func(self) -> bool or None:
  44. group_names = ('METASERVICE-ADMIN')
  45. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  46. return True
  47. return False