Use a much cleaner approach to register routes for each post kind

This commit is contained in:
Danielle McLean 2017-10-27 16:16:08 +11:00
parent aec22e813d
commit 9bd6bc3d1c
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5

View file

@ -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<id>\d+)'
slug = r'(?P<slug>.+)'
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<id>\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<slug>.+)$', 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))