Fix crash when no news is available

This commit is contained in:
Antoine Bartuccio 2025-02-25 18:07:06 +01:00
parent 10701ccdfa
commit a01ea13f5b
2 changed files with 26 additions and 18 deletions

View File

@ -44,7 +44,7 @@
<em>{% trans %}Nothing to come...{% endtrans %}</em> <em>{% trans %}Nothing to come...{% endtrans %}</em>
</div> </div>
{% else %} {% else %}
{% for day, dates_group in news_dates %} {% for day, dates_group in news_dates.items() %}
<div class="news_events_group"> <div class="news_events_group">
<div class="news_events_group_date"> <div class="news_events_group_date">
<div> <div>
@ -120,7 +120,7 @@
<template x-for="newsDate in newsList" :key="newsDate.id"> <template x-for="newsDate in newsList" :key="newsDate.id">
<article <article
class="news_event" class="news_event"
x-data="{ newsState: newsDate.news.is_published ? AlertState.PULISHED : AlertState.PENDING }" x-data="{ newsState: newsDate.news.is_published ? AlertState.PUBLISHED : AlertState.PENDING }"
> >
<template x-if="!newsDate.news.is_published"> <template x-if="!newsDate.news.is_published">
{{ news_moderation_alert("newsDate.news.id", user, "newsState") }} {{ news_moderation_alert("newsDate.news.id", user, "newsState") }}

View File

@ -253,7 +253,7 @@ class NewsListView(TemplateView):
key=lambda u: u.date_of_birth.year, key=lambda u: u.date_of_birth.year,
) )
def get_last_day(self) -> date: def get_last_day(self) -> date | None:
"""Get the last day when news will be displayed """Get the last day when news will be displayed
The returned day is the third one where something happen. The returned day is the third one where something happen.
@ -261,31 +261,39 @@ class NewsListView(TemplateView):
D on 20/03, E on 21/03 and F on 22/03 ; D on 20/03, E on 21/03 and F on 22/03 ;
then the result is 20/03. then the result is 20/03.
""" """
return list( try:
NewsDate.objects.filter(end_date__gt=now()) return list(
.order_by("start_date") NewsDate.objects.filter(end_date__gt=now())
.values_list("start_date__date", flat=True) .order_by("start_date")
.distinct()[:4] .values_list("start_date__date", flat=True)
)[-1] .distinct()[:4]
)[-1]
except IndexError:
return None
def get_news_dates(self, until: date): def get_news_dates(self, until: date) -> dict[date, list[date]]:
"""Return the event dates to display. """Return the event dates to display.
The selected events are the ones that happens between The selected events are the ones that happens between
right now and the given day (included). right now and the given day (included).
""" """
return itertools.groupby( return {
NewsDate.objects.viewable_by(self.request.user) date: list(dates)
.filter(end_date__gt=now(), start_date__date__lte=until) for date, dates in itertools.groupby(
.order_by("start_date") NewsDate.objects.viewable_by(self.request.user)
.select_related("news", "news__club"), .filter(end_date__gt=now(), start_date__date__lte=until)
key=lambda d: d.start_date.date(), .order_by("start_date")
) .select_related("news", "news__club"),
key=lambda d: d.start_date.date(),
)
}
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
last_day = self.get_last_day() last_day = self.get_last_day()
return super().get_context_data(**kwargs) | { return super().get_context_data(**kwargs) | {
"news_dates": self.get_news_dates(until=last_day), "news_dates": self.get_news_dates(until=last_day)
if last_day is not None
else {},
"birthdays": self.get_birthdays(), "birthdays": self.get_birthdays(),
"last_day": last_day, "last_day": last_day,
} }