From f39782346f84a6621c1a0453577d688fe0757208 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Thu, 26 Oct 2017 09:01:36 +1100 Subject: [PATCH] Make entry kinds a teeny bit easier to manage by moving some of the smarts into the actual kinds.Entry class --- entries/kinds.py | 43 +++++++++++++++++++++++++++++++------------ entries/models.py | 2 +- entries/urls.py | 14 ++++++-------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/entries/kinds.py b/entries/kinds.py index 62f6ba1..a5d0407 100644 --- a/entries/kinds.py +++ b/entries/kinds.py @@ -1,22 +1,41 @@ class Entry: fields = () - @classmethod - def has(cls, field): - return field in cls.fields + def has(self, field): + return field in self.fields + + def __init__(self, id, plural, icon, fields=()): + self.id = id + self.plural = plural + self.icon = icon + self.fields = fields + + @property + def index(self): + return self.plural + '_index' + + @property + def entry(self): + return self.plural + '_entry' + + @property + def entry_slug(self): + return self.entry + '_slug' -class Note(Entry): - id = 'note' - icon = 'fa fa-paper-plane' - plural = 'notes' +Note = Entry( + id='note', + icon='fa fa-paper-plane', + plural='notes', +) -class Article(Entry): - id = 'article' - icon = 'fa fa-file-text' - plural = 'articles' - fields = ('slug', 'name') +Article = Entry( + id='article', + icon='fa fa-file-text', + plural='articles', + fields=('slug', 'name'), +) all = (Note, Article) diff --git a/entries/models.py b/entries/models.py index db8064e..393af4a 100644 --- a/entries/models.py +++ b/entries/models.py @@ -7,7 +7,7 @@ from meta.models import ModelMeta from users.models import Profile from . import kinds -ENTRY_KINDS = [(k.id, k.__name__) for k in kinds.all] +ENTRY_KINDS = [(k.id, k.id) for k in kinds.all] class Entry(ModelMeta, models.Model): diff --git a/entries/urls.py b/entries/urls.py index 6cdc45c..034fee1 100644 --- a/entries/urls.py +++ b/entries/urls.py @@ -5,20 +5,18 @@ from lemoncurry import breadcrumbs app_name = 'entries' urlpatterns = [] for k in kinds.all: - index = k.plural + '_index' urlpatterns.append( - url(r'^{k}$'.format(k=k.plural), views.index, name=index, kwargs={'kind': k}) + url(r'^{k}$'.format(k=k.plural), views.index, name=k.index, kwargs={'kind': k}) ) - breadcrumbs.add(app_name + ':' + index, label=k.plural, parent='home:index') + breadcrumbs.add(app_name + ':' + k.index, label=k.plural, parent='home:index') - entry = k.plural + '_entry' pattern = r'^{k}/(?P\d+)'.format(k=k.plural) urlpatterns.append( - url(pattern + '$', views.entry, name=entry) + url(pattern + '$', views.entry, name=k.entry) ) - breadcrumbs.add(app_name + ':' + entry, parent=app_name + ':' + index) + breadcrumbs.add(app_name + ':' + k.entry, parent=app_name + ':' + k.index) if k.has('slug'): urlpatterns.append( - url(pattern + r'/(?P.+)$', views.entry, name=entry + '_slug') + url(pattern + r'/(?P.+)$', views.entry, name=k.entry_slug) ) - breadcrumbs.add(app_name + ':' + entry + '_slug', parent=app_name + ':' + index) + breadcrumbs.add(app_name + ':' + k.entry_slug, parent=app_name + ':' + k.index)