from django_tables2 import SingleTableView from django.contrib.auth.mixins import UserPassesTestMixin from django.views.generic.edit import UpdateView, CreateView, DeleteView from SharixAdmin.tables import TasksTable from django.contrib.auth.decorators import login_required from webservice_running.models import Tasks from SharixAdmin.forms import TasksCreateForm from django.http import JsonResponse from django.urls import reverse from SharixAdmin.utils import group_required from django.utils.translation import gettext as _ from .base import BaseView class TaskCreate(UserPassesTestMixin, CreateView): model = Tasks form_class = TasksCreateForm template_name = "SharixAdmin/task_management_form.html" page_title = _('Управление задачами') page_name = 'task_management' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ 'object': self.object, }) return context def get_success_url(self): return reverse('task_management') def test_func(self) -> bool or None: group_names = ('METASERVICE-ADMIN') if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser: return True return False class TaskListView(UserPassesTestMixin, BaseView, SingleTableView): table_class = TasksTable queryset = Tasks.objects.all() template_name = 'SharixAdmin/task_management.html' page_title = _('Управление задачами') page_name = 'task_management' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ 'object_list': context['object_list'], }) return context def testing(self, queryset, is_descending): queryset = queryset.annotate.order_by("-" if is_descending else "") return (queryset, True) def test_func(self) -> bool or None: group_names = ('METASERVICE-ADMIN') if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser: return True return False class TasksUpdateView(UserPassesTestMixin, UpdateView): model = Tasks form_class = TasksCreateForm template_name = "SharixAdmin/task_management_form.html" page_title = _('Управление задачами') page_name = 'task_management' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ 'object': self.object, }) return context def test_func(self) -> bool or None: group_names = ('METASERVICE-ADMIN') if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser: return True return False def get_success_url(self): return reverse('task_management') class TasksDelete(UserPassesTestMixin, DeleteView): model = Tasks template_name = "SharixAdmin/task_management_delete.html" page_title = _('Управление задачами') page_name = 'task_management' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ 'object': self.object, }) return context def get_success_url(self): return reverse('task_management') def test_func(self) -> bool or None: group_names = ('METASERVICE-ADMIN') if bool(self.request.user.groups.filter(name=group_names)) or self.request.user.is_superuser: return True return False @login_required @group_required('METASERVICE-ADMIN') def change_tasks_status(request): if request.method == 'POST': task_id = request.POST.get('task_id') new_status = request.POST.get('new_status') tasks = Tasks.objects.get(pk=task_id) tasks.status = new_status tasks.save() return JsonResponse({'status': 'success'}) else: return JsonResponse({'status': 'error'})