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