__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3"
from django.contrib import messages
from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404, render, redirect
from django.views.generic.base import TemplateView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.list import ListView
from guardian.decorators import permission_required
from .models import NewsLetter, NewsItem, NewsLetterNewsItemsTable
from .forms import (
NewsLetterForm,
NewsItemForm,
NewsLetterNewsItemsTableForm,
NewsLetterNewsItemsOrderingFormSet,
)
from scipost.mixins import PermissionsMixin
[docs]class NewsManageView(PermissionsMixin, TemplateView):
"""
General management of News.
"""
permission_required = "scipost.can_manage_news"
template_name = "news/news_manage.html"
[docs] def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["newsletters"] = NewsLetter.objects.all()
context["newsitems"] = NewsItem.objects.all()
context["add_ni_to_nl_form"] = NewsLetterNewsItemsTableForm()
return context
[docs]class NewsLetterView(TemplateView):
"""
Newsletter, for public consumption online.
"""
template_name = "news/newsletter_detail.html"
[docs] def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["nl"] = get_object_or_404(
NewsLetter,
date__year=self.kwargs["year"],
date__month=self.kwargs["month"],
date__day=self.kwargs["day"],
)
return context
[docs]class NewsLetterCreateView(PermissionsMixin, CreateView):
"""
Create a NewsLetter.
"""
permission_required = "scipost.can_manage_news"
form_class = NewsLetterForm
template_name = "news/newsletter_create.html"
success_url = reverse_lazy("news:manage")
[docs]class NewsLetterUpdateView(PermissionsMixin, UpdateView):
"""
Update a NewsLetter.
"""
permission_required = "scipost.can_manage_news"
model = NewsLetter
form_class = NewsLetterForm
template_name = "news/newsletter_update.html"
success_url = reverse_lazy("news:news")
[docs]@permission_required("scipost.can_manage_news", raise_exception=True)
def newsletter_update_ordering(request, pk):
newsletter = get_object_or_404(NewsLetter, pk=pk)
ni_formset = NewsLetterNewsItemsOrderingFormSet(
request.POST or None,
queryset=newsletter.newsletternewsitemstable_set.order_by("order"),
)
if ni_formset.is_valid():
ni_formset.save()
messages.success(request, "Newsletter items ordering updated")
return redirect(newsletter.get_absolute_url())
context = {
"ni_formset": ni_formset,
}
return render(request, "news/newsletter_update_ordering.html", context)
[docs]class NewsLetterDeleteView(PermissionsMixin, DeleteView):
"""
Delete a NewsLetter.
"""
permission_required = "scipost.can_manage_news"
model = NewsLetter
success_url = reverse_lazy("news:news")
[docs]class NewsItemCreateView(PermissionsMixin, CreateView):
"""
Create a NewsItem.
"""
permission_required = "scipost.can_manage_news"
form_class = NewsItemForm
template_name = "news/newsitem_create.html"
success_url = reverse_lazy("news:news")
[docs]class NewsItemDetailView(DetailView):
model = NewsItem
[docs]class NewsItemUpdateView(PermissionsMixin, UpdateView):
"""
Update a NewsItem.
"""
permission_required = "scipost.can_manage_news"
model = NewsItem
form_class = NewsItemForm
template_name = "news/newsitem_update.html"
success_url = reverse_lazy("news:news")
[docs]class NewsItemDeleteView(PermissionsMixin, DeleteView):
"""
Delete a NewsItem.
"""
permission_required = "scipost.can_manage_news"
model = NewsItem
success_url = reverse_lazy("news:news")
[docs]class NewsLetterNewsItemsTableCreateView(PermissionsMixin, CreateView):
"""
Add a NewsItem to a NewsLetter.
"""
permission_required = "scipost.can_manage_news"
form_class = NewsLetterNewsItemsTableForm
success_url = reverse_lazy("news:manage")
[docs]class NewsListView(ListView):
model = NewsItem
paginate_by = 10
[docs] def get_queryset(self):
qs = super().get_queryset()
return qs.filter(published=True)