3 from django.db import models
4 from django.urls import reverse
5 from django.conf import settings
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)
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 = str(stdout, "utf-8", errors="ignore")[: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(
47 def make_for_contest(self):
48 assert not self.poem_set.filter(for_contest=True).exists()
49 for i in range(settings.POETRY_POEMS_FOR_CONTEST):
53 return self.poem_set.filter(for_contest=True)
55 def contest_poems(self):
56 return self.poem_set.filter(in_contest=True)
59 class Poem(models.Model):
60 slug = models.SlugField(max_length=50, db_index=True)
61 title = models.CharField(max_length=255, null=True, blank=True)
62 text = models.TextField()
63 poet = models.ForeignKey(Poet, on_delete=models.CASCADE)
64 created_at = models.DateTimeField(auto_now_add=True, editable=False, db_index=True)
65 seen_at = models.DateTimeField(auto_now_add=True, editable=False)
66 view_count = models.IntegerField(default=1)
68 for_contest = models.BooleanField(default=False)
69 in_contest = models.BooleanField(default=False)
72 oneliner = "%s, %s" % (self.poet.name, self.pretty_title())
73 oneliner += " [%s…]" % (" ".join(self.text.split()[:5]))
76 def get_absolute_url(self):
77 return reverse('poetry_poem', kwargs={'slug': self.slug})
79 def pretty_title(self):
80 return self.title or "***"
84 self.seen_at = datetime.now()