Sith/com/views.py

746 lines
24 KiB
Python
Raw Normal View History

#
# Copyright 2016,2017
# - Skia <skia@libskia.so>
2017-08-19 14:13:53 +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-12-21 20:03:52 +00:00
import itertools
2024-06-24 11:07:36 +00:00
from datetime import timedelta
from smtplib import SMTPRecipientsRefused
2025-01-09 23:45:25 +00:00
from typing import Any
2024-06-24 11:07:36 +00:00
2016-12-23 19:11:27 +00:00
from django.conf import settings
2025-01-09 23:45:25 +00:00
from django.contrib.auth.mixins import (
AccessMixin,
PermissionRequiredMixin,
)
2024-06-24 11:07:36 +00:00
from django.core.exceptions import PermissionDenied, ValidationError
2025-01-09 23:45:25 +00:00
from django.db.models import Max
2017-03-12 17:13:04 +00:00
from django.forms.models import modelform_factory
2024-06-24 11:07:36 +00:00
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse, reverse_lazy
from django.utils import timezone
2024-10-02 22:21:16 +00:00
from django.utils.timezone import localdate
2024-06-24 11:07:36 +00:00
from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, ListView, View
from django.views.generic.edit import CreateView, DeleteView, UpdateView
2024-06-24 11:07:36 +00:00
from club.models import Club, Mailing
2025-01-09 23:45:25 +00:00
from com.calendar import IcsCalendar
from com.forms import NewsDateForm, NewsForm, PosterForm
2024-06-24 11:07:36 +00:00
from com.models import News, NewsDate, Poster, Screen, Sith, Weekmail, WeekmailArticle
2025-01-09 23:45:25 +00:00
from core.models import User
2018-10-04 19:29:19 +00:00
from core.views import (
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,
2018-10-04 19:29:19 +00:00
)
from core.views.widgets.markdown import MarkdownInput
# Sith object
sith = Sith.objects.first
2017-06-12 06:59:03 +00:00
class ComTabsMixin(TabedViewMixin):
def get_tabs_title(self):
return _("Communication administration")
def get_list_of_tabs(self):
2025-01-09 23:45:25 +00:00
return [
{"url": reverse("com:weekmail"), "slug": "weekmail", "name": _("Weekmail")},
2018-10-04 19:29:19 +00:00
{
"url": reverse("com:weekmail_destinations"),
"slug": "weekmail_destinations",
"name": _("Weekmail destinations"),
2025-01-09 23:45:25 +00:00
},
{
"url": reverse("com:info_edit"),
"slug": "info",
"name": _("Info message"),
},
2018-10-04 19:29:19 +00:00
{
"url": reverse("com:alert_edit"),
"slug": "alert",
"name": _("Alert message"),
2025-01-09 23:45:25 +00:00
},
2018-10-04 19:29:19 +00:00
{
"url": reverse("com:mailing_admin"),
"slug": "mailings",
"name": _("Mailing lists administration"),
2025-01-09 23:45:25 +00:00
},
2018-10-04 19:29:19 +00:00
{
"url": reverse("com:poster_list"),
"slug": "posters",
"name": _("Posters list"),
2025-01-09 23:45:25 +00:00
},
2018-10-04 19:29:19 +00:00
{
"url": reverse("com:screen_list"),
"slug": "screens",
"name": _("Screens list"),
2025-01-09 23:45:25 +00:00
},
]
2017-06-12 06:59:03 +00:00
2025-01-09 23:45:25 +00:00
class IsComAdminMixin(AccessMixin):
def dispatch(self, request, *args, **kwargs):
if not request.user.is_com_admin:
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
class ComEditView(ComTabsMixin, CanEditPropMixin, UpdateView):
model = Sith
2018-10-04 19:29:19 +00:00
template_name = "core/edit.jinja"
def get_object(self, queryset=None):
return Sith.objects.first()
2017-06-12 06:59:03 +00:00
class AlertMsgEditView(ComEditView):
form_class = modelform_factory(
Sith, fields=["alert_msg"], widgets={"alert_msg": MarkdownInput}
)
current_tab = "alert"
2018-10-04 19:29:19 +00:00
success_url = reverse_lazy("com:alert_edit")
2017-06-12 06:59:03 +00:00
class InfoMsgEditView(ComEditView):
form_class = modelform_factory(
Sith, fields=["info_msg"], widgets={"info_msg": MarkdownInput}
)
current_tab = "info"
2018-10-04 19:29:19 +00:00
success_url = reverse_lazy("com:info_edit")
2017-06-12 06:59:03 +00:00
2017-01-11 00:34:16 +00:00
class WeekmailDestinationEditView(ComEditView):
2018-10-04 19:29:19 +00:00
fields = ["weekmail_destinations"]
2017-01-11 00:34:16 +00:00
current_tab = "weekmail_destinations"
2018-10-04 19:29:19 +00:00
success_url = reverse_lazy("com:weekmail_destinations")
2017-01-11 00:34:16 +00:00
2025-01-09 23:45:25 +00:00
# News
class NewsCreateView(PermissionRequiredMixin, CreateView):
"""View to either create or update News."""
model = News
form_class = NewsForm
2018-10-04 19:29:19 +00:00
template_name = "com/news_edit.jinja"
2025-01-09 23:45:25 +00:00
permission_required = "com.add_news"
2025-01-09 23:45:25 +00:00
def get_date_form_kwargs(self) -> dict[str, Any]:
"""Get initial data for NewsDateForm"""
if self.request.method == "POST":
return {"data": self.request.POST}
return {}
2016-12-23 18:43:59 +00:00
2025-01-09 23:45:25 +00:00
def get_form_kwargs(self):
return super().get_form_kwargs() | {
"author": self.request.user,
"date_form": NewsDateForm(**self.get_date_form_kwargs()),
}
def get_initial(self):
init = super().get_initial()
# if the id of a club is provided, select it by default
if club_id := self.request.GET.get("club"):
init["club"] = Club.objects.filter(id=club_id).first()
return init
2016-12-23 17:40:12 +00:00
2017-06-12 06:59:03 +00:00
2025-01-09 23:45:25 +00:00
class NewsUpdateView(UpdateView):
model = News
form_class = NewsForm
2018-10-04 19:29:19 +00:00
template_name = "com/news_edit.jinja"
2025-01-09 23:45:25 +00:00
pk_url_kwarg = "news_id"
2025-01-09 23:45:25 +00:00
def dispatch(self, request, *args, **kwargs):
if (
not request.user.has_perm("com.edit_news")
and self.get_object().author != request.user
):
raise PermissionDenied
return super().dispatch(request, *args, **kwargs)
2016-12-23 18:43:59 +00:00
2016-12-23 17:40:12 +00:00
def form_valid(self, form):
self.object = form.save()
2025-01-09 23:45:25 +00:00
IcsCalendar.make_internal()
2024-06-27 12:46:43 +00:00
return super().form_valid(form)
2016-12-23 17:40:12 +00:00
2025-01-09 23:45:25 +00:00
def get_date_form_kwargs(self) -> dict[str, Any]:
"""Get initial data for NewsDateForm"""
response = {}
if self.request.method == "POST":
response["data"] = self.request.POST
dates = list(self.object.dates.order_by("id"))
if len(dates) == 0:
return {}
response["instance"] = dates[0]
occurrences = NewsDateForm.get_occurrences(len(dates))
if occurrences is not None:
response["initial"] = {"is_weekly": True, "occurrences": occurrences}
return response
def get_form_kwargs(self):
return super().get_form_kwargs() | {
"author": self.request.user,
"date_form": NewsDateForm(**self.get_date_form_kwargs()),
}
class NewsDeleteView(PermissionRequiredMixin, DeleteView):
model = News
2018-10-04 19:29:19 +00:00
pk_url_kwarg = "news_id"
template_name = "core/delete_confirm.jinja"
success_url = reverse_lazy("com:news_admin_list")
2025-01-09 23:45:25 +00:00
permission_required = "com.delete_news"
2017-09-17 17:33:53 +00:00
2025-01-09 23:45:25 +00:00
class NewsModerateView(PermissionRequiredMixin, DetailView):
2016-12-23 17:40:12 +00:00
model = News
2018-10-04 19:29:19 +00:00
pk_url_kwarg = "news_id"
2025-01-09 23:45:25 +00:00
permission_required = "com.moderate_news"
2016-12-23 17:40:12 +00:00
def get(self, request, *args, **kwargs):
self.object = self.get_object()
if "remove" in request.GET:
2017-05-13 16:31:23 +00:00
self.object.is_moderated = False
else:
self.object.is_moderated = True
2016-12-23 17:40:12 +00:00
self.object.moderator = request.user
self.object.save()
if "next" in self.request.GET:
2018-10-04 19:29:19 +00:00
return redirect(self.request.GET["next"])
return redirect("com:news_admin_list")
2016-12-23 17:40:12 +00:00
2017-06-12 06:59:03 +00:00
2025-01-09 23:45:25 +00:00
class NewsAdminListView(PermissionRequiredMixin, ListView):
model = News
2018-10-04 19:29:19 +00:00
template_name = "com/news_admin_list.jinja"
2025-01-09 23:45:25 +00:00
queryset = News.objects.select_related(
"club", "author", "moderator"
).prefetch_related("dates")
permission_required = ["com.moderate_news", "com.delete_news"]
2017-06-12 06:59:03 +00:00
2025-01-09 23:45:25 +00:00
class NewsListView(ListView):
model = News
2018-10-04 19:29:19 +00:00
template_name = "com/news_list.jinja"
queryset = News.objects.filter(is_moderated=True)
2025-01-09 23:45:25 +00:00
def get_queryset(self):
return super().get_queryset().viewable_by(self.request.user)
2016-12-23 17:40:12 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["NewsDate"] = NewsDate
kwargs["timedelta"] = timedelta
2024-12-21 20:03:52 +00:00
kwargs["birthdays"] = itertools.groupby(
2018-10-04 19:29:19 +00:00
User.objects.filter(
2024-10-02 22:21:16 +00:00
date_of_birth__month=localdate().month,
date_of_birth__day=localdate().day,
2018-10-04 19:29:19 +00:00
)
.filter(role__in=["STUDENT", "FORMER STUDENT"])
2024-12-21 20:03:52 +00:00
.order_by("-date_of_birth"),
key=lambda u: u.date_of_birth.year,
2018-10-04 19:29:19 +00:00
)
2016-12-23 17:40:12 +00:00
return kwargs
2017-06-12 06:59:03 +00:00
2016-12-23 17:40:12 +00:00
class NewsDetailView(CanViewMixin, DetailView):
model = News
2018-10-04 19:29:19 +00:00
template_name = "com/news_detail.jinja"
pk_url_kwarg = "news_id"
2025-01-09 23:45:25 +00:00
queryset = News.objects.select_related("club", "author", "moderator")
def get_context_data(self, **kwargs):
return super().get_context_data(**kwargs) | {"date": self.object.dates.first()}
2018-10-04 19:29:19 +00:00
2016-12-23 17:40:12 +00:00
2017-01-02 23:07:45 +00:00
# Weekmail
2017-06-12 06:59:03 +00:00
class WeekmailPreviewView(ComTabsMixin, QuickNotifMixin, CanEditPropMixin, DetailView):
2017-01-10 17:30:27 +00:00
model = Weekmail
2018-10-04 19:29:19 +00:00
template_name = "com/weekmail_preview.jinja"
success_url = reverse_lazy("com:weekmail")
2017-01-11 11:18:42 +00:00
current_tab = "weekmail"
def dispatch(self, request, *args, **kwargs):
self.bad_recipients = []
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
2017-01-11 11:18:42 +00:00
def post(self, request, *args, **kwargs):
self.object = self.get_object()
if request.POST["send"] == "validate":
try:
2017-01-11 11:18:42 +00:00
self.object.send()
2018-10-04 19:29:19 +00:00
return HttpResponseRedirect(
reverse("com:weekmail") + "?qn_weekmail_send_success"
)
except SMTPRecipientsRefused as e:
self.bad_recipients = e.recipients
elif request.POST["send"] == "clean":
try:
self.object.send() # This should fail
except SMTPRecipientsRefused as e:
users = User.objects.filter(email__in=e.recipients)
for u in users:
u.preferences.receive_weekmail = False
u.preferences.save()
self.quick_notif_list += ["qn_success"]
2024-06-27 12:46:43 +00:00
return super().get(request, *args, **kwargs)
2017-01-10 17:30:27 +00:00
def get_object(self, queryset=None):
2018-10-04 19:29:19 +00:00
return self.model.objects.filter(sent=False).order_by("-id").first()
2017-01-10 17:30:27 +00:00
def get_context_data(self, **kwargs):
2024-07-12 07:34:16 +00:00
"""Add rendered weekmail."""
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["weekmail_rendered"] = self.object.render_html()
kwargs["bad_recipients"] = self.bad_recipients
2017-01-10 17:30:27 +00:00
return kwargs
2017-06-12 06:59:03 +00:00
2017-01-15 22:09:30 +00:00
class WeekmailEditView(ComTabsMixin, QuickNotifMixin, CanEditPropMixin, UpdateView):
2017-01-02 23:07:45 +00:00
model = Weekmail
2018-10-04 19:29:19 +00:00
template_name = "com/weekmail.jinja"
form_class = modelform_factory(
Weekmail,
fields=["title", "intro", "joke", "protip", "conclusion"],
help_texts={"title": _("Delete and save to regenerate")},
2018-12-20 14:06:20 +00:00
widgets={
"intro": MarkdownInput,
"joke": MarkdownInput,
"protip": MarkdownInput,
"conclusion": MarkdownInput,
},
2018-10-04 19:29:19 +00:00
)
success_url = reverse_lazy("com:weekmail")
2017-01-11 00:34:16 +00:00
current_tab = "weekmail"
2017-01-02 23:07:45 +00:00
def get_object(self, queryset=None):
2018-10-04 19:29:19 +00:00
weekmail = self.model.objects.filter(sent=False).order_by("-id").first()
2017-01-03 15:50:53 +00:00
if not weekmail.title:
now = timezone.now()
2018-10-04 19:29:19 +00:00
weekmail.title = _("Weekmail of the ") + (
now + timedelta(days=6 - now.weekday())
).strftime("%d/%m/%Y")
2017-01-10 17:30:27 +00:00
weekmail.save()
2017-01-03 15:50:53 +00:00
return weekmail
2017-01-02 23:07:45 +00:00
2017-01-07 14:08:23 +00:00
def get(self, request, *args, **kwargs):
self.object = self.get_object()
if "up_article" in request.GET:
2018-10-04 19:29:19 +00:00
art = get_object_or_404(
WeekmailArticle, id=request.GET["up_article"], weekmail=self.object
)
prev_art = (
self.object.articles.order_by("rank").filter(rank__lt=art.rank).last()
)
2017-01-07 14:08:23 +00:00
if prev_art:
art.rank, prev_art.rank = prev_art.rank, art.rank
art.save()
prev_art.save()
2018-10-04 19:29:19 +00:00
self.quick_notif_list += ["qn_success"]
if "down_article" in request.GET:
2018-10-04 19:29:19 +00:00
art = get_object_or_404(
WeekmailArticle, id=request.GET["down_article"], weekmail=self.object
)
next_art = (
self.object.articles.order_by("rank").filter(rank__gt=art.rank).first()
)
2017-01-07 14:08:23 +00:00
if next_art:
art.rank, next_art.rank = next_art.rank, art.rank
art.save()
next_art.save()
2018-10-04 19:29:19 +00:00
self.quick_notif_list += ["qn_success"]
if "add_article" in request.GET:
2018-10-04 19:29:19 +00:00
art = get_object_or_404(
WeekmailArticle, id=request.GET["add_article"], weekmail=None
)
2017-01-07 14:08:23 +00:00
art.weekmail = self.object
2018-10-04 19:29:19 +00:00
art.rank = self.object.articles.aggregate(Max("rank"))["rank__max"] or 0
2017-01-07 14:08:23 +00:00
art.rank += 1
art.save()
2018-10-04 19:29:19 +00:00
self.quick_notif_list += ["qn_success"]
if "del_article" in request.GET:
2018-10-04 19:29:19 +00:00
art = get_object_or_404(
WeekmailArticle, id=request.GET["del_article"], weekmail=self.object
)
2017-01-07 14:08:23 +00:00
art.weekmail = None
art.rank = -1
art.save()
2018-10-04 19:29:19 +00:00
self.quick_notif_list += ["qn_success"]
2024-06-27 12:46:43 +00:00
return super().get(request, *args, **kwargs)
2017-01-07 14:08:23 +00:00
def get_context_data(self, **kwargs):
2024-07-12 07:34:16 +00:00
"""Add orphan articles."""
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["orphans"] = WeekmailArticle.objects.filter(weekmail=None)
2017-01-07 14:08:23 +00:00
return kwargs
2017-06-12 06:59:03 +00:00
2018-10-04 19:29:19 +00:00
class WeekmailArticleEditView(
ComTabsMixin, QuickNotifMixin, CanEditPropMixin, UpdateView
):
2024-07-12 07:34:16 +00:00
"""Edit an article."""
2018-10-04 19:29:19 +00:00
2017-01-07 14:08:23 +00:00
model = WeekmailArticle
2018-12-20 14:06:20 +00:00
form_class = modelform_factory(
WeekmailArticle,
fields=["title", "club", "content"],
widgets={"content": MarkdownInput},
)
2017-01-07 14:08:23 +00:00
pk_url_kwarg = "article_id"
2018-10-04 19:29:19 +00:00
template_name = "core/edit.jinja"
success_url = reverse_lazy("com:weekmail")
2017-01-07 14:08:23 +00:00
quick_notif_url_arg = "qn_weekmail_article_edit"
2017-01-11 00:34:16 +00:00
current_tab = "weekmail"
2017-01-07 14:08:23 +00:00
2017-06-12 06:59:03 +00:00
class WeekmailArticleCreateView(QuickNotifMixin, CreateView):
2024-07-12 07:34:16 +00:00
"""Post an article."""
2018-10-04 19:29:19 +00:00
2017-01-07 14:08:23 +00:00
model = WeekmailArticle
2018-12-20 14:06:20 +00:00
form_class = modelform_factory(
WeekmailArticle,
fields=["title", "club", "content"],
widgets={"content": MarkdownInput},
)
2018-10-04 19:29:19 +00:00
template_name = "core/create.jinja"
success_url = reverse_lazy("core:user_tools")
2017-01-07 14:08:23 +00:00
quick_notif_url_arg = "qn_weekmail_new_article"
def get_initial(self):
2024-06-27 12:46:43 +00:00
if "club" not in self.request.GET:
return {}
return {"club": Club.objects.filter(id=self.request.GET.get("club")).first()}
def post(self, request, *args, **kwargs):
form = self.get_form()
self.object = form.instance
2024-06-27 12:46:43 +00:00
form.is_valid() # Valid a first time to populate club field
m = form.instance.club.get_membership_for(request.user)
if m is None or m.role <= settings.SITH_MAXIMUM_FREE_ROLE:
2018-10-04 19:29:19 +00:00
form.add_error(
"club",
ValidationError(
_(
"You must be a board member of the selected club to post in the Weekmail."
)
),
)
if form.is_valid() and "preview" not in request.POST:
return self.form_valid(form)
else:
return self.form_invalid(form)
2017-01-07 14:08:23 +00:00
def form_valid(self, form):
form.instance.author = self.request.user
2024-06-27 12:46:43 +00:00
return super().form_valid(form)
2017-01-07 14:08:23 +00:00
2017-06-12 06:59:03 +00:00
2017-01-15 22:09:30 +00:00
class WeekmailArticleDeleteView(CanEditPropMixin, DeleteView):
2024-07-12 07:34:16 +00:00
"""Delete an article."""
2018-10-04 19:29:19 +00:00
2017-01-07 14:08:23 +00:00
model = WeekmailArticle
2018-10-04 19:29:19 +00:00
template_name = "core/delete_confirm.jinja"
success_url = reverse_lazy("com:weekmail")
pk_url_kwarg = "article_id"
2017-08-19 14:13:53 +00:00
class MailingListAdminView(ComTabsMixin, ListView):
template_name = "com/mailing_admin.jinja"
model = Mailing
current_tab = "mailings"
def dispatch(self, request, *args, **kwargs):
if not (request.user.is_com_admin or request.user.is_root):
2017-08-19 14:13:53 +00:00
raise PermissionDenied
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
2017-08-19 14:13:53 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["moderated"] = self.get_queryset().filter(is_moderated=True).all()
kwargs["unmoderated"] = self.get_queryset().filter(is_moderated=False).all()
kwargs["has_moderated"] = len(kwargs["moderated"]) > 0
kwargs["has_unmoderated"] = len(kwargs["unmoderated"]) > 0
2017-08-19 14:13:53 +00:00
return kwargs
2017-08-21 17:53:17 +00:00
class MailingModerateView(View):
def get(self, request, *args, **kwargs):
2018-10-04 19:29:19 +00:00
mailing = get_object_or_404(Mailing, pk=kwargs["mailing_id"])
2017-08-21 17:53:17 +00:00
if mailing.can_moderate(request.user):
mailing.is_moderated = True
mailing.moderator = request.user
mailing.save()
2018-10-04 19:29:19 +00:00
return redirect("com:mailing_admin")
2017-08-21 17:53:17 +00:00
raise PermissionDenied
2017-11-28 13:43:05 +00:00
class PosterListBaseView(ListView):
2024-07-12 07:34:16 +00:00
"""List communication posters."""
2018-10-04 19:29:19 +00:00
current_tab = "posters"
model = Poster
2018-10-04 19:29:19 +00:00
template_name = "com/poster_list.jinja"
2017-11-28 13:43:05 +00:00
def dispatch(self, request, *args, **kwargs):
2018-10-04 19:29:19 +00:00
club_id = kwargs.pop("club_id", None)
2017-12-05 14:53:36 +00:00
self.club = None
2017-12-05 14:24:46 +00:00
if club_id:
self.club = get_object_or_404(Club, pk=club_id)
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
2017-11-28 13:43:05 +00:00
def get_queryset(self):
2017-12-05 14:24:46 +00:00
if self.request.user.is_com_admin:
2018-10-04 19:29:19 +00:00
return Poster.objects.all().order_by("-date_begin")
2017-12-05 14:24:46 +00:00
else:
return Poster.objects.filter(club=self.club.id)
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2017-12-05 14:24:46 +00:00
if not self.request.user.is_com_admin:
2018-10-04 19:29:19 +00:00
kwargs["club"] = self.club
return kwargs
2017-11-28 13:43:05 +00:00
class PosterCreateBaseView(CreateView):
2024-07-12 07:34:16 +00:00
"""Create communication poster."""
2018-10-04 19:29:19 +00:00
current_tab = "posters"
2017-11-01 17:12:33 +00:00
form_class = PosterForm
2018-10-04 19:29:19 +00:00
template_name = "core/create.jinja"
2017-11-28 13:43:05 +00:00
def get_queryset(self):
return Poster.objects.all()
def dispatch(self, request, *args, **kwargs):
2018-10-04 19:29:19 +00:00
if "club_id" in kwargs:
self.club = get_object_or_404(Club, pk=kwargs["club_id"])
2024-06-27 12:46:43 +00:00
return super().dispatch(request, *args, **kwargs)
2017-12-05 14:24:46 +00:00
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
2018-10-04 19:29:19 +00:00
kwargs.update({"user": self.request.user})
2017-12-05 14:24:46 +00:00
return kwargs
2017-11-01 17:12:33 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2017-12-05 14:24:46 +00:00
if not self.request.user.is_com_admin:
2018-10-04 19:29:19 +00:00
kwargs["club"] = self.club
2017-11-01 17:12:33 +00:00
return kwargs
2017-11-28 13:43:05 +00:00
def form_valid(self, form):
2017-12-05 14:24:46 +00:00
if self.request.user.is_com_admin:
2017-11-28 13:43:05 +00:00
form.instance.is_moderated = True
2024-06-27 12:46:43 +00:00
return super().form_valid(form)
2017-11-28 13:43:05 +00:00
2017-11-28 13:43:05 +00:00
class PosterEditBaseView(UpdateView):
2024-07-12 07:34:16 +00:00
"""Edit communication poster."""
2018-10-04 19:29:19 +00:00
pk_url_kwarg = "poster_id"
current_tab = "posters"
2017-11-01 17:12:33 +00:00
form_class = PosterForm
2018-10-04 19:29:19 +00:00
template_name = "com/poster_edit.jinja"
2017-11-28 13:43:05 +00:00
2018-04-26 12:48:29 +00:00
def get_initial(self):
return {
2024-12-28 16:33:26 +00:00
"date_begin": self.object.date_begin.strftime("%Y-%m-%d %H:%M:%S")
if self.object.date_begin
else None,
"date_end": self.object.date_end.strftime("%Y-%m-%d %H:%M:%S")
if self.object.date_end
else None,
}
2018-04-26 12:48:29 +00:00
2017-11-28 13:43:05 +00:00
def dispatch(self, request, *args, **kwargs):
if kwargs.get("club_id"):
2017-11-28 13:43:05 +00:00
try:
2018-10-04 19:29:19 +00:00
self.club = Club.objects.get(pk=kwargs["club_id"])
2024-06-27 12:46:43 +00:00
except Club.DoesNotExist as e:
raise PermissionDenied from e
return super().dispatch(request, *args, **kwargs)
2017-11-28 13:43:05 +00:00
def get_queryset(self):
return Poster.objects.all()
2017-12-05 14:24:46 +00:00
def get_form_kwargs(self):
2024-06-27 12:46:43 +00:00
kwargs = super().get_form_kwargs()
2018-10-04 19:29:19 +00:00
kwargs.update({"user": self.request.user})
2017-12-05 14:24:46 +00:00
return kwargs
2017-11-28 13:43:05 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
if hasattr(self, "club"):
kwargs["club"] = self.club
2017-11-28 13:43:05 +00:00
return kwargs
def form_valid(self, form):
if self.request.user.is_com_admin:
2017-11-28 13:43:05 +00:00
form.instance.is_moderated = False
2024-06-27 12:46:43 +00:00
return super().form_valid(form)
2017-11-28 13:43:05 +00:00
2017-12-05 14:24:46 +00:00
class PosterDeleteBaseView(DeleteView):
2024-07-12 07:34:16 +00:00
"""Edit communication poster."""
2018-10-04 19:29:19 +00:00
2017-12-05 14:24:46 +00:00
pk_url_kwarg = "poster_id"
2017-11-28 13:43:05 +00:00
current_tab = "posters"
model = Poster
2018-10-04 19:29:19 +00:00
template_name = "core/delete_confirm.jinja"
2017-12-05 14:24:46 +00:00
def dispatch(self, request, *args, **kwargs):
if kwargs.get("club_id"):
2017-12-05 14:24:46 +00:00
try:
2018-10-04 19:29:19 +00:00
self.club = Club.objects.get(pk=kwargs["club_id"])
2024-06-27 12:46:43 +00:00
except Club.DoesNotExist as e:
raise PermissionDenied from e
return super().dispatch(request, *args, **kwargs)
2017-12-05 14:24:46 +00:00
class PosterListView(IsComAdminMixin, ComTabsMixin, PosterListBaseView):
2024-07-12 07:34:16 +00:00
"""List communication posters."""
2017-11-28 13:43:05 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["app"] = "com"
2017-11-28 13:43:05 +00:00
return kwargs
class PosterCreateView(IsComAdminMixin, ComTabsMixin, PosterCreateBaseView):
2024-07-12 07:34:16 +00:00
"""Create communication poster."""
2018-10-04 19:29:19 +00:00
success_url = reverse_lazy("com:poster_list")
2017-11-28 13:43:05 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["app"] = "com"
2017-11-28 13:43:05 +00:00
return kwargs
class PosterEditView(IsComAdminMixin, ComTabsMixin, PosterEditBaseView):
2024-07-12 07:34:16 +00:00
"""Edit communication poster."""
2018-10-04 19:29:19 +00:00
success_url = reverse_lazy("com:poster_list")
2017-11-01 17:12:33 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["app"] = "com"
2017-11-01 17:12:33 +00:00
return kwargs
2017-12-05 14:24:46 +00:00
class PosterDeleteView(IsComAdminMixin, ComTabsMixin, PosterDeleteBaseView):
2024-07-12 07:34:16 +00:00
"""Delete communication poster."""
2018-10-04 19:29:19 +00:00
success_url = reverse_lazy("com:poster_list")
class PosterModerateListView(IsComAdminMixin, ComTabsMixin, ListView):
2024-07-12 07:34:16 +00:00
"""Moderate list communication poster."""
2018-10-04 19:29:19 +00:00
current_tab = "posters"
model = Poster
2018-10-04 19:29:19 +00:00
template_name = "com/poster_moderate.jinja"
2017-11-28 13:43:05 +00:00
queryset = Poster.objects.filter(is_moderated=False).all()
2017-11-01 17:12:33 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["app"] = "com"
return kwargs
2017-11-01 17:12:33 +00:00
class PosterModerateView(IsComAdminMixin, ComTabsMixin, View):
2024-07-12 07:34:16 +00:00
"""Moderate communication poster."""
2018-10-04 19:29:19 +00:00
def get(self, request, *args, **kwargs):
2018-10-04 19:29:19 +00:00
obj = get_object_or_404(Poster, pk=kwargs["object_id"])
2017-10-23 07:30:26 +00:00
if obj.can_be_moderated_by(request.user):
obj.is_moderated = True
obj.moderator = request.user
obj.save()
2018-10-04 19:29:19 +00:00
return redirect("com:poster_moderate_list")
raise PermissionDenied
2017-11-01 17:12:33 +00:00
def get_context_data(self, **kwargs):
kwargs = super(PosterModerateListView, self).get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["app"] = "com"
2017-11-01 17:12:33 +00:00
return kwargs
class ScreenListView(IsComAdminMixin, ComTabsMixin, ListView):
2024-07-12 07:34:16 +00:00
"""List communication screens."""
2018-10-04 19:29:19 +00:00
current_tab = "screens"
model = Screen
2018-10-04 19:29:19 +00:00
template_name = "com/screen_list.jinja"
class ScreenSlideshowView(DetailView):
2024-07-12 07:34:16 +00:00
"""Slideshow of actives posters."""
2018-10-04 19:29:19 +00:00
pk_url_kwarg = "screen_id"
model = Screen
2018-10-04 19:29:19 +00:00
template_name = "com/screen_slideshow.jinja"
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
kwargs["posters"] = self.object.active_posters()
return kwargs
class ScreenCreateView(IsComAdminMixin, ComTabsMixin, CreateView):
2024-07-12 07:34:16 +00:00
"""Create communication screen."""
2018-10-04 19:29:19 +00:00
current_tab = "screens"
model = Screen
2018-10-04 19:29:19 +00:00
fields = ["name"]
template_name = "core/create.jinja"
success_url = reverse_lazy("com:screen_list")
class ScreenEditView(IsComAdminMixin, ComTabsMixin, UpdateView):
2024-07-12 07:34:16 +00:00
"""Edit communication screen."""
2018-10-04 19:29:19 +00:00
pk_url_kwarg = "screen_id"
current_tab = "screens"
model = Screen
2018-10-04 19:29:19 +00:00
fields = ["name"]
template_name = "com/screen_edit.jinja"
success_url = reverse_lazy("com:screen_list")
class ScreenDeleteView(IsComAdminMixin, ComTabsMixin, DeleteView):
2024-07-12 07:34:16 +00:00
"""Delete communication screen."""
2018-10-04 19:29:19 +00:00
pk_url_kwarg = "screen_id"
current_tab = "screens"
model = Screen
2018-10-04 19:29:19 +00:00
template_name = "core/delete_confirm.jinja"
success_url = reverse_lazy("com:screen_list")