corrections
[turniej.git] / apps / poetry / models.py
index 39f3e05..63bf8f2 100644 (file)
@@ -3,6 +3,7 @@ import os
 import subprocess
 from django.db import models
 from django.core.urlresolvers import reverse
+from django.conf import settings
 from poetry.utils import get_hash
 
 
@@ -25,9 +26,11 @@ class Poet(models.Model):
     def write(self):
         curdir = os.getcwd()
         os.chdir(os.path.dirname(self.path))
-        text = subprocess.check_output(self.path).decode("utf-8")
+        proc = subprocess.Popen(self.path,
+            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         os.chdir(curdir)
-        text = text[:1000]
+        stdout, stderr = proc.communicate()
+        text = unicode(stdout, "utf-8", errors="ignore")[:1000]
         slug = get_hash(text)
         title = ''
         if self.first_line_title:
@@ -42,6 +45,17 @@ class Poet(models.Model):
             )
         return poem
 
+    def make_for_contest(self):
+        assert not self.poem_set.filter(for_contest=True).exists()
+        for i in range(settings.POETRY_POEMS_FOR_CONTEST):
+            p = self.write()
+            p.for_contest = True
+            p.save()
+        return self.poem_set.filter(for_contest=True)
+
+    def contest_poems(self):
+        return self.poem_set.filter(in_contest=True)
+
 
 class Poem(models.Model):
     slug = models.SlugField(max_length=50, db_index=True)
@@ -57,8 +71,7 @@ class Poem(models.Model):
 
     def __unicode__(self):
         oneliner = u"%s, %s" % (self.poet.name, self.pretty_title())
-        if not self.title:
-            oneliner += u" [%s…]" % self.text[:20]
+        oneliner += u" [%s…]" % (u" ".join(self.text.split()[:5]))
         return oneliner
 
     def get_absolute_url(self):