Update banner.
[wolnelektury.git] / src / lesmianator / models.py
index 5db02c2..0595810 100644 (file)
@@ -2,10 +2,10 @@
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
-import cPickle
+import pickle
+from pickle import PickleError
 from datetime import datetime
 from random import randint
-from StringIO import StringIO
 
 from django.core.files.base import ContentFile
 from django.db import models
@@ -32,9 +32,9 @@ class Poem(models.Model):
 
     try:
         f = open(settings.LESMIANATOR_PICKLE)
-        global_dictionary = cPickle.load(f)
+        global_dictionary = pickle.load(f)
         f.close()
-    except:
+    except (IOError, AttributeError, PickleError):
         global_dictionary = {}
 
     def visit(self):
@@ -42,7 +42,7 @@ class Poem(models.Model):
         self.seen_at = datetime.utcnow().replace(tzinfo=utc)
         self.save()
 
-    def __unicode__(self):
+    def __str__(self):
         return "%s (%s...)" % (self.slug, self.text[:20])
 
     @staticmethod
@@ -108,8 +108,8 @@ class Continuations(models.Model):
     class Meta:
         unique_together = (('content_type', 'object_id'), )
 
-    def __unicode__(self):
-        return "Continuations for: %s" % unicode(self.content_object)
+    def __str__(self):
+        return "Continuations for: %s" % str(self.content_object)
 
     @staticmethod
     def join_conts(a, b):
@@ -123,9 +123,8 @@ class Continuations(models.Model):
     @classmethod
     def for_book(cls, book, length=3):
         # count from this book only
-        output = StringIO()
         wldoc = book.wldocument(parse_dublincore=False)
-        output = wldoc.as_text(('raw-text',)).get_string()
+        output = wldoc.as_text(('raw-text',)).get_bytes()
         del wldoc
 
         conts = {}
@@ -149,7 +148,7 @@ class Continuations(models.Model):
     @classmethod
     def get(cls, sth):
         object_type = ContentType.objects.get_for_model(sth)
-        should_keys = set([sth.id])
+        should_keys = {sth.id}
         if isinstance(sth, Tag):
             should_keys = set(b.pk for b in Book.tagged.with_any((sth,)).iterator())
         try:
@@ -157,7 +156,7 @@ class Continuations(models.Model):
             if not obj.pickle:
                 raise cls.DoesNotExist
             f = open(obj.pickle.path)
-            keys, conts = cPickle.load(f)
+            keys, conts = pickle.load(f)
             f.close()
             if set(keys) != should_keys:
                 raise cls.DoesNotExist
@@ -171,8 +170,6 @@ class Continuations(models.Model):
                 raise NotImplementedError('Lesmianator continuations: only Book and Tag supported')
 
             c, created = cls.objects.get_or_create(content_type=object_type, object_id=sth.id)
-            c.pickle.save(sth.slug+'.p', ContentFile(cPickle.dumps((should_keys, conts))))
+            c.pickle.save(sth.slug+'.p', ContentFile(pickle.dumps((should_keys, conts))))
             c.save()
             return conts
-
-