From 712ab223ba12affa78df5d2fb0e45d766b3d6b80 Mon Sep 17 00:00:00 2001 From: bkfox Date: Tue, 28 Nov 2023 02:16:40 +0100 Subject: [PATCH] add pdocasts --- .../0016_alter_staticpage_attach_to.py | 32 +++++++++++++++++++ aircox/models/page.py | 2 ++ aircox/templates/aircox/home.html | 20 ++++++++++++ aircox/templates/aircox/program_detail.html | 4 +-- aircox/views/base.py | 9 ++++++ aircox/views/episode.py | 8 +++++ aircox/views/home.py | 26 +++++++++------ aircox/views/page.py | 10 ------ aircox/views/program.py | 4 +-- 9 files changed, 91 insertions(+), 24 deletions(-) create mode 100644 aircox/migrations/0016_alter_staticpage_attach_to.py diff --git a/aircox/migrations/0016_alter_staticpage_attach_to.py b/aircox/migrations/0016_alter_staticpage_attach_to.py new file mode 100644 index 0000000..caaf307 --- /dev/null +++ b/aircox/migrations/0016_alter_staticpage_attach_to.py @@ -0,0 +1,32 @@ +# Generated by Django 4.2.1 on 2023-11-28 01:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("aircox", "0015_alter_schedule_timezone_alter_staticpage_attach_to"), + ] + + operations = [ + migrations.AlterField( + model_name="staticpage", + name="attach_to", + field=models.SmallIntegerField( + blank=True, + choices=[ + (0, "Home page"), + (1, "Diffusions page"), + (2, "Logs page"), + (3, "Programs list"), + (4, "Episodes list"), + (5, "Articles list"), + (6, "Publications list"), + (7, "Podcasts list"), + ], + help_text="display this page content to related element", + null=True, + verbose_name="attach to", + ), + ), + ] diff --git a/aircox/models/page.py b/aircox/models/page.py index ac12923..73a2151 100644 --- a/aircox/models/page.py +++ b/aircox/models/page.py @@ -246,6 +246,7 @@ class StaticPage(BasePage): ATTACH_TO_EPISODES = 0x04 ATTACH_TO_ARTICLES = 0x05 ATTACH_TO_PAGES = 0x06 + ATTACH_TO_PODCASTS = 0x07 ATTACH_TO_CHOICES = ( (ATTACH_TO_HOME, _("Home page")), @@ -255,6 +256,7 @@ class StaticPage(BasePage): (ATTACH_TO_EPISODES, _("Episodes list")), (ATTACH_TO_ARTICLES, _("Articles list")), (ATTACH_TO_PAGES, _("Publications list")), + (ATTACH_TO_PODCASTS, _("Podcasts list")), ) VIEWS = { ATTACH_TO_HOME: "home", diff --git a/aircox/templates/aircox/home.html b/aircox/templates/aircox/home.html index d195322..ab3d806 100644 --- a/aircox/templates/aircox/home.html +++ b/aircox/templates/aircox/home.html @@ -48,6 +48,26 @@ {% endif %} + +{% if last_podcasts %} +
+

{% translate "Last podcasts" %}

+ + + {% for object in last_podcasts %} + {% page_widget "card" object open=True %} + {% endfor %} + + + +
+{% endif %} + {% if last_publications %}

{% translate "Last publications" %}

diff --git a/aircox/templates/aircox/program_detail.html b/aircox/templates/aircox/program_detail.html index 1e9e69c..3ce07b3 100644 --- a/aircox/templates/aircox/program_detail.html +++ b/aircox/templates/aircox/program_detail.html @@ -11,7 +11,7 @@

{% translate "Last Episodes" %}

- {% for object in episodes|slice:":3" %} + {% for object in episodes %} {% page_widget "card" object %} {% endfor %} @@ -30,7 +30,7 @@

{% translate "Last Articles" %}

- {% for object in articles|slice:3 %} + {% for object in articles %} {% page_widget "card" object %} {% endfor %} diff --git a/aircox/views/base.py b/aircox/views/base.py index 58463f7..54c5e8d 100644 --- a/aircox/views/base.py +++ b/aircox/views/base.py @@ -8,6 +8,7 @@ __all__ = ("BaseView", "BaseAPIView") class BaseView(TemplateResponseMixin, ContextMixin): header_template_name = "aircox/widgets/header.html" + related_count = 7 @property def station(self): @@ -16,6 +17,14 @@ class BaseView(TemplateResponseMixin, ContextMixin): # def get_queryset(self): # return super().get_queryset().station(self.station) + def get_related_queryset(self): + """Return a queryset of related pages or None.""" + return None + + def get_related_url(self): + """Return an url to the list of related pages.""" + return None + def get_page(self): return None diff --git a/aircox/views/episode.py b/aircox/views/episode.py index 78a9d3e..b6ea07c 100644 --- a/aircox/views/episode.py +++ b/aircox/views/episode.py @@ -33,3 +33,11 @@ class EpisodeListView(PageListView): filterset_class = EpisodeFilters parent_model = Program attach_to_value = StaticPage.ATTACH_TO_EPISODES + + +class PodcastListView(PageListView): + model = Episode + filterset_class = EpisodeFilters + parent_model = Program + attach_to_value = StaticPage.ATTACH_TO_PODCASTS + queryset = Episode.objects.published().with_podcasts().order_by("-pub_date") diff --git a/aircox/views/home.py b/aircox/views/home.py index b47d3fc..213ea61 100644 --- a/aircox/views/home.py +++ b/aircox/views/home.py @@ -1,9 +1,9 @@ -from datetime import date, timedelta +from datetime import date, datetime, timedelta from django.utils import timezone as tz from django.views.generic import ListView -from ..models import Diffusion, Log, Page, StaticPage +from ..models import Diffusion, Episode, Log, Page, StaticPage from .base import BaseView from .mixins import AttachedToMixin @@ -13,22 +13,24 @@ class HomeView(AttachedToMixin, BaseView, ListView): attach_to_value = StaticPage.ATTACH_TO_HOME model = Diffusion queryset = Diffusion.objects.on_air().select_related("episode").order_by("-start") - diffusion_count = 7 - publications_count = 7 + log_diffusion_count = 3 + + publications_queryset = Page.objects.select_subclasses().published().order_by("-pub_date") + podcasts_queryset = Episode.objects.published().with_podcasts().order_by("-pub_date") def get_queryset(self): - return super().get_queryset().date(date.today()) + return super().get_queryset().before(datetime.now() - timedelta(hours=12)) def get_logs(self, diffusions): today = date.today() # diffs = Diffusion.objects.on_air().date(today) object_list = self.object_list - diffs = list(object_list[: self.diffusion_count]) + diffs = list(object_list[: self.related_count - 3]) logs = Log.objects.on_air().date(today).filter(track__isnull=False) if diffs: min_date = diffs[-1].start - timedelta(hours=1) logs = logs.after(min_date) - return Log.merge_diffusions(logs, object_list, diff_count=self.diffusion_count) + return Log.merge_diffusions(logs, object_list, diff_count=self.log_diffusion_count) def get_next_diffs(self): now = tz.now() @@ -38,12 +40,12 @@ class HomeView(AttachedToMixin, BaseView, ListView): if current_diff: diffs = [current_diff] + list(next_diffs.exclude(pk=current_diff.pk)[:9]) else: - diffs = next_diffs[: self.diffusion_count] + diffs = next_diffs[: self.related_count] return diffs def get_last_publications(self): # note: with postgres db, possible to use distinct() - qs = Page.objects.select_subclasses().published().order_by("-pub_date") + qs = self.publications_queryset.all() parents = set() items = [] for publication in qs: @@ -51,10 +53,13 @@ class HomeView(AttachedToMixin, BaseView, ListView): if parent_id is not None and parent_id in parents: continue items.append(publication) - if len(items) == self.publications_count: + if len(items) == self.related_count: break return items + def get_last_podcasts(self): + return self.podcasts_queryset.all()[: self.related_count] + def get_context_data(self, **kwargs): next_diffs = self.get_next_diffs() current_diff = next_diffs and next_diffs[0] @@ -66,6 +71,7 @@ class HomeView(AttachedToMixin, BaseView, ListView): "logs": self.get_logs(self.object_list), "next_diffs": next_diffs, "last_publications": self.get_last_publications(), + "last_podcasts": self.get_last_podcasts(), } ) return super().get_context_data(**kwargs) diff --git a/aircox/views/page.py b/aircox/views/page.py index f36d8dd..b3e6e55 100644 --- a/aircox/views/page.py +++ b/aircox/views/page.py @@ -133,16 +133,6 @@ class PageDetailView(BasePageDetailView): template_name = None context_object_name = "page" - related_count = 7 - - def get_related_queryset(self): - """Return a queryset of related pages or None.""" - return None - - def get_related_url(self): - """Return an url to the list of related pages.""" - return None - def get_template_names(self): return super().get_template_names() + ["aircox/page_detail.html"] diff --git a/aircox/views/program.py b/aircox/views/program.py index 5b0b6d1..1e1d23c 100644 --- a/aircox/views/program.py +++ b/aircox/views/program.py @@ -39,8 +39,8 @@ class ProgramDetailView(BaseProgramMixin, PageDetailView): return reverse("program-list") + f"?category__id={self.object.category_id}" def get_context_data(self, **kwargs): - episodes = Episode.objects.program(self.object).published().order_by("-pub_date") - articles = Article.objects.parent(self.object).published().order_by("-pub_date") + episodes = Episode.objects.program(self.object).published().order_by("-pub_date")[: self.related_count] + articles = Article.objects.parent(self.object).published().order_by("-pub_date")[: self.related_count] return super().get_context_data(articles=articles, episodes=episodes, **kwargs)