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)
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 = str(stdout, "utf-8", errors="ignore")[: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)
56 def contest_poems(self):
57 return self.poem_set.filter(in_contest=True)
60 class Poem(models.Model):
61 slug = models.SlugField(max_length=50, db_index=True)
62 title = models.CharField(max_length=255, null=True, blank=True)
63 text = models.TextField()
64 poet = models.ForeignKey(Poet)
65 created_at = models.DateTimeField(auto_now_add=True, editable=False, db_index=True)
66 seen_at = models.DateTimeField(auto_now_add=True, editable=False)
67 view_count = models.IntegerField(default=1)
69 for_contest = models.BooleanField(default=False)
70 in_contest = models.BooleanField(default=False)
73 oneliner = "%s, %s" % (self.poet.name, self.pretty_title())
74 oneliner += " [%s…]" % (" ".join(self.text.split()[:5]))
77 def get_absolute_url(self):
78 return reverse('poetry_poem', kwargs={'slug': self.slug})
80 def pretty_title(self):
81 return self.title or "***"
85 self.seen_at = datetime.now()