Sith/pedagogy/views.py

235 lines
7.5 KiB
Python
Raw Normal View History

2019-05-16 14:51:30 +00:00
#
# Copyright 2019
2019-05-16 14:51:30 +00:00
# - Sli <antoine@bartuccio.fr>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
2024-06-24 11:07:36 +00:00
from django.conf import settings
2025-01-16 14:10:46 +00:00
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.db.models import Exists, OuterRef
2024-06-24 11:07:36 +00:00
from django.shortcuts import get_object_or_404
from django.urls import reverse, reverse_lazy
2019-06-15 22:04:36 +00:00
from django.views.generic import (
CreateView,
DeleteView,
FormView,
2024-07-18 18:23:30 +00:00
TemplateView,
2024-06-24 11:07:36 +00:00
UpdateView,
2019-06-15 22:04:36 +00:00
)
2019-07-05 18:11:33 +00:00
2025-01-16 14:10:46 +00:00
from core.auth.mixins import PermissionOrAuthorRequiredMixin
from core.models import Notification, User
from core.views import DetailFormView
2019-06-20 10:15:12 +00:00
from pedagogy.forms import (
UVCommentForm,
UVCommentModerationForm,
2024-06-24 11:07:36 +00:00
UVCommentReportForm,
UVForm,
2019-06-20 10:15:12 +00:00
)
2024-07-18 18:23:30 +00:00
from pedagogy.models import UV, UVComment, UVCommentReport
2019-06-15 22:04:36 +00:00
2019-05-16 14:51:30 +00:00
2025-01-16 14:10:46 +00:00
class UVDetailFormView(PermissionRequiredMixin, DetailFormView):
2024-07-12 07:34:16 +00:00
"""Display every comment of an UV and detailed infos about it.
Allow to comment the UV.
2019-05-16 14:51:30 +00:00
"""
2019-06-16 00:19:56 +00:00
model = UV
pk_url_kwarg = "uv_id"
template_name = "pedagogy/uv_detail.jinja"
form_class = UVCommentForm
2025-01-16 14:10:46 +00:00
permission_required = "pedagogy.view_uv"
def has_permission(self):
if self.request.method == "POST" and not self.request.user.has_perm(
"pedagogy.add_uvcomment"
):
# if it's a POST request, the user is trying to add a new UVComment
# thus he also needs the "add_uvcomment" permission
return False
return super().has_permission()
2019-05-16 14:51:30 +00:00
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
kwargs["author_id"] = self.request.user.id
2025-01-16 14:10:46 +00:00
kwargs["uv_id"] = self.object.id
kwargs["is_creation"] = True
return kwargs
def form_valid(self, form):
form.save()
2024-06-27 12:46:43 +00:00
return super().form_valid(form)
2024-07-18 18:23:30 +00:00
def get_context_data(self, **kwargs):
return super().get_context_data(**kwargs) | {
2025-01-16 14:10:46 +00:00
"comments": list(
self.object.comments.viewable_by(self.request.user)
.annotate_is_reported()
.select_related("author")
.order_by("-publish_date")
2024-07-18 18:23:30 +00:00
)
}
2025-01-16 14:10:46 +00:00
def get_success_url(self):
# once the new uv comment has been saved
# redirect to the same page we are currently
return self.request.path
2019-05-16 14:51:30 +00:00
2025-01-16 14:10:46 +00:00
class UVCommentUpdateView(PermissionOrAuthorRequiredMixin, UpdateView):
2024-07-12 07:34:16 +00:00
"""Allow edit of a given comment."""
2019-05-16 14:51:30 +00:00
model = UVComment
form_class = UVCommentForm
pk_url_kwarg = "comment_id"
template_name = "core/edit.jinja"
2025-01-16 14:10:46 +00:00
permission_required = "pedagogy.change_uvcomment"
author_field = "author"
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
2025-01-16 14:10:46 +00:00
kwargs["author_id"] = self.object.author_id
kwargs["uv_id"] = self.object.uv_id
kwargs["is_creation"] = False
return kwargs
def get_success_url(self):
2025-01-16 14:10:46 +00:00
return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.object.uv_id})
2025-01-16 14:10:46 +00:00
class UVCommentDeleteView(PermissionOrAuthorRequiredMixin, DeleteView):
2024-07-12 07:34:16 +00:00
"""Allow delete of a given comment."""
model = UVComment
pk_url_kwarg = "comment_id"
template_name = "core/delete_confirm.jinja"
2025-01-16 14:10:46 +00:00
permission_required = "pedagogy.delete_uvcomment"
author_field = "author"
def get_success_url(self):
2025-01-16 14:10:46 +00:00
return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.object.uv_id})
2019-05-16 14:51:30 +00:00
2025-01-16 14:10:46 +00:00
class UVGuideView(PermissionRequiredMixin, TemplateView):
2024-07-12 07:34:16 +00:00
"""UV guide main page."""
2019-05-16 14:51:30 +00:00
2019-06-15 21:31:31 +00:00
template_name = "pedagogy/guide.jinja"
2025-01-16 14:10:46 +00:00
permission_required = "pedagogy.view_uv"
2019-05-16 14:51:30 +00:00
class UVCommentReportCreateView(PermissionRequiredMixin, CreateView):
2024-07-12 07:34:16 +00:00
"""Create a new report for an inapropriate comment."""
2019-05-16 14:51:30 +00:00
model = UVCommentReport
form_class = UVCommentReportForm
template_name = "core/edit.jinja"
permission_required = "pedagogy.add_uvcommentreport"
2019-05-16 14:51:30 +00:00
def dispatch(self, request, *args, **kwargs):
self.uv_comment = get_object_or_404(UVComment, pk=kwargs["comment_id"])
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
2019-05-16 14:51:30 +00:00
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
kwargs["reporter_id"] = self.request.user.id
kwargs["comment_id"] = self.uv_comment.id
return kwargs
2019-05-16 14:51:30 +00:00
def form_valid(self, form):
2024-06-27 12:46:43 +00:00
resp = super().form_valid(form)
# Send a message to moderation admins
unread_notif_subquery = Notification.objects.filter(
user=OuterRef("pk"), type="PEDAGOGY_MODERATION", viewed=False
)
for user in User.objects.filter(
~Exists(unread_notif_subquery),
groups__id__in=[settings.SITH_GROUP_PEDAGOGY_ADMIN_ID],
):
Notification.objects.create(
user=user,
url=reverse("pedagogy:moderation"),
type="PEDAGOGY_MODERATION",
)
return resp
def get_success_url(self):
2025-01-16 14:10:46 +00:00
return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.uv_comment.uv_id})
2019-05-16 14:51:30 +00:00
2025-01-16 14:10:46 +00:00
class UVModerationFormView(PermissionRequiredMixin, FormView):
2024-07-12 07:34:16 +00:00
"""Moderation interface (Privileged)."""
2019-05-16 14:51:30 +00:00
2019-06-20 10:15:12 +00:00
form_class = UVCommentModerationForm
template_name = "pedagogy/moderation.jinja"
2025-01-16 14:10:46 +00:00
permission_required = "pedagogy.delete_uvcomment"
success_url = reverse_lazy("pedagogy:moderation")
2019-06-20 10:15:12 +00:00
def form_valid(self, form):
form_clean = form.clean()
accepted = form_clean.get("accepted_reports", [])
if len(accepted) > 0: # delete the reported comments
UVComment.objects.filter(reports__in=accepted).delete()
denied = form_clean.get("denied_reports", [])
if len(denied) > 0: # delete the comments themselves
UVCommentReport.objects.filter(id__in={d.id for d in denied}).delete()
2024-06-27 12:46:43 +00:00
return super().form_valid(form)
2019-06-20 10:15:12 +00:00
2019-05-16 14:51:30 +00:00
class UVCreateView(PermissionRequiredMixin, CreateView):
2024-07-12 07:34:16 +00:00
"""Add a new UV (Privileged)."""
2019-05-16 14:51:30 +00:00
model = UV
form_class = UVForm
template_name = "pedagogy/uv_edit.jinja"
permission_required = "pedagogy.add_uv"
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
kwargs["author_id"] = self.request.user.id
return kwargs
2019-05-16 14:51:30 +00:00
2025-01-16 14:10:46 +00:00
class UVDeleteView(PermissionRequiredMixin, DeleteView):
2024-07-12 07:34:16 +00:00
"""Allow to delete an UV (Privileged)."""
2019-05-16 14:51:30 +00:00
2019-06-15 22:04:36 +00:00
model = UV
pk_url_kwarg = "uv_id"
template_name = "core/delete_confirm.jinja"
2025-01-16 14:10:46 +00:00
permission_required = "pedagogy.delete_uv"
success_url = reverse_lazy("pedagogy:guide")
2019-06-15 22:04:36 +00:00
2025-01-16 14:10:46 +00:00
class UVUpdateView(PermissionRequiredMixin, UpdateView):
2024-07-12 07:34:16 +00:00
"""Allow to edit an UV (Privilegied)."""
2019-06-15 22:04:36 +00:00
model = UV
form_class = UVForm
pk_url_kwarg = "uv_id"
template_name = "pedagogy/uv_edit.jinja"
2025-01-16 14:10:46 +00:00
permission_required = "pedagogy.change_uv"
2019-06-15 22:04:36 +00:00
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
obj = self.get_object()
2025-01-16 14:10:46 +00:00
kwargs["author_id"] = obj.author_id
2019-06-15 22:04:36 +00:00
return kwargs