From 9bd6bc3d1cf41199757a9e7bb1feb6368b9373cd Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Fri, 27 Oct 2017 16:16:08 +1100 Subject: [PATCH] Use a much cleaner approach to register routes for each post kind --- entries/urls.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/entries/urls.py b/entries/urls.py index 034fee1..a9b04ba 100644 --- a/entries/urls.py +++ b/entries/urls.py @@ -1,22 +1,28 @@ from django.conf.urls import url from . import kinds, views -from lemoncurry import breadcrumbs +from lemoncurry import breadcrumbs as crumbs + + +def to_url(*args): + return '^{0}$'.format('/'.join(args)) + + +def prefix(route): + return app_name + ':' + route + app_name = 'entries' urlpatterns = [] for k in kinds.all: - urlpatterns.append( - url(r'^{k}$'.format(k=k.plural), views.index, name=k.index, kwargs={'kind': k}) + kind = k.plural + id = r'(?P\d+)' + slug = r'(?P.+)' + urlpatterns += ( + url(to_url(kind), views.index, name=k.index, kwargs={'kind': k}), + url(to_url(kind, id), views.entry, name=k.entry), + url(to_url(kind, id, slug), views.entry, name=k.entry_slug), ) - breadcrumbs.add(app_name + ':' + k.index, label=k.plural, parent='home:index') - pattern = r'^{k}/(?P\d+)'.format(k=k.plural) - urlpatterns.append( - url(pattern + '$', views.entry, name=k.entry) - ) - breadcrumbs.add(app_name + ':' + k.entry, parent=app_name + ':' + k.index) - if k.has('slug'): - urlpatterns.append( - url(pattern + r'/(?P.+)$', views.entry, name=k.entry_slug) - ) - breadcrumbs.add(app_name + ':' + k.entry_slug, parent=app_name + ':' + k.index) + crumbs.add(prefix(k.index), label=k.plural, parent='home:index') + crumbs.add(prefix(k.entry), parent=prefix(k.index)) + crumbs.add(prefix(k.entry_slug), parent=prefix(k.index))