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:
parent
dc99e7a39b
commit
f39782346f
3 changed files with 38 additions and 21 deletions
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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<id>\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<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)
|
||||
|
|
Loading…
Reference in a new issue