Hard linking of texts to catalogue.
[redakcja.git] / src / documents / models / book.py
index 4255530..293d2c4 100644 (file)
@@ -2,14 +2,16 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.apps import apps
+from django.core.files.base import ContentFile
 from django.contrib.sites.models import Site
-from django.db import models, transaction
+from django.db import connection, 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.utils.translation import gettext_lazy as _
 from django.conf import settings
 from slugify import slugify
-
+from librarian.cover import make_cover
+from librarian.dcparser import BookInfo
 
 import apiclient
 from documents.helpers import cached_in_field, GalleryMerger
@@ -17,9 +19,12 @@ from documents.models import BookPublishRecord, ChunkPublishRecord, Project
 from documents.signals import post_publish
 from documents.xml_tools import compile_text, split_xml
 from cover.models import Image
+from io import BytesIO
 import os
 import shutil
 import re
+from urllib.parse import urljoin
+
 
 class Book(models.Model):
     """ A document edited on the wiki """
@@ -40,13 +45,19 @@ class Book(models.Model):
     _on_track = models.IntegerField(null=True, blank=True, db_index=True, editable=False)
     dc_cover_image = models.ForeignKey(Image, blank=True, null=True,
         db_index=True, on_delete=models.SET_NULL, editable=False)
+    dc = models.JSONField(null=True, editable=False)
+    cover = models.FileField(blank=True, upload_to='documents/cover')
+
+    dc_slug = models.CharField(
+        max_length=2048,
+        null=True, blank=True,
+        editable=False,
+    )
     catalogue_book = models.ForeignKey(
         'catalogue.Book',
-        models.DO_NOTHING,
-        to_field='slug',
+        models.PROTECT,
         null=True, blank=True,
-        db_constraint=False,
-        editable=False, db_index=True,
+        editable=False,
         related_name='document_books',
         related_query_name='document_book',
     )
@@ -67,6 +78,14 @@ class Book(models.Model):
             qs = qs.filter(public=True)
         return qs
 
+    @staticmethod
+    def q_dc(field, field_plural, value, prefix=''):
+        if connection.features.supports_json_field_contains:
+            return models.Q(**{f'{prefix}dc__{field_plural}__contains': value})
+        else:
+            return models.Q(**{f'{prefix}dc__{field}': value})
+            
+    
     # Representing
     # ============
 
@@ -272,7 +291,7 @@ class Book(models.Model):
         try:
             changes = self.get_current_changes(publishable=True)
         except self.NoTextError:
-            raise AssertionError(_('Not all chunks have publishable revisions.'))
+            raise AssertionError(_('Not all chunks have approved revisions.'))
 
         from librarian import NoDublinCore, ParseError, ValidationError
 
@@ -351,21 +370,22 @@ class Book(models.Model):
 
     def refresh_dc_cache(self):
         update = {
-            'catalogue_book_id': None,
+            'dc_slug': None,
             'dc_cover_image': None,
         }
 
         info = self.book_info()
         if info is not None:
-            update['catalogue_book_id'] = info.url.slug
+            update['dc_slug'] = info.url.slug
             if info.cover_source:
                 try:
                     image = Image.objects.get(pk=int(info.cover_source.rstrip('/').rsplit('/', 1)[-1]))
                 except:
                     pass
                 else:
-                    if info.cover_source == image.get_full_url():
+                    if info.cover_source.rstrip('/') == image.get_full_url().rstrip('/'):
                         update['dc_cover_image'] = image
+            update['dc'] = info.to_dict()
         Book.objects.filter(pk=self.pk).update(**update)
 
     def touch(self):
@@ -377,6 +397,25 @@ class Book(models.Model):
         }
         Book.objects.filter(pk=self.pk).update(**update)
         self.refresh_dc_cache()
+        self.build_cover()
+
+    def build_cover(self):
+        width, height = 212, 300
+        try:
+            xml = self.materialize(publishable=True).encode('utf-8')
+            info = BookInfo.from_bytes(xml)
+            kwargs = {}
+            if self.project is not None:
+                if self.project.logo_mono or self.project.logo:
+                    kwargs['cover_logo'] = (self.project.logo_mono or self.project.logo).path
+            cover = make_cover(info, width=width, height=height, **kwargs)
+            out = BytesIO()
+            ext = cover.ext()
+            cover.save(out)
+            self.cover.save(f'{self.slug}.{ext}', out, save=False)
+            type(self).objects.filter(pk=self.pk).update(cover=self.cover)
+        except:
+            type(self).objects.filter(pk=self.pk).update(cover='')
 
     # Materializing & publishing
     # ==========================
@@ -407,13 +446,21 @@ class Book(models.Model):
         return compile_text(change.materialize() for change in changes)
 
     def wldocument(self, publishable=True, changes=None, 
-            parse_dublincore=True, strict=False):
+                   parse_dublincore=True, strict=False, librarian2=False):
         from documents.ebook_utils import RedakcjaDocProvider
         from librarian.parser import WLDocument
-
+        from librarian.document import WLDocument as WLDocument2
+
+        provider = RedakcjaDocProvider(publishable=publishable)
+        xml = self.materialize(publishable=publishable, changes=changes).encode('utf-8')
+        
+        if librarian2:
+            return WLDocument2(
+                BytesIO(xml),
+                provider=provider)
         return WLDocument.from_bytes(
-                self.materialize(publishable=publishable, changes=changes).encode('utf-8'),
-                provider=RedakcjaDocProvider(publishable=publishable),
+                xml,
+                provider=provider,
                 parse_dublincore=parse_dublincore,
                 strict=strict)
 
@@ -426,6 +473,19 @@ class Book(models.Model):
         if not fake:
             book_xml = self.materialize(changes=changes)
             data = {"book_xml": book_xml, "days": days, "hidden": hidden}
+            if self.project is not None:
+                if self.project.logo:
+                    data['logo'] = urljoin(
+                        'https://' + Site.objects.get_current().domain,
+                        self.project.logo.url,
+                    )
+                if self.project.logo_mono:
+                    data['logo_mono'] = urljoin(
+                        'https://' + Site.objects.get_current().domain,
+                        self.project.logo_mono.url,
+                    )
+                if self.project.logo_alt:
+                    data['logo_alt'] = self.project.logo_alt
             if host:
                 data['gallery_url'] = host + self.gallery_url()
             apiclient.api_call(user, "books/", data, beta=beta)