Clean up URL handling for models a li'l

This commit is contained in:
Danielle McLean 2017-10-27 16:51:46 +11:00
parent 4ac0bcde43
commit 8814e60624
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
3 changed files with 12 additions and 12 deletions

View file

@ -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(

View file

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

View file

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