1 # -*- coding: utf-8 -*-
4 from django.db import models
5 from django.core.urlresolvers import reverse
6 from django.conf import settings
7 from poetry.utils import get_hash
10 class Poet(models.Model):
11 name = models.CharField(max_length=50)
12 path = models.CharField(max_length=255)
13 slug = models.SlugField(max_length=50, unique=True, db_index=True)
14 author = models.CharField(max_length=128, blank=True)
15 url = models.CharField(max_length=255, blank=True)
16 description = models.TextField(blank=True)
17 first_line_title = models.BooleanField(default=False)
18 skip_first_lines = models.IntegerField(default=0)
20 def __unicode__(self):
23 def get_absolute_url(self):
24 return reverse('poetry_poet', kwargs={'slug': self.slug})
28 os.chdir(os.path.dirname(self.path))
29 proc = subprocess.Popen(self.path,
30 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
32 stdout, stderr = proc.communicate()
33 text = stdout.decode("utf-8")[:1000]
36 if self.first_line_title:
37 title, text = text.split('\n', 1)
38 if self.skip_first_lines:
39 text = text.split('\n', self.skip_first_lines)[-1]
40 text = text.strip('\n')
41 poem = self.poem_set.create(
48 def make_for_contest(self):
49 assert not self.poem_set.filter(for_contest=True).exists()
50 for i in range(settings.POETRY_POEMS_FOR_CONTEST):
54 return self.poem_set.filter(for_contest=True)
57 class Poem(models.Model):
58 slug = models.SlugField(max_length=50, db_index=True)
59 title = models.CharField(max_length=255, null=True, blank=True)
60 text = models.TextField()
61 poet = models.ForeignKey(Poet)
62 created_at = models.DateTimeField(auto_now_add=True, editable=False, db_index=True)
63 seen_at = models.DateTimeField(auto_now_add=True, editable=False)
64 view_count = models.IntegerField(default=1)
66 for_contest = models.BooleanField(default=False)
67 in_contest = models.BooleanField(default=False)
69 def __unicode__(self):
70 oneliner = u"%s, %s" % (self.poet.name, self.pretty_title())
72 oneliner += u" [%s…]" % self.text[:20]
75 def get_absolute_url(self):
76 return reverse('poetry_poem', kwargs={'slug': self.slug})
78 def pretty_title(self):
79 return self.title or "***"
83 self.seen_at = datetime.now()