forked from 00dani/lemoncurry
Use a much cleaner approach to register routes for each post kind
This commit is contained in:
parent
aec22e813d
commit
9bd6bc3d1c
1 changed files with 20 additions and 14 deletions
|
@ -1,22 +1,28 @@
|
||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
from . import kinds, views
|
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'
|
app_name = 'entries'
|
||||||
urlpatterns = []
|
urlpatterns = []
|
||||||
for k in kinds.all:
|
for k in kinds.all:
|
||||||
urlpatterns.append(
|
kind = k.plural
|
||||||
url(r'^{k}$'.format(k=k.plural), views.index, name=k.index, kwargs={'kind': k})
|
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)
|
crumbs.add(prefix(k.index), label=k.plural, parent='home:index')
|
||||||
urlpatterns.append(
|
crumbs.add(prefix(k.entry), parent=prefix(k.index))
|
||||||
url(pattern + '$', views.entry, name=k.entry)
|
crumbs.add(prefix(k.entry_slug), parent=prefix(k.index))
|
||||||
)
|
|
||||||
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)
|
|
||||||
|
|
Loading…
Reference in a new issue