from django_tables2 import SingleTableView from django.views.generic.edit import UpdateView, CreateView, DeleteView from SharixAdmin.tables import CommentsTable from django.contrib.auth.mixins import UserPassesTestMixin from SharixAdmin.forms import CommentsCreateForm, ServiceTypeUpdateForm from dbsynce.models import Comments from django.urls import reverse from django.utils.translation import gettext as _ from SharixAdmin.utils import group_required from django.contrib.auth.decorators import login_required from django.http import JsonResponse class CommentsCreate(UserPassesTestMixin, CreateView): model = Comments form_class = CommentsCreateForm template_name = "SharixAdmin/tech_support_form.html" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ 'title': _('Техническая поддержка'), 'object': self.object, "current_page": "tech_support" }) return context def get_success_url(self): return reverse('tech_support') 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: return True return False class CommentsListView(UserPassesTestMixin, SingleTableView): table_class = CommentsTable queryset = Comments.objects.all() template_name = 'SharixAdmin/comments.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ 'title': 'Комментарии', 'object_list': context['object_list'], "current_page": "comments" }) 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 CommentsUpdateView(UserPassesTestMixin, UpdateView): model = Comments form_class = CommentsCreateForm template_name = "SharixAdmin/tech_support_form.html" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ 'title': _('Комментарии'), 'object': self.object, "current_page": "tech_support" }) 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: return True return False def get_success_url(self): return reverse('tech_support') class CommentsDelete(UserPassesTestMixin, DeleteView): model = Comments template_name = "SharixAdmin/comments_delete.html" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context.update({ 'title': 'Комментарии', 'object': self.object, "current_page": "comments" }) return context def get_success_url(self): return reverse('comments') 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_comments_status(request): if request.method == 'POST': comments_id = request.POST.get('comments_id') new_status = request.POST.get('new_status') comments = Comments.objects.get(pk=comments_id) comments.status = new_status comments.save() return JsonResponse({'status': 'success'}) else: return JsonResponse({'status': 'error'})