Sith/trombi/views.py

544 lines
18 KiB
Python
Raw Normal View History

2023-04-06 11:08:42 +00:00
# -*- coding:utf-8 -*-
2017-05-09 21:42:01 +00:00
#
2023-04-06 11:08:42 +00:00
# Copyright 2023 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
# All contributors are listed in the CONTRIBUTORS file.
2017-05-09 21:42:01 +00:00
#
2023-04-06 11:08:42 +00:00
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
2017-05-09 21:42:01 +00:00
#
2023-04-06 11:08:42 +00:00
# You can find the whole source code at https://github.com/ae-utbm/sith3
2017-05-09 21:42:01 +00:00
#
2023-04-06 11:08:42 +00:00
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
2017-05-09 21:42:01 +00:00
#
2023-04-06 11:08:42 +00:00
# PREVIOUSLY LICENSED UNDER THE MIT LICENSE,
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE.old
# OR WITHIN THE LOCAL FILE "LICENSE.old"
2017-05-09 21:42:01 +00:00
#
2024-06-24 11:07:36 +00:00
from datetime import date
from ajax_select.fields import AutoCompleteSelectField
2017-05-09 21:42:01 +00:00
from django import forms
2017-05-12 16:30:06 +00:00
from django.conf import settings
from django.core.exceptions import PermissionDenied
2024-06-24 11:07:36 +00:00
from django.forms.models import modelform_factory
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse, reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, RedirectView, TemplateView, View
from django.views.generic.edit import CreateView, DeleteView, UpdateView
2017-05-09 21:42:01 +00:00
2024-06-24 11:07:36 +00:00
from club.models import Club
from core.models import User
2018-10-04 19:29:19 +00:00
from core.views import (
2024-06-24 11:07:36 +00:00
CanCreateMixin,
2018-10-04 19:29:19 +00:00
CanEditMixin,
CanEditPropMixin,
2024-06-24 11:07:36 +00:00
CanViewMixin,
2018-10-04 19:29:19 +00:00
QuickNotifMixin,
2024-06-24 11:07:36 +00:00
TabedViewMixin,
2019-10-02 12:56:04 +00:00
UserIsLoggedMixin,
2018-10-04 19:29:19 +00:00
)
2024-06-24 11:07:36 +00:00
from core.views.forms import SelectDate
from trombi.models import Trombi, TrombiClubMembership, TrombiComment, TrombiUser
2017-05-09 21:42:01 +00:00
2017-06-12 08:10:52 +00:00
class TrombiTabsMixin(TabedViewMixin):
def get_tabs_title(self):
return _("Trombi")
def get_list_of_tabs(self):
tab_list = []
2018-10-04 19:29:19 +00:00
tab_list.append(
{"url": reverse("trombi:user_tools"), "slug": "tools", "name": _("Tools")}
)
if hasattr(self.request.user, "trombi_user"):
tab_list.append(
{
"url": reverse("trombi:profile"),
"slug": "profile",
"name": _("My profile"),
}
)
tab_list.append(
{
"url": reverse("trombi:pictures"),
"slug": "pictures",
"name": _("My pictures"),
}
)
try:
trombi = self.request.user.trombi_user.trombi
if self.request.user.is_owner(trombi):
2018-10-04 19:29:19 +00:00
tab_list.append(
{
"url": reverse(
"trombi:detail", kwargs={"trombi_id": trombi.id}
),
"slug": "admin_tools",
"name": _("Admin tools"),
}
)
2017-06-12 08:10:52 +00:00
except:
pass
return tab_list
2017-06-12 08:10:52 +00:00
class UserIsInATrombiMixin(View):
"""
This view check if the requested user has a trombi_user attribute
"""
def dispatch(self, request, *args, **kwargs):
if not hasattr(self.request.user, "trombi_user"):
raise Http404()
return super(UserIsInATrombiMixin, self).dispatch(request, *args, **kwargs)
2017-05-10 20:17:05 +00:00
class TrombiForm(forms.ModelForm):
2017-05-09 21:42:01 +00:00
class Meta:
2017-05-10 20:17:05 +00:00
model = Trombi
2018-10-04 19:29:19 +00:00
fields = [
"subscription_deadline",
"comments_deadline",
"max_chars",
"show_profiles",
]
widgets = {"subscription_deadline": SelectDate, "comments_deadline": SelectDate}
2017-06-12 08:10:52 +00:00
2017-05-09 21:42:01 +00:00
2017-06-13 08:55:12 +00:00
class TrombiCreateView(CanCreateMixin, CreateView):
2017-05-09 21:42:01 +00:00
"""
2017-05-10 20:17:05 +00:00
Create a trombi for a club
2017-05-09 21:42:01 +00:00
"""
2018-10-04 19:29:19 +00:00
2017-05-10 20:17:05 +00:00
model = Trombi
form_class = TrombiForm
2018-10-04 19:29:19 +00:00
template_name = "core/create.jinja"
2017-05-09 21:42:01 +00:00
def post(self, request, *args, **kwargs):
"""
Affect club
"""
form = self.get_form()
if form.is_valid():
2018-10-04 19:29:19 +00:00
club = get_object_or_404(Club, id=self.kwargs["club_id"])
2017-05-09 21:42:01 +00:00
form.instance.club = club
ret = self.form_valid(form)
return ret
else:
return self.form_invalid(form)
2017-06-12 08:10:52 +00:00
class TrombiEditView(CanEditPropMixin, TrombiTabsMixin, UpdateView):
2017-05-10 20:17:05 +00:00
model = Trombi
form_class = TrombiForm
2018-10-04 19:29:19 +00:00
template_name = "core/edit.jinja"
pk_url_kwarg = "trombi_id"
current_tab = "admin_tools"
2017-05-09 21:42:01 +00:00
def get_success_url(self):
2017-06-12 08:10:52 +00:00
return super(TrombiEditView, self).get_success_url() + "?qn_success"
class AddUserForm(forms.Form):
2018-10-04 19:29:19 +00:00
user = AutoCompleteSelectField(
"users", required=True, label=_("Select user"), help_text=None
)
class TrombiDetailView(CanEditMixin, QuickNotifMixin, TrombiTabsMixin, DetailView):
2017-05-10 20:17:05 +00:00
model = Trombi
2018-10-04 19:29:19 +00:00
template_name = "trombi/detail.jinja"
pk_url_kwarg = "trombi_id"
current_tab = "admin_tools"
2017-05-09 21:42:01 +00:00
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = AddUserForm(request.POST)
if form.is_valid():
try:
2018-10-04 19:29:19 +00:00
TrombiUser(user=form.cleaned_data["user"], trombi=self.object).save()
self.quick_notif_list.append("qn_success")
2018-10-04 19:29:19 +00:00
except: # We don't care about duplicate keys
self.quick_notif_list.append("qn_fail")
return super(TrombiDetailView, self).get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
kwargs = super(TrombiDetailView, self).get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["form"] = AddUserForm()
return kwargs
2018-10-04 19:29:19 +00:00
class TrombiExportView(CanEditMixin, TrombiTabsMixin, DetailView):
model = Trombi
2018-10-04 19:29:19 +00:00
template_name = "trombi/export.jinja"
pk_url_kwarg = "trombi_id"
current_tab = "admin_tools"
2017-06-12 08:10:52 +00:00
2018-10-04 19:29:19 +00:00
class TrombiDeleteUserView(CanEditPropMixin, TrombiTabsMixin, DeleteView):
model = TrombiUser
2018-10-04 19:29:19 +00:00
pk_url_kwarg = "user_id"
template_name = "core/delete_confirm.jinja"
current_tab = "admin_tools"
2017-05-09 21:42:01 +00:00
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return (
reverse("trombi:detail", kwargs={"trombi_id": self.object.trombi.id})
+ "?qn_success"
)
2017-05-09 21:42:01 +00:00
2017-06-12 08:10:52 +00:00
2018-10-04 19:29:19 +00:00
class TrombiModerateCommentsView(
CanEditPropMixin, QuickNotifMixin, TrombiTabsMixin, DetailView
):
2017-05-12 16:30:06 +00:00
model = Trombi
2018-10-04 19:29:19 +00:00
template_name = "trombi/comment_moderation.jinja"
pk_url_kwarg = "trombi_id"
current_tab = "admin_tools"
2017-05-12 16:30:06 +00:00
def get_context_data(self, **kwargs):
kwargs = super(TrombiModerateCommentsView, self).get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["comments"] = TrombiComment.objects.filter(
is_moderated=False, author__trombi__id=self.object.id
).exclude(target__user__id=self.request.user.id)
2017-05-12 16:30:06 +00:00
return kwargs
2017-06-12 08:10:52 +00:00
2017-05-12 16:30:06 +00:00
class TrombiModerateForm(forms.Form):
reason = forms.CharField(help_text=_("Explain why you rejected the comment"))
action = forms.CharField(initial="delete", widget=forms.widgets.HiddenInput)
2017-06-12 08:10:52 +00:00
2017-05-12 16:30:06 +00:00
class TrombiModerateCommentView(DetailView):
model = TrombiComment
2018-10-04 19:29:19 +00:00
template_name = "core/edit.jinja"
pk_url_kwarg = "comment_id"
2017-05-12 16:30:06 +00:00
def dispatch(self, request, *args, **kwargs):
self.object = self.get_object()
if not request.user.is_owner(self.object.author.trombi):
raise Http404()
return super(TrombiModerateCommentView, self).dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
if "action" in request.POST:
2018-10-04 19:29:19 +00:00
if request.POST["action"] == "accept":
2017-05-12 16:30:06 +00:00
self.object.is_moderated = True
self.object.save()
2018-10-04 19:29:19 +00:00
return redirect(
reverse(
"trombi:moderate_comments",
kwargs={"trombi_id": self.object.author.trombi.id},
)
+ "?qn_success"
)
elif request.POST["action"] == "reject":
return super(TrombiModerateCommentView, self).get(
request, *args, **kwargs
)
elif request.POST["action"] == "delete" and "reason" in request.POST.keys():
2017-05-12 16:30:06 +00:00
self.object.author.user.email_user(
2017-06-12 08:10:52 +00:00
subject="[%s] %s" % (settings.SITH_NAME, _("Rejected comment")),
2018-10-04 19:29:19 +00:00
message=_(
'Your comment to %(target)s on the Trombi "%(trombi)s" was rejected for the following '
"reason: %(reason)s\n\n"
"Your comment was:\n\n%(content)s"
)
% {
"target": self.object.target.user.get_display_name(),
"trombi": self.object.author.trombi,
"reason": request.POST["reason"],
"content": self.object.content,
2017-06-12 08:10:52 +00:00
},
)
2017-05-12 16:30:06 +00:00
self.object.delete()
2018-10-04 19:29:19 +00:00
return redirect(
reverse(
"trombi:moderate_comments",
kwargs={"trombi_id": self.object.author.trombi.id},
)
+ "?qn_success"
)
2017-05-12 16:30:06 +00:00
raise Http404
def get_context_data(self, **kwargs):
kwargs = super(TrombiModerateCommentView, self).get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["form"] = TrombiModerateForm()
2017-05-12 16:30:06 +00:00
return kwargs
2018-10-04 19:29:19 +00:00
2017-05-09 21:42:01 +00:00
# User side
2017-06-12 08:10:52 +00:00
2017-05-11 16:33:45 +00:00
class TrombiModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
2018-10-04 19:29:19 +00:00
return _("%(name)s (deadline: %(date)s)") % {
"name": str(obj),
"date": str(obj.subscription_deadline),
}
2017-05-11 16:33:45 +00:00
2017-06-12 08:10:52 +00:00
2017-05-10 20:17:05 +00:00
class UserTrombiForm(forms.Form):
2018-10-04 19:29:19 +00:00
trombi = TrombiModelChoiceField(
Trombi.availables.all(),
required=False,
label=_("Select trombi"),
help_text=_(
"This allows you to subscribe to a Trombi. "
"Be aware that you can subscribe only once, so don't play with that, "
"or you will expose yourself to the admins' wrath!"
),
)
2017-06-12 08:10:52 +00:00
2017-05-09 21:42:01 +00:00
2019-10-02 12:56:04 +00:00
class UserTrombiToolsView(
QuickNotifMixin, TrombiTabsMixin, UserIsLoggedMixin, TemplateView
):
2017-05-09 21:42:01 +00:00
"""
2017-05-10 20:17:05 +00:00
Display a user's trombi tools
2017-05-09 21:42:01 +00:00
"""
2018-10-04 19:29:19 +00:00
2017-05-10 20:17:05 +00:00
template_name = "trombi/user_tools.jinja"
current_tab = "tools"
2017-05-09 21:42:01 +00:00
def post(self, request, *args, **kwargs):
2017-05-10 20:17:05 +00:00
self.form = UserTrombiForm(request.POST)
2017-05-09 21:42:01 +00:00
if self.form.is_valid():
if hasattr(request.user, "trombi_user"):
trombi_user = request.user.trombi_user
trombi_user.trombi = self.form.cleaned_data["trombi"]
else:
trombi_user = TrombiUser(
user=request.user, trombi=self.form.cleaned_data["trombi"]
)
2017-05-10 20:17:05 +00:00
trombi_user.save()
2018-10-04 19:29:19 +00:00
self.quick_notif_list += ["qn_success"]
2017-05-10 20:17:05 +00:00
return super(UserTrombiToolsView, self).get(request, *args, **kwargs)
2017-05-09 21:42:01 +00:00
def get_context_data(self, **kwargs):
2017-05-10 20:17:05 +00:00
kwargs = super(UserTrombiToolsView, self).get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["user"] = self.request.user
if not (
hasattr(self.request.user, "trombi_user")
and self.request.user.trombi_user.trombi
):
2018-10-04 19:29:19 +00:00
kwargs["subscribe_form"] = UserTrombiForm()
2017-05-11 16:33:45 +00:00
else:
2018-10-04 19:29:19 +00:00
kwargs["trombi"] = self.request.user.trombi_user.trombi
kwargs["date"] = date
2017-05-09 21:42:01 +00:00
return kwargs
2017-06-12 08:10:52 +00:00
class UserTrombiEditPicturesView(TrombiTabsMixin, UserIsInATrombiMixin, UpdateView):
model = TrombiUser
2018-10-04 19:29:19 +00:00
fields = ["profile_pict", "scrub_pict"]
template_name = "core/edit.jinja"
current_tab = "pictures"
def get_object(self):
return self.request.user.trombi_user
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse("trombi:user_tools") + "?qn_success"
2017-06-12 08:10:52 +00:00
class UserTrombiEditProfileView(
QuickNotifMixin, TrombiTabsMixin, UserIsInATrombiMixin, UpdateView
):
2017-05-10 19:59:22 +00:00
model = User
2018-10-04 19:29:19 +00:00
form_class = modelform_factory(
User,
fields=[
"second_email",
"phone",
"department",
"dpt_option",
"quote",
"parent_address",
],
labels={
"second_email": _("Personal email (not UTBM)"),
"phone": _("Phone"),
"parent_address": _("Native town"),
},
)
template_name = "trombi/edit_profile.jinja"
current_tab = "profile"
2017-05-10 19:59:22 +00:00
def get_object(self):
return self.request.user
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse("trombi:user_tools") + "?qn_success"
2017-06-12 08:10:52 +00:00
class UserTrombiResetClubMembershipsView(UserIsInATrombiMixin, RedirectView):
permanent = False
def get(self, request, *args, **kwargs):
user = self.request.user.trombi_user
user.make_memberships()
return redirect(self.get_success_url())
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse("trombi:profile") + "?qn_success"
2017-06-12 08:10:52 +00:00
class UserTrombiDeleteMembershipView(TrombiTabsMixin, CanEditMixin, DeleteView):
model = TrombiClubMembership
pk_url_kwarg = "membership_id"
template_name = "core/delete_confirm.jinja"
2018-10-04 19:29:19 +00:00
success_url = reverse_lazy("trombi:profile")
current_tab = "profile"
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return (
super(UserTrombiDeleteMembershipView, self).get_success_url()
+ "?qn_success"
)
2017-06-12 08:10:52 +00:00
# Used by admins when someone does not have every club in his list
class UserTrombiAddMembershipView(TrombiTabsMixin, CreateView):
model = TrombiClubMembership
template_name = "core/edit.jinja"
fields = ["club", "role", "start", "end"]
pk_url_kwarg = "user_id"
current_tab = "profile"
def dispatch(self, request, *arg, **kwargs):
self.trombi_user = get_object_or_404(TrombiUser, pk=kwargs["user_id"])
if not self.trombi_user.trombi.is_owned_by(request.user):
raise PermissionDenied()
return super(UserTrombiAddMembershipView, self).dispatch(
request, *arg, **kwargs
)
def form_valid(self, form):
membership = form.save(commit=False)
membership.user = self.trombi_user
membership.save()
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self):
return reverse(
"trombi:detail", kwargs={"trombi_id": self.trombi_user.trombi.id}
)
class UserTrombiEditMembershipView(CanEditMixin, TrombiTabsMixin, UpdateView):
model = TrombiClubMembership
pk_url_kwarg = "membership_id"
2018-10-04 19:29:19 +00:00
fields = ["role", "start", "end"]
template_name = "core/edit.jinja"
current_tab = "profile"
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return (
super(UserTrombiEditMembershipView, self).get_success_url() + "?qn_success"
)
class UserTrombiProfileView(TrombiTabsMixin, DetailView):
2017-05-12 08:25:26 +00:00
model = TrombiUser
pk_url_kwarg = "user_id"
template_name = "trombi/user_profile.jinja"
context_object_name = "trombi_user"
current_tab = "tools"
2017-05-12 08:25:26 +00:00
def get(self, request, *args, **kwargs):
self.object = self.get_object()
if request.user.is_anonymous:
raise PermissionDenied()
2018-10-04 19:29:19 +00:00
if (
self.object.trombi.id != request.user.trombi_user.trombi.id
or self.object.user.id == request.user.id
or not self.object.trombi.show_profiles
):
2017-05-12 08:25:26 +00:00
raise Http404()
return super(UserTrombiProfileView, self).get(request, *args, **kwargs)
2017-06-12 08:10:52 +00:00
2019-10-02 12:56:04 +00:00
class TrombiCommentFormView(UserIsLoggedMixin):
"""
2017-05-10 20:17:05 +00:00
Create/edit a trombi comment
"""
2018-10-04 19:29:19 +00:00
2017-05-10 20:17:05 +00:00
model = TrombiComment
2018-10-04 19:29:19 +00:00
fields = ["content"]
template_name = "trombi/comment.jinja"
def get_form_class(self):
2017-05-10 20:17:05 +00:00
self.trombi = self.request.user.trombi_user.trombi
2017-05-11 16:33:45 +00:00
if date.today() <= self.trombi.subscription_deadline:
2018-10-04 19:29:19 +00:00
raise Http404(
_(
"You can not yet write comment, you must wait for "
"the subscription deadline to be passed."
)
)
2017-05-11 16:33:45 +00:00
if self.trombi.comments_deadline < date.today():
2018-10-04 19:29:19 +00:00
raise Http404(
_(
"You can not write comment anymore, the deadline is "
"already passed."
)
)
return modelform_factory(
self.model,
fields=self.fields,
widgets={
"content": forms.widgets.Textarea(
attrs={"maxlength": self.trombi.max_chars}
)
},
help_texts={
"content": _("Maximum characters: %(max_length)s")
% {"max_length": self.trombi.max_chars}
},
)
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse("trombi:user_tools") + "?qn_success"
def get_context_data(self, **kwargs):
kwargs = super(TrombiCommentFormView, self).get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
if "user_id" in self.kwargs.keys():
kwargs["target"] = get_object_or_404(TrombiUser, id=self.kwargs["user_id"])
else:
2018-10-04 19:29:19 +00:00
kwargs["target"] = self.object.target
return kwargs
2017-06-12 08:10:52 +00:00
class TrombiCommentCreateView(TrombiCommentFormView, CreateView):
def form_valid(self, form):
2018-10-04 19:29:19 +00:00
target = get_object_or_404(TrombiUser, id=self.kwargs["user_id"])
author = self.request.user.trombi_user
form.instance.author = author
form.instance.target = target
# Check that this combination does not already have a comment
old = TrombiComment.objects.filter(author=author, target=target).first()
if old:
old.content = form.instance.content
old.save()
return HttpResponseRedirect(self.get_success_url())
2017-05-10 20:17:05 +00:00
return super(TrombiCommentCreateView, self).form_valid(form)
2017-06-12 08:10:52 +00:00
2017-05-10 20:17:05 +00:00
class TrombiCommentEditView(TrombiCommentFormView, CanViewMixin, UpdateView):
pk_url_kwarg = "comment_id"
def form_valid(self, form):
form.instance.is_moderated = False
return super(TrombiCommentEditView, self).form_valid(form)