Source Code

/ src / blog / urls.py

from . import views
from django_distill import distill_path
from blog.models import Author, Post, Column
from django.db.models import Count


def get_post_detail_params():
    for post in Post.objects.all().only("slug", "date"):
        yield {
            "year": post.date.year,
            "month": f"{post.date.month:02d}",
            "day": f"{post.date.day:02d}",
            "slug": post.slug,
        }


def get_archive_day_params():
    """
    Yield dicts of {"year": int, "month": int, "day": int} for all days
    that have multiple posts on the same day.
    """
    qs = (
        Post.objects
        .values("date__year", "date__month", "date__day")
        .annotate(post_count=Count("id"))
        .filter(post_count__gt=1)
        .order_by("date__year", "date__month", "date__day")
    )

    for item in qs:
        yield {
            "year": item["date__year"],
            "month": item["date__month"],
            "day": item["date__day"],
        }


def get_archive_month_params():
    """
    Yield dicts of {"year": int, "month": int} for all months
    that have posts.
    """
    qs = Post.objects.values_list("date__year", "date__month").distinct()
    for year, month in qs:
        yield {
            "year": year,
            "month": month,
        }


def get_archive_year_params():
    """
    Yield dicts of {"year": int} for all years that have posts.
    """
    qs = Post.objects.values_list("date__year", flat=True).distinct()
    for year in qs:
        yield {
            "year": year
        }



urlpatterns = [
    distill_path('', views.blog_index, name='blog_index'),

    distill_path('friends/', views.friends_index, name='friends_index'),
    distill_path('community/', views.page_community, name='page_community'),
    distill_path('webrings/', views.page_webrings, name='page_webrings'),
    distill_path('encrypted/', views.posts_encrypted, name='posts_encrypted'),
    distill_path('recycle/', views.posts_recycle, name='posts_recycle'),

    distill_path(
        "<int:year>/<str:month>/<str:day>/<slug:slug>/",
        views.post_detail,
        name="post_detail",
        distill_func=get_post_detail_params,
        distill_file="{year}/{month}/{day}/{slug}/index.html",
    ),








    distill_path(
        "column/<str:column>/",
        views.column_index,
        name="column_index",
        distill_func=lambda: ({"column": c.slug} for c in Column.objects.only("slug")),
        distill_file="column/{column}/index.html",
    ),

    distill_path(
        "<int:year>/",
        views.archive_year,
        name="archive_year",
        distill_func=get_archive_year_params,
        distill_file="{year}/index.html",
    ),

    distill_path(
        "<int:year>/<int:month>/",
        views.archive_month,
        name="archive_month",
        distill_func=get_archive_month_params,
        distill_file="{year}/{month:02d}/index.html",
    ),

    distill_path(
        "<int:year>/<int:month>/<int:day>/",
        views.archive_month,
        name="archive_day",
        distill_func=get_archive_day_params,
        distill_file="{year}/{month:02d}/{day:02d}/index.html",
    ),


    distill_path(
        "author/<str:author_key>",
        views.author_detail,
        name="author_detail",
        distill_func=lambda: ({"author_key": a.slug} for a in Author.objects.only("slug")),
        distill_file="author/{author_key}/index.html",
    ),

    distill_path(
        "author/",
        views.author_list,
        name="author_list",
        distill_file="author/index.html"
    ),

    distill_path(
        "rss.xml",
        views.rss_feed,
        name="rss_feed",
        distill_file="rss.xml",
    ),

    distill_path(
        "feed_rss_updated.xml",
        views.rss_feed,
        name="rss_feed_old",
        distill_file="feed_rss_updated.xml",
    ),
]