Skip to content

Commit

Permalink
Merge pull request #57 from gitenberg-dev/model-author
Browse files Browse the repository at this point in the history
Add properties to the book model
  • Loading branch information
eshellman committed Nov 28, 2017
2 parents 80ea099 + eec2a7c commit 821a5ee
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion gitensite/apps/bookinfo/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging

import yaml as PyYAML
def default_ctor(loader, tag_suffix, node):
return tag_suffix + ' ' + node.value
PyYAML.add_multi_constructor('!lcc', default_ctor)
PyYAML.add_multi_constructor('!lcsh', default_ctor)

from gitenberg.metadata.pandata import Pandata

from django.db import models
Expand All @@ -9,6 +16,12 @@

gh_org = 'GITenberg'

def smart_truncate(content, length=100, suffix='...'):
if len(content) <= length:
return content
else:
return ' '.join(content[:length+1].split(' ')[0:-1]) + suffix

class Book(models.Model):
book_id = models.IntegerField(unique=True)
repo_name = models.CharField(max_length=255, null=True, blank=True)
Expand All @@ -20,6 +33,25 @@ class Book(models.Model):
def __unicode__(self):
return self.repo_name

@property
def author(self):
if self.yaml:
try:
obj = PyYAML.load(self.yaml)
return obj["creator"]["author"]["agent_name"]
except:
return ""
else:
return ""

@property
def title_short(self):
return smart_truncate(self.title, 65)

@property
def description_short(self):
return smart_truncate(self.description, 300)

@property
def repo_url(self):
return 'https://github.com/{}/{}'.format(gh_org,self.repo_name)
Expand All @@ -35,7 +67,7 @@ def downloads_url(self):
@property
def pg_url(self):
return 'https://www.gutenberg.org/ebooks/{}'.format(self.book_id)

_pandata=None
def metadata(self):
if not self._pandata:
Expand Down

0 comments on commit 821a5ee

Please sign in to comment.