Skip to content

Commit

Permalink
Merge pull request #1597 from laws-africa/article-path
Browse files Browse the repository at this point in the history
Add regex to article url
  • Loading branch information
actlikewill authored Nov 3, 2023
2 parents 913466b + a2051ed commit 89c36df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 13 additions & 0 deletions peachjam/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import string
import subprocess
import tempfile
from datetime import datetime
from functools import wraps

from django.utils.translation import get_language_from_request
Expand Down Expand Up @@ -57,3 +58,15 @@ def chunks(lst, n):
"""Yield successive n-sized chunks from list."""
for i in range(n):
yield lst[i::n]


class ISODateConverter:
regex = r"\d{4}-\d{2}-\d{2}"

def to_python(self, value):
# invalid values will raise ValueError which will raise 404
return datetime.strptime(value, "%Y-%m-%d").date()

def to_url(self, value):
# invalid values will raise ValueError which will raise NoReverseMatch
return datetime.strptime(value, "%Y-%m-%d").date().strftime("%Y-%m-%d")
8 changes: 6 additions & 2 deletions peachjam/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path, re_path
from django.urls import include, path, re_path, register_converter
from django.views.decorators.cache import cache_page

from peachjam.feeds import (
Expand All @@ -27,6 +27,7 @@
LegalInstrumentAtomSiteNewsFeed,
LegislationAtomSiteNewsFeed,
)
from peachjam.helpers import ISODateConverter
from peachjam.views import (
AboutPageView,
ArticleDetailView,
Expand Down Expand Up @@ -68,6 +69,9 @@
)
from peachjam.views.metabase_stats import MetabaseStatsView

register_converter(ISODateConverter, "isodate")


# cache duration for most cached pages
CACHE_DURATION = 60 * 60 * 24

Expand Down Expand Up @@ -207,7 +211,7 @@
name="article_topic_list",
),
path(
"articles/<str:date>/<str:author>/<slug:slug>",
"articles/<isodate:date>/<str:author>/<slug:slug>",
ArticleDetailView.as_view(),
name="article_detail",
),
Expand Down

0 comments on commit 89c36df

Please sign in to comment.