From 6a81eda4ccd59a90181c34d583ac38ce477ae0c4 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 16 Jul 2024 16:28:02 -0700 Subject: [PATCH] Redesign month page, closes #469 --- blog/views.py | 61 +++++++++++++++++++++++------------ static/css/all.css | 2 +- templates/archive_month.html | 33 +++++++------------ templates/archive_series.html | 2 +- templates/archive_tag.html | 2 +- templates/projects/index.html | 2 +- templates/series_index.html | 2 +- 7 files changed, 56 insertions(+), 48 deletions(-) diff --git a/blog/views.py b/blog/views.py index d3883198..57bec212 100644 --- a/blog/views.py +++ b/blog/views.py @@ -249,36 +249,55 @@ def archive_year(request, year): ) + def archive_month(request, year, month): year = int(year) month = MONTHS_3_REV[month.lower()] - def by_date(objs): - lookup = {} - for obj in objs: - lookup.setdefault(obj.created.date(), []).append(obj) - return lookup + items = [] + from django.db import connection - entries = list( - Entry.objects.filter(created__year=year, created__month=month).order_by( - "created" + cursor = connection.cursor() + for model, content_type in ( + (Entry, "entry"), + (Quotation, "quotation"), + (Blogmark, "blogmark"), + ): + ids = model.objects.filter( + created__year=year, created__month=month + ).values_list("id", flat=True) + items.extend( + [ + {"type": content_type, "obj": obj} + for obj in list( + model.objects.prefetch_related("tags").in_bulk(ids).values() + ) + ] ) - ) - blogmarks = list(Blogmark.objects.filter(created__year=year, created__month=month)) - quotations = list( - Quotation.objects.filter(created__year=year, created__month=month) - ) - # photos = list(Photo.objects.filter( - # created__year=year, created__month=month - # )) - # Extract non-de-duped list of ALL tags, for tag cloud - tags = [] - for obj in entries + blogmarks + quotations: - tags.extend([t.tag for t in obj.tags.all()]) + if not items: + raise Http404 + items.sort(key=lambda x: x["obj"].created) + # Paginate it + paginator = Paginator(items, min(1000, int(request.GET.get("size") or "30"))) + page_number = request.GET.get("page") or "1" + if page_number == "last": + page_number = paginator.num_pages + try: + page = paginator.page(page_number) + except PageNotAnInteger: + raise Http404 + except EmptyPage: + raise Http404 + return render( request, "archive_month.html", - {"date": datetime.date(year, month, 1), "entries": entries, "tags": tags}, + { + "items": page.object_list, + "total": paginator.count, + "page": page, + "date": datetime.date(year, month, 1), + }, ) diff --git a/static/css/all.css b/static/css/all.css index 36f672b1..6492b7c6 100644 --- a/static/css/all.css +++ b/static/css/all.css @@ -923,7 +923,7 @@ div p.search-selections .filters { padding-right: 0.6em; } -.archive-tag-h2 { +.archive-h2 { font-size: 1.4em; } h3.blog-mixed-list-year { diff --git a/templates/archive_month.html b/templates/archive_month.html index 4b058d87..b5e5eff5 100644 --- a/templates/archive_month.html +++ b/templates/archive_month.html @@ -1,28 +1,17 @@ -{% extends "smallhead.html" %} +{% extends "smallhead.html" %}{% load humanize %} {% block title %}Archive for {{ date|date:"F Y" }}{% endblock %} {% block primary %} -
-

{{ date|date:"F Y"}}

- -{% if entries %} -

Blog entries

- -{% endif %} - -{% if tags %} -

Tags

-

-{% load tag_cloud %} -{% tag_cloud_for_tags tags %} -

-{% endif %} -
+ +

{{ date|date:"F Y"}}

+ +{% load blog_tags %} + +{% blog_mixed_list_with_dates items %} + +{% include "_pagination.html" %} + {% endblock %} {% block secondary %} @@ -31,4 +20,4 @@

Tags

{% load blog_calendar %} {% render_calendar_month_only date %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/archive_series.html b/templates/archive_series.html index 0c8339fb..03b9a408 100644 --- a/templates/archive_series.html +++ b/templates/archive_series.html @@ -3,7 +3,7 @@ {% block title %}Simon Willison: {{ series }}{% endblock %} {% block primary %} -

Series: {{ series }}

+

Series: {{ series }}

{{ series.summary }}

diff --git a/templates/archive_tag.html b/templates/archive_tag.html index 6c5f8858..e8b58589 100644 --- a/templates/archive_tag.html +++ b/templates/archive_tag.html @@ -3,7 +3,7 @@ {% block title %}Simon Willison on {{ tags|join:" and " }}{% endblock %} {% block primary %} -

{{ total|intcomma }} item{{ total|pluralize }} tagged “{{ tags|join:"” and “" }}”

+

{{ total|intcomma }} item{{ total|pluralize }} tagged “{{ tags|join:"” and “" }}”

{% if tag.description %}
{{ tag.description_rendered }}
diff --git a/templates/projects/index.html b/templates/projects/index.html index bd2f7fdb..01b4f6b0 100644 --- a/templates/projects/index.html +++ b/templates/projects/index.html @@ -2,7 +2,7 @@ {% block title %}Simon Willison’s Weblog: Projects{% endblock %} {% block primary %} -

Projects

+

Projects

{% for project in projects %}

{{ project }}

diff --git a/templates/series_index.html b/templates/series_index.html index 8b3d855d..c66624a6 100644 --- a/templates/series_index.html +++ b/templates/series_index.html @@ -2,7 +2,7 @@ {% block title %}Simon Willison’s Weblog: Series of posts{% endblock %} {% block primary %} -

Series of posts

+

Series of posts

{% for series in all_series %}

{{ series.title }}