1 # -*- coding: utf-8 -*-
4 from django.db import models
5 from django.core.urlresolvers import reverse
6 from poetry.utils import get_hash
9 class Poet(models.Model):
10 name = models.CharField(max_length=50)
11 path = models.CharField(max_length=255)
12 slug = models.SlugField(max_length=50, unique=True, db_index=True)
13 author = models.CharField(max_length=128, blank=True)
14 url = models.CharField(max_length=255, blank=True)
15 description = models.TextField(blank=True)
16 first_line_title = models.BooleanField(default=False)
17 skip_first_lines = models.IntegerField(default=0)
19 def __unicode__(self):
22 def get_absolute_url(self):
23 return reverse('poetry_poet', kwargs={'slug': self.slug})
27 os.chdir(os.path.dirname(self.path))
28 text = subprocess.check_output(self.path).decode("utf-8")
33 if self.first_line_title:
34 title, text = text.split('\n', 1)
35 if self.skip_first_lines:
36 text = text.split('\n', self.skip_first_lines)[-1]
37 text = text.strip('\n')
38 poem = self.poem_set.create(
46 class Poem(models.Model):
47 slug = models.SlugField(max_length=50, db_index=True)
48 title = models.CharField(max_length=255, null=True, blank=True)
49 text = models.TextField()
50 poet = models.ForeignKey(Poet)
51 created_at = models.DateTimeField(auto_now_add=True, editable=False, db_index=True)
52 seen_at = models.DateTimeField(auto_now_add=True, editable=False)
53 view_count = models.IntegerField(default=1)
55 for_contest = models.BooleanField(default=False)
56 in_contest = models.BooleanField(default=False)
58 def __unicode__(self):
59 oneliner = u"%s, %s" % (self.poet.name, self.pretty_title())
61 oneliner += u" [%s…]" % self.text[:20]
64 def get_absolute_url(self):
65 return reverse('poetry_poem', kwargs={'slug': self.slug})
67 def pretty_title(self):
68 return self.title or "***"
72 self.seen_at = datetime.now()