1
0

comments.py 4.1 KB

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