58b8caf268c9703ef6485dcab93a600453065a1c
[turniej.git] / apps / poetry / models.py
1 import os
2 import subprocess
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
7
8
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)
18
19     def __str__(self):
20         return self.name
21
22     def get_absolute_url(self):
23         return reverse('poetry_poet', kwargs={'slug': self.slug})
24
25     def write(self):
26         curdir = os.getcwd()
27         os.chdir(os.path.dirname(self.path))
28         proc = subprocess.Popen(self.path,
29             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
30         os.chdir(curdir)
31         stdout, stderr = proc.communicate()
32         text = str(stdout, "utf-8", errors="ignore")[:1000]
33         slug = get_hash(text)
34         title = ''
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(
41             slug=slug,
42             text=text,
43             title=title
44             )
45         return poem
46
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):
50             p = self.write()
51             p.for_contest = True
52             p.save()
53         return self.poem_set.filter(for_contest=True)
54
55     def contest_poems(self):
56         return self.poem_set.filter(in_contest=True)
57
58
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)
67
68     for_contest = models.BooleanField(default=False)
69     in_contest = models.BooleanField(default=False)
70
71     def __str__(self):
72         oneliner = "%s, %s" % (self.poet.name, self.pretty_title())
73         oneliner += " [%s…]" % (" ".join(self.text.split()[:5]))
74         return oneliner
75
76     def get_absolute_url(self):
77         return reverse('poetry_poem', kwargs={'slug': self.slug})
78
79     def pretty_title(self):
80         return self.title or "***"
81
82     def visit(self):
83         self.view_count += 1
84         self.seen_at = datetime.now()
85         self.save()