remove the banner
[wolnelektury.git] / src / lesmianator / models.py
index 092a828..fbcee04 100644 (file)
@@ -1,39 +1,36 @@
-# -*- coding: utf-8 -*-
-# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
-# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 #
 #
-import cPickle
-from cPickle import PickleError
+from functools import reduce
+import pickle
+from pickle import PickleError
 from datetime import datetime
 from random import randint
 from datetime import datetime
 from random import randint
-from StringIO import StringIO
 
 from django.core.files.base import ContentFile
 from django.db import models
 from django.utils.timezone import utc
 
 from django.core.files.base import ContentFile
 from django.db import models
 from django.utils.timezone import utc
-from django.utils.translation import ugettext_lazy as _
-from django.core.urlresolvers import reverse
 from django.contrib.auth.models import User
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes.fields import GenericForeignKey
 from django.conf import settings
 from django.contrib.auth.models import User
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes.fields import GenericForeignKey
 from django.conf import settings
+from django.urls import reverse
 
 
-from jsonfield import JSONField
 from catalogue.models import Book, Tag
 
 
 class Poem(models.Model):
 from catalogue.models import Book, Tag
 
 
 class Poem(models.Model):
-    slug = models.SlugField(_('slug'), max_length=120, db_index=True)
-    text = models.TextField(_('text'))
-    created_by = models.ForeignKey(User, null=True)
-    created_from = JSONField(_('extra information'), null=True, blank=True)
-    created_at = models.DateTimeField(_('creation date'), auto_now_add=True, editable=False)
-    seen_at = models.DateTimeField(_('last view date'), auto_now_add=True, editable=False)
-    view_count = models.IntegerField(_('view count'), default=1)
+    slug = models.SlugField('slug', max_length=120, db_index=True)
+    text = models.TextField('tekst')
+    created_by = models.ForeignKey(User, models.SET_NULL, null=True)
+    created_from = models.TextField('dodatkowe informacje', null=True, blank=True)
+    created_at = models.DateTimeField('data utworzenia', auto_now_add=True, editable=False)
+    seen_at = models.DateTimeField('data ostatniego obejrzenia', auto_now_add=True, editable=False)
+    view_count = models.IntegerField('licznik obejrzeń', default=1)
 
     try:
 
     try:
-        f = open(settings.LESMIANATOR_PICKLE)
-        global_dictionary = cPickle.load(f)
+        f = open(settings.LESMIANATOR_PICKLE, 'rb')
+        global_dictionary = pickle.load(f)
         f.close()
     except (IOError, AttributeError, PickleError):
         global_dictionary = {}
         f.close()
     except (IOError, AttributeError, PickleError):
         global_dictionary = {}
@@ -43,13 +40,13 @@ class Poem(models.Model):
         self.seen_at = datetime.utcnow().replace(tzinfo=utc)
         self.save()
 
         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
     def choose_letter(word, continuations):
         if word not in continuations:
         return "%s (%s...)" % (self.slug, self.text[:20])
 
     @staticmethod
     def choose_letter(word, continuations):
         if word not in continuations:
-            return u'\n'
+            return '\n'
 
         choices = sum((continuations[word][letter]
                        for letter in continuations[word]))
 
         choices = sum((continuations[word][letter]
                        for letter in continuations[word]))
@@ -68,7 +65,7 @@ class Poem(models.Model):
             return ''
 
         letters = []
             return ''
 
         letters = []
-        word = u''
+        word = ''
 
         finished_stanza_verses = 0
         current_stanza_verses = 0
 
         finished_stanza_verses = 0
         current_stanza_verses = 0
@@ -84,7 +81,7 @@ class Poem(models.Model):
             word = word[-length + 1:] + letter
             char_count += 1
 
             word = word[-length + 1:] + letter
             char_count += 1
 
-            if letter == u'\n':
+            if letter == '\n':
                 if verse_start:
                     finished_stanza_verses += current_stanza_verses
                     current_stanza_verses = 0
                 if verse_start:
                     finished_stanza_verses += current_stanza_verses
                     current_stanza_verses = 0
@@ -101,16 +98,16 @@ class Poem(models.Model):
 
 
 class Continuations(models.Model):
 
 
 class Continuations(models.Model):
-    pickle = models.FileField(_('Continuations file'), upload_to='lesmianator')
-    content_type = models.ForeignKey(ContentType)
+    pickle = models.FileField('plik kontynuacji', upload_to='lesmianator')
+    content_type = models.ForeignKey(ContentType, models.CASCADE)
     object_id = models.PositiveIntegerField()
     content_object = GenericForeignKey('content_type', 'object_id')
 
     class Meta:
         unique_together = (('content_type', 'object_id'), )
 
     object_id = models.PositiveIntegerField()
     content_object = GenericForeignKey('content_type', 'object_id')
 
     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):
 
     @staticmethod
     def join_conts(a, b):
@@ -124,9 +121,8 @@ class Continuations(models.Model):
     @classmethod
     def for_book(cls, book, length=3):
         # count from this book only
     @classmethod
     def for_book(cls, book, length=3):
         # count from this book only
-        output = StringIO()
         wldoc = book.wldocument(parse_dublincore=False)
         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 = {}
         del wldoc
 
         conts = {}
@@ -157,8 +153,8 @@ class Continuations(models.Model):
             obj = cls.objects.get(content_type=object_type, object_id=sth.id)
             if not obj.pickle:
                 raise cls.DoesNotExist
             obj = cls.objects.get(content_type=object_type, object_id=sth.id)
             if not obj.pickle:
                 raise cls.DoesNotExist
-            f = open(obj.pickle.path)
-            keys, conts = cPickle.load(f)
+            f = open(obj.pickle.path, 'rb')
+            keys, conts = pickle.load(f)
             f.close()
             if set(keys) != should_keys:
                 raise cls.DoesNotExist
             f.close()
             if set(keys) != should_keys:
                 raise cls.DoesNotExist
@@ -172,6 +168,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)
                 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
             c.save()
             return conts