From 8814e60624e63d66b3b312815bbdc94b9d360c4e Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Fri, 27 Oct 2017 16:51:46 +1100 Subject: [PATCH] Clean up URL handling for models a li'l --- entries/kinds.py | 11 +++-------- entries/models.py | 11 +++++++---- users/models.py | 2 ++ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/entries/kinds.py b/entries/kinds.py index 454560e..c4e37a7 100644 --- a/entries/kinds.py +++ b/entries/kinds.py @@ -1,14 +1,9 @@ class Entry: - fields = () - - def has(self, field): - return field in self.fields - - def __init__(self, id, plural, icon, fields=()): + def __init__(self, id, plural, icon, slug=False): self.id = id self.plural = plural self.icon = icon - self.fields = fields + self.slug = slug @property def index(self): @@ -34,7 +29,7 @@ Article = Entry( id='article', icon='fa fa-file-text', plural='articles', - fields=('slug', 'name'), + slug=True, ) Photo = Entry( diff --git a/entries/models.py b/entries/models.py index 8a9fa27..dcdae7f 100644 --- a/entries/models.py +++ b/entries/models.py @@ -70,15 +70,18 @@ class Entry(ModelMeta, models.Model): def __str__(self): return '{0} {1}: {2}'.format(self.kind, self.id, self.title) + def get_absolute_url(self): + return self.url + @property def url(self): kind = kinds.from_id[self.kind] - route = 'entries:{kind}_entry'.format(kind=kind.plural) + route = kind.entry args = [self.id] - if kind.has('slug'): - route += '_slug' + if kind.slug: + route = kind.entry_slug args.append(self.slug) - return reverse(route, args=args) + return reverse('entries:' + route, args=args) @property def slug(self): diff --git a/users/models.py b/users/models.py index ba7196d..94b1762 100644 --- a/users/models.py +++ b/users/models.py @@ -26,6 +26,8 @@ class User(ModelMeta, AbstractUser): # This is gonna need to change if I ever decide to add multiple-user support ;) url = '/' + def get_absolute_url(self): + return self.url @property def avatar_url(self):