task_management.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from django_tables2 import SingleTableView
  2. from django.contrib.auth.mixins import UserPassesTestMixin
  3. from django.views.generic.edit import UpdateView, CreateView, DeleteView
  4. from SharixAdmin.tables import TasksTable
  5. from django.contrib.auth.decorators import login_required
  6. from webservice_running.models import Tasks
  7. from SharixAdmin.forms import TasksCreateForm
  8. from django.http import JsonResponse
  9. from django.urls import reverse
  10. from SharixAdmin.utils import group_required
  11. from django.utils.translation import gettext as _
  12. from .base import BaseView
  13. class TaskCreate(UserPassesTestMixin, CreateView):
  14. model = Tasks
  15. form_class = TasksCreateForm
  16. template_name = "SharixAdmin/task_management_form.html"
  17. page_title = _('Управление задачами')
  18. page_name = 'task_management'
  19. def get_context_data(self, **kwargs):
  20. context = super().get_context_data(**kwargs)
  21. context.update({
  22. 'object': self.object,
  23. })
  24. return context
  25. def get_success_url(self):
  26. return reverse('task_management')
  27. def test_func(self) -> bool or None:
  28. group_names = ('METASERVICE-ADMIN')
  29. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  30. return True
  31. return False
  32. class TaskListView(UserPassesTestMixin, BaseView, SingleTableView):
  33. table_class = TasksTable
  34. queryset = Tasks.objects.all()
  35. template_name = 'SharixAdmin/task_management.html'
  36. page_title = _('Управление задачами')
  37. page_name = 'task_management'
  38. def get_context_data(self, **kwargs):
  39. context = super().get_context_data(**kwargs)
  40. context.update({
  41. 'object_list': context['object_list'],
  42. })
  43. return context
  44. def testing(self, queryset, is_descending):
  45. queryset = queryset.annotate.order_by("-" if is_descending else "")
  46. return (queryset, True)
  47. def test_func(self) -> bool or None:
  48. group_names = ('METASERVICE-ADMIN')
  49. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  50. return True
  51. return False
  52. class TasksUpdateView(UserPassesTestMixin, UpdateView):
  53. model = Tasks
  54. form_class = TasksCreateForm
  55. template_name = "SharixAdmin/task_management_form.html"
  56. page_title = _('Управление задачами')
  57. page_name = 'task_management'
  58. def get_context_data(self, **kwargs):
  59. context = super().get_context_data(**kwargs)
  60. context.update({
  61. 'object': self.object,
  62. })
  63. return context
  64. def test_func(self) -> bool or None:
  65. group_names = ('METASERVICE-ADMIN')
  66. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  67. return True
  68. return False
  69. def get_success_url(self):
  70. return reverse('task_management')
  71. class TasksDelete(UserPassesTestMixin, DeleteView):
  72. model = Tasks
  73. template_name = "SharixAdmin/task_management_delete.html"
  74. page_title = _('Управление задачами')
  75. page_name = 'task_management'
  76. def get_context_data(self, **kwargs):
  77. context = super().get_context_data(**kwargs)
  78. context.update({
  79. 'object': self.object,
  80. })
  81. return context
  82. def get_success_url(self):
  83. return reverse('task_management')
  84. def test_func(self) -> bool or None:
  85. group_names = ('METASERVICE-ADMIN')
  86. if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser:
  87. return True
  88. return False
  89. @login_required
  90. @group_required('METASERVICE-ADMIN')
  91. def change_tasks_status(request):
  92. if request.method == 'POST':
  93. task_id = request.POST.get('task_id')
  94. new_status = request.POST.get('new_status')
  95. tasks = Tasks.objects.get(pk=task_id)
  96. tasks.status = new_status
  97. tasks.save()
  98. return JsonResponse({'status': 'success'})
  99. else:
  100. return JsonResponse({'status': 'error'})