Clean up URL handling for models a li'l

pull/1/head
Danielle McLean 5 years ago
parent 4ac0bcde43
commit 8814e60624
Signed by: 00dani
GPG Key ID: 5A5D2D1AFF12EEC5
  1. 11
      entries/kinds.py
  2. 11
      entries/models.py
  3. 2
      users/models.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(

@ -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):

@ -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):

Loading…
Cancel
Save