Figured out how to do both title and description without getting duplication :3

This commit is contained in:
Danielle McLean 2017-10-27 19:41:25 +11:00
parent 8853e42508
commit 08b44ccee6
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5

View file

@ -1,6 +1,7 @@
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from itertools import groupby
from slugify import slugify from slugify import slugify
from textwrap import shorten from textwrap import shorten
@ -40,7 +41,7 @@ class Entry(ModelMeta, models.Model):
updated = models.DateTimeField() updated = models.DateTimeField()
_metadata = { _metadata = {
'description': 'excerpt_if_unused', 'description': 'excerpt',
'image': 'image_url', 'image': 'image_url',
'twitter_creator': 'twitter_creator', 'twitter_creator': 'twitter_creator',
'og_profile_id': 'og_profile_id', 'og_profile_id': 'og_profile_id',
@ -48,17 +49,21 @@ class Entry(ModelMeta, models.Model):
@property @property
def title(self): def title(self):
return self.name if self.name else self.excerpt if self.name:
return self.name
return shorten(self.paragraphs[0], width=50, placeholder='')
@property @property
def excerpt(self): def excerpt(self):
first_line = self.content.split('\n')[0] try:
return shorten(first_line, width=100, placeholder='') return self.paragraphs[0 if self.name else 1]
except IndexError:
return ' '
@property @property
def excerpt_if_unused(self): def paragraphs(self):
if self.name: lines = self.content.splitlines()
return self.excerpt return ["\n".join(para) for k, para in groupby(lines, key=bool)]
@property @property
def twitter_creator(self): def twitter_creator(self):