Newer
Older
from typing import Any, Dict
from django.db.models import QuerySet
from django.http import FileResponse, HttpRequest
from django.urls import reverse_lazy
from django.utils.translation import gettext as _
from django.views import View
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.list import ListView
from aleksis.core.mixins import AdvancedCreateView, AdvancedDeleteView, AdvancedEditView
from .forms import PosterGroupForm, PosterUploadForm
from .models import Poster, PosterGroup
class PosterGroupListView(ListView):
"""Show a list of all poster groups."""
template_name = "resint/group/list.html"
model = PosterGroup
class PosterGroupCreateView(AdvancedCreateView):
"""Create a new poster group."""
model = PosterGroup
success_url = reverse_lazy("poster_group_list")
template_name = "resint/group/create.html"
success_message = _("The poster group has been saved.")
form_class = PosterGroupForm
class PosterGroupEditView(AdvancedEditView):
"""Edit an existing poster group."""
model = PosterGroup
success_url = reverse_lazy("poster_group_list")
template_name = "resint/group/edit.html"
success_message = _("The poster group has been saved.")
form_class = PosterGroupForm
class PosterGroupDeleteView(AdvancedDeleteView):
"""Delete a poster group."""
model = PosterGroup
success_url = reverse_lazy("poster_group_list")
success_message = _("The poster group has been deleted.")
template_name = "core/pages/delete.html"
class PosterListView(ListView):
"""Show a list of all uploaded posters."""
template_name = "resint/poster/list.html"
model = Poster
def get_queryset(self) -> QuerySet:
return Poster.objects.all().order_by("-year", "-week")
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
context = super().get_context_data(**kwargs)
context["poster_groups"] = PosterGroup.objects.all().order_by("name")
return context
class PosterUploadView(AdvancedCreateView):
"""Upload a new poster."""
model = Poster
success_url = reverse_lazy("poster_index")
template_name = "resint/poster/upload.html"
success_message = _("The poster has been uploaded.")
form_class = PosterUploadForm
class PosterDeleteView(AdvancedDeleteView):
"""Delete an uploaded poster."""
model = Poster
success_url = reverse_lazy("poster_index")
success_message = _("The poster has been deleted.")
template_name = "core/pages/delete.html"
class PosterCurrentView(SingleObjectMixin, View):
"""Show the poster which is currently valid."""
model = PosterGroup
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> FileResponse:
group = self.get_object()
current_poster = group.current_poster
file = current_poster.pdf if current_poster else group.default_pdf
return FileResponse(file, content_type="application/pdf")