From 37e18971db05ff39478a884e066465d79410cdcd Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sat, 26 Oct 2019 15:19:14 +0200 Subject: [PATCH] Include raids in the previous/next month in the raid calendar. --- drakul/raids/urls.py | 2 +- drakul/raids/views.py | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/drakul/raids/urls.py b/drakul/raids/urls.py index 6cf83d9..6766da3 100644 --- a/drakul/raids/urls.py +++ b/drakul/raids/urls.py @@ -4,7 +4,7 @@ from . import views urlpatterns = [ path("calendar/", views.RaidCalendar.as_view(), name="raid_calendar"), - path("calendar///", views.RaidCalendar.as_view(month_format="%m"), name="raid_calendar"), + path("calendar///", views.RaidCalendar.as_view(), name="raid_calendar"), path("raids/create/", views.RaidCreateView.as_view(), name="raid_create"), path("raids//", views.RaidDetailView.as_view(), name="raid_detail"), path("raids//change/", views.RaidChangeView.as_view(), name="raid_change"), diff --git a/drakul/raids/views.py b/drakul/raids/views.py index 15bb9ad..f2d098d 100644 --- a/drakul/raids/views.py +++ b/drakul/raids/views.py @@ -1,5 +1,6 @@ import itertools from calendar import Calendar +from datetime import timedelta from django.contrib.auth import get_user_model from django.contrib.auth.mixins import PermissionRequiredMixin @@ -22,6 +23,7 @@ class RaidCalendar(MonthArchiveView): allow_future = True date_field = "date" ordering = "date" + month_format = "%m" template_name_suffix = "_calendar" def get_queryset(self): @@ -31,6 +33,15 @@ class RaidCalendar(MonthArchiveView): max_status=Max("responses__status", filter=Q(responses__character__user=self.request.user)) ) + def get_dated_queryset(self, **lookup): + """ + Include raids one week into the previous/next month so we can show them "grayed out" in the calendar. + """ + date_field = self.get_date_field() + lookup[f"{date_field}__gte"] -= timedelta(weeks=1) + lookup[f"{date_field}__lt"] += timedelta(weeks=1) + return super().get_dated_queryset(**lookup) + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) date = context["month"] @@ -53,19 +64,23 @@ class RaidCalendar(MonthArchiveView): return context def get_year(self): + """ + Default to the current year if not provided in the url kwargs. + """ try: return super().get_year() except Http404: return timezone.now().strftime(self.get_year_format()) def get_month(self): + """ + Default to the current month if not provided in the url kwargs. + TODO: Could get the month of the next raid instead. + """ try: return super().get_month() except Http404: - return timezone.now().strftime(self.get_month_format()) # TODO: Could get date of next raid instead - - def get_ordering(self): - return super().get_ordering() + return timezone.now().strftime(self.get_month_format()) class RaidDetailView(SingleObjectMixin, MultiModelFormView):