import datetime

from django.views.generic import ListView

from aircox.models import Diffusion, StaticPage
from .base import BaseView
from .mixins import AttachedToMixin, GetDateMixin

__all__ = ("DiffusionListView",)


class DiffusionListView(GetDateMixin, AttachedToMixin, BaseView, ListView):
    """View for timetables."""

    model = Diffusion
    has_filters = True
    redirect_date_url = "diffusion-list"
    attach_to_value = StaticPage.ATTACH_TO_DIFFUSIONS

    def get_date(self):
        date = super().get_date()
        return date if date is not None else datetime.date.today()

    def get_queryset(self):
        return super().get_queryset().date(self.date).order_by("start")

    def get_context_data(self, **kwargs):
        start = self.date - datetime.timedelta(days=self.date.weekday())
        dates = [start + datetime.timedelta(days=i) for i in range(0, 7)]
        return super().get_context_data(date=self.date, dates=dates, **kwargs)
