Make entry kinds a teeny bit easier to manage by moving some of the smarts into the actual kinds.Entry class

This commit is contained in:
Danielle McLean 2017-10-26 09:01:36 +11:00
parent dc99e7a39b
commit f39782346f
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
3 changed files with 38 additions and 21 deletions

View file

@ -1,22 +1,41 @@
class Entry: class Entry:
fields = () fields = ()
@classmethod def has(self, field):
def has(cls, field): return field in self.fields
return field in cls.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): Note = Entry(
id = 'note' id='note',
icon = 'fa fa-paper-plane' icon='fa fa-paper-plane',
plural = 'notes' plural='notes',
)
class Article(Entry): Article = Entry(
id = 'article' id='article',
icon = 'fa fa-file-text' icon='fa fa-file-text',
plural = 'articles' plural='articles',
fields = ('slug', 'name') fields=('slug', 'name'),
)
all = (Note, Article) all = (Note, Article)

View file

@ -7,7 +7,7 @@ from meta.models import ModelMeta
from users.models import Profile from users.models import Profile
from . import kinds 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): class Entry(ModelMeta, models.Model):

View file

@ -5,20 +5,18 @@ from lemoncurry import breadcrumbs
app_name = 'entries' app_name = 'entries'
urlpatterns = [] urlpatterns = []
for k in kinds.all: for k in kinds.all:
index = k.plural + '_index'
urlpatterns.append( 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<id>\d+)'.format(k=k.plural) pattern = r'^{k}/(?P<id>\d+)'.format(k=k.plural)
urlpatterns.append( 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'): if k.has('slug'):
urlpatterns.append( urlpatterns.append(
url(pattern + r'/(?P<slug>.+)$', views.entry, name=entry + '_slug') url(pattern + r'/(?P<slug>.+)$', 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)