ef8bf9470420ed450828322b49416219daea03af
[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
57 class Poem(models.Model):
58     slug = models.SlugField(max_length=50, db_index=True)
59     title = models.CharField(max_length=255, null=True, blank=True)
60     text = models.TextField()
61     poet = models.ForeignKey(Poet)
62     created_at = models.DateTimeField(auto_now_add=True, editable=False, db_index=True)
63     seen_at = models.DateTimeField(auto_now_add=True, editable=False)
64     view_count = models.IntegerField(default=1)
65
66     for_contest = models.BooleanField(default=False)
67     in_contest = models.BooleanField(default=False)
68
69     def __unicode__(self):
70         oneliner = u"%s, %s" % (self.poet.name, self.pretty_title())
71         if not self.title:
72             oneliner += u" [%s…]" % self.text[:20]
73         return oneliner
74
75     def get_absolute_url(self):
76         return reverse('poetry_poem', kwargs={'slug': self.slug})
77
78     def pretty_title(self):
79         return self.title or "***"
80
81     def visit(self):
82         self.view_count += 1
83         self.seen_at = datetime.now()
84         self.save()