Include raids in the previous/next month in the raid calendar.

This commit is contained in:
Casper V. Kristensen 2019-10-26 15:19:14 +02:00
parent b3ba6caa07
commit 37e18971db
Signed by: caspervk
GPG key ID: 289CA03790535054
2 changed files with 20 additions and 5 deletions

View file

@ -4,7 +4,7 @@ from . import views
urlpatterns = [
path("calendar/", views.RaidCalendar.as_view(), name="raid_calendar"),
path("calendar/<int:year>/<int:month>/", views.RaidCalendar.as_view(month_format="%m"), name="raid_calendar"),
path("calendar/<int:year>/<int:month>/", views.RaidCalendar.as_view(), name="raid_calendar"),
path("raids/create/", views.RaidCreateView.as_view(), name="raid_create"),
path("raids/<int:pk>/", views.RaidDetailView.as_view(), name="raid_detail"),
path("raids/<int:pk>/change/", views.RaidChangeView.as_view(), name="raid_change"),

View file

@ -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):