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 proc = subprocess.Popen(self.path,
29 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
31 stdout, stderr = proc.communicate()
32 text = stdout.decode("utf-8")[:1000]
35 if self.first_line_title:
36 title, text = text.split('\n', 1)
37 if self.skip_first_lines:
38 text = text.split('\n', self.skip_first_lines)[-1]
39 text = text.strip('\n')
40 poem = self.poem_set.create(
48 class Poem(models.Model):
49 slug = models.SlugField(max_length=50, db_index=True)
50 title = models.CharField(max_length=255, null=True, blank=True)
51 text = models.TextField()
52 poet = models.ForeignKey(Poet)
53 created_at = models.DateTimeField(auto_now_add=True, editable=False, db_index=True)
54 seen_at = models.DateTimeField(auto_now_add=True, editable=False)
55 view_count = models.IntegerField(default=1)
57 for_contest = models.BooleanField(default=False)
58 in_contest = models.BooleanField(default=False)
60 def __unicode__(self):
61 oneliner = u"%s, %s" % (self.poet.name, self.pretty_title())
63 oneliner += u" [%s…]" % self.text[:20]
66 def get_absolute_url(self):
67 return reverse('poetry_poem', kwargs={'slug': self.slug})
69 def pretty_title(self):
70 return self.title or "***"
74 self.seen_at = datetime.now()