Django 2.0
[redakcja.git] / src / catalogue / models / book.py
index 1fcd05a..fc0e18f 100755 (executable)
@@ -1,11 +1,10 @@
-# -*- coding: utf-8 -*-
-#
 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.contrib.sites.models import Site
 from django.db import models, transaction
 from django.template.loader import render_to_string
+from django.urls import reverse
 from django.utils.translation import ugettext_lazy as _
 from django.conf import settings
 from slugify import slugify
@@ -15,7 +14,6 @@ import apiclient
 from catalogue.helpers import cached_in_field, GalleryMerger
 from catalogue.models import BookPublishRecord, ChunkPublishRecord, Project
 from catalogue.signals import post_publish
-from catalogue.tasks import refresh_instance, book_content_updated
 from catalogue.xml_tools import compile_text, split_xml
 from cover.models import Image
 import os
@@ -29,14 +27,13 @@ class Book(models.Model):
     slug = models.SlugField(_('slug'), max_length=128, unique=True, db_index=True)
     public = models.BooleanField(_('public'), default=True, db_index=True)
     gallery = models.CharField(_('scan gallery name'), max_length=255, blank=True)
-    project = models.ForeignKey(Project, null=True, blank=True)
+    project = models.ForeignKey(Project, models.SET_NULL, null=True, blank=True)
 
     #wl_slug = models.CharField(_('title'), max_length=255, null=True, db_index=True, editable=False)
-    parent = models.ForeignKey('self', null=True, blank=True, verbose_name=_('parent'), related_name="children", editable=False)
+    parent = models.ForeignKey('self', models.SET_NULL, null=True, blank=True, verbose_name=_('parent'), related_name="children", editable=False)
     parent_number = models.IntegerField(_('parent number'), null=True, blank=True, db_index=True, editable=False)
 
     # Cache
-    _short_html = models.TextField(null=True, blank=True, editable=False)
     _single = models.NullBooleanField(editable=False, db_index=True)
     _new_publishable = models.NullBooleanField(editable=False)
     _published = models.NullBooleanField(editable=False)
@@ -68,18 +65,17 @@ class Book(models.Model):
     def __len__(self):
         return self.chunk_set.count()
 
-    def __nonzero__(self):
+    def __bool__(self):
         """
             Necessary so that __len__ isn't used for bool evaluation.
         """
         return True
 
-    def __unicode__(self):
+    def __str__(self):
         return self.title
 
-    @models.permalink
     def get_absolute_url(self):
-        return ("catalogue_book", [self.slug])
+        return reverse("catalogue_book", args=[self.slug])
 
     def correct_about(self):
         return "http://%s%s" % (
@@ -97,7 +93,7 @@ class Book(models.Model):
     # =======================
 
     def accessible(self, request):
-        return self.public or request.user.is_authenticated()
+        return self.public or request.user.is_authenticated
 
     @classmethod
     @transaction.atomic
@@ -268,12 +264,12 @@ class Book(models.Model):
 
         try:
             bi = self.wldocument(changes=changes, strict=True).book_info
-        except ParseError, e:
-            raise AssertionError(_('Invalid XML') + ': ' + unicode(e))
+        except ParseError as e:
+            raise AssertionError(_('Invalid XML') + ': ' + str(e))
         except NoDublinCore:
             raise AssertionError(_('No Dublin Core found.'))
-        except ValidationError, e:
-            raise AssertionError(_('Invalid Dublin Core') + ': ' + unicode(e))
+        except ValidationError as e:
+            raise AssertionError(_('Invalid Dublin Core') + ': ' + str(e))
 
         valid_about = self.correct_about()
         assert bi.about == valid_about, _("rdf:about is not") + " " + valid_about
@@ -281,7 +277,7 @@ class Book(models.Model):
     def publishable_error(self):
         try:
             return self.assert_publishable()
-        except AssertionError, e:
+        except AssertionError as e:
             return e
         else:
             return None
@@ -326,10 +322,6 @@ class Book(models.Model):
         return len(self) == 1
     single = cached_in_field('_single')(is_single)
 
-    @cached_in_field('_short_html')
-    def short_html(self):
-        return render_to_string('catalogue/book_list/book.html', {'book': self})
-
     def book_info(self, publishable=True):
         try:
             book_xml = self.materialize(publishable=publishable)
@@ -363,25 +355,14 @@ class Book(models.Model):
         Book.objects.filter(pk=self.pk).update(**update)
 
     def touch(self):
-        # this should only really be done when text or publishable status changes
-        book_content_updated.delay(self)
-
         update = {
             "_new_publishable": self.is_new_publishable(),
             "_published": self.is_published(),
             "_single": self.is_single(),
             "_on_track": self.get_on_track(),
-            "_short_html": None,
         }
         Book.objects.filter(pk=self.pk).update(**update)
-        refresh_instance(self)
-
-    def refresh(self):
-        """This should be done offline."""
-        self.short_html
-        self.single
-        self.new_publishable
-        self.published
+        self.refresh_dc_cache()
 
     # Materializing & publishing
     # ==========================