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
1 changed files with 12 additions and 7 deletions

View File

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