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 untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
3 changed files with 12 additions and 12 deletions

View file

@ -1,14 +1,9 @@
class Entry: class Entry:
fields = () def __init__(self, id, plural, icon, slug=False):
def has(self, field):
return field in self.fields
def __init__(self, id, plural, icon, fields=()):
self.id = id self.id = id
self.plural = plural self.plural = plural
self.icon = icon self.icon = icon
self.fields = fields self.slug = slug
@property @property
def index(self): def index(self):
@ -34,7 +29,7 @@ Article = Entry(
id='article', id='article',
icon='fa fa-file-text', icon='fa fa-file-text',
plural='articles', plural='articles',
fields=('slug', 'name'), slug=True,
) )
Photo = Entry( Photo = Entry(

View file

@ -70,15 +70,18 @@ class Entry(ModelMeta, models.Model):
def __str__(self): def __str__(self):
return '{0} {1}: {2}'.format(self.kind, self.id, self.title) return '{0} {1}: {2}'.format(self.kind, self.id, self.title)
def get_absolute_url(self):
return self.url
@property @property
def url(self): def url(self):
kind = kinds.from_id[self.kind] kind = kinds.from_id[self.kind]
route = 'entries:{kind}_entry'.format(kind=kind.plural) route = kind.entry
args = [self.id] args = [self.id]
if kind.has('slug'): if kind.slug:
route += '_slug' route = kind.entry_slug
args.append(self.slug) args.append(self.slug)
return reverse(route, args=args) return reverse('entries:' + route, args=args)
@property @property
def slug(self): 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 ;) # This is gonna need to change if I ever decide to add multiple-user support ;)
url = '/' url = '/'
def get_absolute_url(self):
return self.url
@property @property
def avatar_url(self): def avatar_url(self):