From 5c4cc4e7b2aaccef0f28fd5924aa840ad9232c53 Mon Sep 17 00:00:00 2001 From: j Date: Wed, 16 Aug 2023 17:58:28 +0200 Subject: [PATCH] include week in archive view --- app/item/models.py | 8 ++++++-- app/templates/archive.html | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/item/models.py b/app/item/models.py index dee6142..8609dc8 100644 --- a/app/item/models.py +++ b/app/item/models.py @@ -7,6 +7,7 @@ import lxml.html from django.conf import settings from django.contrib.auth import get_user_model from django.db import models +from django.db.models.functions import ExtractWeek, ExtractYear from django.urls import reverse from django.utils.timesince import timesince @@ -64,7 +65,6 @@ class Item(models.Model): cal = now.date().isocalendar() monday = now.date() - timedelta(days=now.date().isocalendar().weekday - 1) monday = timezone.datetime(monday.year, monday.month, monday.day, tzinfo=now.tzinfo) - print(monday) first_post = qs.filter(published__gt=monday).first() if first_post and first_post.published < now: week = qs.filter(published__gt=monday) @@ -76,7 +76,11 @@ class Item(models.Model): else: last_monday = monday - timedelta(days=7) week = qs.filter(published__gt=last_monday) - archive = qs.exclude(id__in=week) + archive = (qs + .exclude(id__in=week) + .annotate(year=ExtractYear('published')) + .annotate(week=ExtractWeek('published')) + ) return week, archive def get_absolute_url(self): diff --git a/app/templates/archive.html b/app/templates/archive.html index 2207109..d1a1884 100644 --- a/app/templates/archive.html +++ b/app/templates/archive.html @@ -1,6 +1,9 @@ {% extends "base.html" %} {% block content %} {% for item in items %} + {% ifchanged item.week %} +

{{ item.year }} week {{ item.week }}

+ {% endifchanged %} {% include "listitem.html" with item=item %} {% endfor %} {% endblock %}