Skip to content
Snippets Groups Projects
Commit 66d2c716 authored by OndraRehounek's avatar OndraRehounek Committed by jan.bednarik
Browse files

main: handle empty first month in timeline

parent 063db107
Branches
No related tags found
2 merge requests!614Release,!613main: handle empty first month in timeline
Pipeline #9709 passed
...@@ -3,4 +3,4 @@ ...@@ -3,4 +3,4 @@
line_length = 88 line_length = 88
multi_line_output = 3 multi_line_output = 3
include_trailing_comma = true include_trailing_comma = true
known_third_party = PyPDF2,arrow,bleach,bs4,captcha,celery,django,environ,faker,fastjsonschema,icalevnt,markdown,modelcluster,pirates,pytest,pytz,requests,sentry_sdk,taggit,tweepy,wagtail,wagtailmetadata,weasyprint,yaml known_third_party = PyPDF2,arrow,bleach,bs4,captcha,celery,dateutil,django,environ,faker,fastjsonschema,icalevnt,markdown,modelcluster,pirates,pytest,pytz,requests,sentry_sdk,taggit,tweepy,wagtail,wagtailmetadata,weasyprint,yaml
from datetime import timedelta from datetime import timedelta
from functools import cached_property from functools import cached_property
from dateutil.relativedelta import relativedelta
from django.conf import settings from django.conf import settings
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db import models from django.db import models
...@@ -333,15 +334,21 @@ class MainArticlesPage( ...@@ -333,15 +334,21 @@ class MainArticlesPage(
class Meta: class Meta:
verbose_name = "Rozcestník článků" verbose_name = "Rozcestník článků"
def get_article_data_list(self, month_back): def get_article_data_list(self, months_back: int = 1):
first_day_of_last_month = None target_date_list = (
last_month = timezone.now().today() MainArticlePage.objects.filter(article_type=ARTICLE_TYPES.WORK_TIMELINE)
for x in range(month_back): .order_by("-date")
last_month = last_month.replace(day=1) - timedelta(days=1) .values_list("date", flat=True)
first_day_of_last_month = last_month.replace(day=1) )
if not target_date_list:
return [self.get_empty_month_data(timezone.now().date())]
target_date = target_date_list[0] - relativedelta(months=months_back)
first_day_of_target_month = target_date.replace(day=1)
sorted_article_qs = MainArticlePage.objects.filter( sorted_article_qs = MainArticlePage.objects.filter(
date__gt=first_day_of_last_month, article_type=ARTICLE_TYPES.WORK_TIMELINE date__gt=first_day_of_target_month, article_type=ARTICLE_TYPES.WORK_TIMELINE
).order_by("-date") ).order_by("-date")
article_data_list = [] article_data_list = []
...@@ -365,16 +372,21 @@ class MainArticlesPage( ...@@ -365,16 +372,21 @@ class MainArticlesPage(
def get_context(self, request, *args, **kwargs): def get_context(self, request, *args, **kwargs):
ctx = super().get_context(request, args, kwargs) ctx = super().get_context(request, args, kwargs)
article_timeline_list = self.get_article_data_list(1) article_timeline_list = self.get_article_data_list(1)
ctx["article_timeline_list"] = article_timeline_list ctx["article_timeline_list"] = article_timeline_list
ctx["show_next_timeline_articles"] = MainArticlePage.objects.filter( ctx["show_next_timeline_articles"] = MainArticlePage.objects.filter(
article_type=ARTICLE_TYPES.WORK_TIMELINE article_type=ARTICLE_TYPES.WORK_TIMELINE
).count() > len(article_timeline_list) ).count() > len(article_timeline_list)
article_list = MainArticlePage.objects.filter( article_list = MainArticlePage.objects.filter(
article_type=ARTICLE_TYPES.PRESS_RELEASE article_type=ARTICLE_TYPES.PRESS_RELEASE
).order_by("-date") ).order_by("-date")[
:11
] # dám LIMIT +1, abych věděl, jestli má cenu show_next
ctx["article_article_list"] = article_list[:10] ctx["article_article_list"] = article_list[:10]
ctx["show_next_article"] = len(article_list) > 4 ctx["show_next_article"] = len(article_list) > 10
return ctx return ctx
def get_timeline_articles_response(self, request): def get_timeline_articles_response(self, request):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment