Simplify entry routing by using one route with an optional slug for permalinks, rather than a route with a slug and a route without

This commit is contained in:
Danielle McLean 2017-10-28 14:01:31 +11:00
parent 1f6a587329
commit 5987e54105
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
3 changed files with 7 additions and 15 deletions

View file

@ -13,10 +13,6 @@ class Entry:
def entry(self): def entry(self):
return self.plural + '_entry' return self.plural + '_entry'
@property
def entry_slug(self):
return self.entry + '_slug'
Note = Entry( Note = Entry(
id='note', id='note',

View file

@ -88,12 +88,10 @@ class Entry(ModelMeta, models.Model):
@property @property
def url(self): def url(self):
kind = kinds.from_id[self.kind] kind = kinds.from_id[self.kind]
route = kind.entry
args = [self.id] args = [self.id]
if kind.slug: if kind.slug:
route = kind.entry_slug
args.append(self.slug) args.append(self.slug)
return reverse('entries:' + route, args=args) return reverse('entries:' + kind.entry, args=args)
@property @property
def slug(self): def slug(self):

View file

@ -3,8 +3,8 @@ from . import kinds, views
from lemoncurry import breadcrumbs as crumbs from lemoncurry import breadcrumbs as crumbs
def to_url(*args): def to_pat(*args):
return '^{0}$'.format('/'.join(args)) return '^{0}$'.format(''.join(args))
def prefix(route): def prefix(route):
@ -15,14 +15,12 @@ app_name = 'entries'
urlpatterns = [] urlpatterns = []
for k in kinds.all: for k in kinds.all:
kind = k.plural kind = k.plural
id = r'(?P<id>\d+)' id = r'/(?P<id>\d+)'
slug = r'(?P<slug>.+)' slug = r'(?:/(?P<slug>.+))?'
urlpatterns += ( urlpatterns += (
url(to_url(kind), views.index, name=k.index, kwargs={'kind': k}), url(to_pat(kind), views.index, name=k.index, kwargs={'kind': k}),
url(to_url(kind, id), views.entry, name=k.entry), url(to_pat(kind, id, slug), views.entry, name=k.entry),
url(to_url(kind, id, slug), views.entry, name=k.entry_slug),
) )
crumbs.add(prefix(k.index), label=k.plural, parent='home: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), parent=prefix(k.index))
crumbs.add(prefix(k.entry_slug), parent=prefix(k.index))