publish PDF with images
authorJan Szejko <j-sz@o2.pl>
Tue, 15 Mar 2016 15:58:37 +0000 (16:58 +0100)
committerJan Szejko <j-sz@o2.pl>
Tue, 15 Mar 2016 15:58:37 +0000 (16:58 +0100)
lib/librarian
src/catalogue/fields.py
src/catalogue/forms.py
src/catalogue/models/book.py
src/catalogue/utils.py
src/wolnelektury/settings/static.py

index fc7a836..3acdb97 160000 (submodule)
@@ -1 +1 @@
-Subproject commit fc7a8368b6033616780d563149d78db33b6b91a3
+Subproject commit 3acdb97ed96bb54a04c031e017c2059f86db57e4
index 4261252..bc05aa3 100644 (file)
@@ -8,7 +8,7 @@ from django.db import models
 from django.db.models.fields.files import FieldFile
 from catalogue import app_settings
 from catalogue.constants import LANGUAGES_3TO2
-from catalogue.utils import remove_zip, truncate_html_words
+from catalogue.utils import remove_zip, truncate_html_words, gallery_path
 from celery.task import Task, task
 from celery.utils.log import get_task_logger
 from waiter.utils import clear_cache
@@ -117,7 +117,8 @@ class BuildTxt(BuildEbook):
 class BuildPdf(BuildEbook):
     @staticmethod
     def transform(wldoc, fieldfile):
-        return wldoc.as_pdf(morefloats=settings.LIBRARIAN_PDF_MOREFLOATS, cover=True)
+        return wldoc.as_pdf(morefloats=settings.LIBRARIAN_PDF_MOREFLOATS, cover=True,
+                            ilustr_path=gallery_path(wldoc.book_info.url.slug))
 
     def build(self, fieldfile):
         BuildEbook.build(self, fieldfile)
index 82a5d1f..1b03455 100644 (file)
@@ -2,6 +2,9 @@
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
+import urllib
+import os.path
+
 from django import forms
 from django.utils.translation import ugettext_lazy as _
 
@@ -10,11 +13,13 @@ from waiter.models import WaitedFile
 from django.core.exceptions import ValidationError
 from catalogue.utils import get_customized_pdf_path
 from catalogue.tasks import build_custom_pdf
+from wolnelektury.utils import makedirs
 
 
 class BookImportForm(forms.Form):
     book_xml_file = forms.FileField(required=False)
     book_xml = forms.CharField(required=False)
+    gallery_url = forms.CharField(required=False)
 
     def clean(self):
         from django.core.files.base import ContentFile
@@ -22,13 +27,14 @@ class BookImportForm(forms.Form):
         if not self.cleaned_data['book_xml_file']:
             if self.cleaned_data['book_xml']:
                 self.cleaned_data['book_xml_file'] = \
-                        ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
+                    ContentFile(self.cleaned_data['book_xml'].encode('utf-8'))
             else:
                 raise forms.ValidationError(_("Please supply an XML."))
         return super(BookImportForm, self).clean()
 
     def save(self, commit=True, **kwargs):
-        return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True, **kwargs)
+        return Book.from_xml_file(self.cleaned_data['book_xml_file'], overwrite=True,
+                                  remote_gallery_url=self.cleaned_data['gallery_url'], **kwargs)
 
 
 FORMATS = [(f, f.upper()) for f in Book.ebook_formats]
index 5194fe1..6e66f2e 100644 (file)
@@ -4,7 +4,9 @@
 #
 from collections import OrderedDict
 from random import randint
+import os.path
 import re
+import urllib
 from django.conf import settings
 from django.db import connection, models, transaction
 from django.db.models import permalink
@@ -19,9 +21,10 @@ from newtagging import managers
 from catalogue import constants
 from catalogue.fields import EbookField
 from catalogue.models import Tag, Fragment, BookMedia
-from catalogue.utils import create_zip
+from catalogue.utils import create_zip, gallery_url, gallery_path
 from catalogue import app_settings
 from catalogue import tasks
+from wolnelektury.utils import makedirs
 
 bofh_storage = BofhFileSystemStorage()
 
@@ -131,6 +134,12 @@ class Book(models.Model):
     def create_url(slug):
         return 'catalogue.views.book_detail', [slug]
 
+    def gallery_path(self):
+        return gallery_path(self.slug)
+
+    def gallery_url(self):
+        return gallery_url(self.slug)
+
     @property
     def name(self):
         return self.title
@@ -236,6 +245,16 @@ class Book(models.Model):
             index.index.rollback()
             raise e
 
+    def download_pictures(self, remote_gallery_url):
+        gallery_path = self.gallery_path()
+        ilustr_elements = list(self.wldocument().edoc.findall('//ilustr'))
+        if ilustr_elements:
+            makedirs(gallery_path)
+            for ilustr in ilustr_elements:
+                ilustr_src = ilustr.get('src')
+                ilustr_path = os.path.join(gallery_path, ilustr_src)
+                urllib.urlretrieve('%s/%s' % (remote_gallery_url, ilustr_src), ilustr_path)
+
     @classmethod
     def from_xml_file(cls, xml_file, **kwargs):
         from django.core.files import File
@@ -254,7 +273,7 @@ class Book(models.Model):
 
     @classmethod
     def from_text_and_meta(cls, raw_file, book_info, overwrite=False, dont_build=None, search_index=True,
-                           search_index_tags=True):
+                           search_index_tags=True, remote_gallery_url=None):
         if dont_build is None:
             dont_build = set()
         dont_build = set.union(set(dont_build), set(app_settings.DONT_BUILD))
@@ -322,6 +341,9 @@ class Book(models.Model):
         cls.repopulate_ancestors()
         tasks.update_counters.delay()
 
+        if remote_gallery_url:
+            book.download_pictures(remote_gallery_url)
+
         # No saves beyond this point.
 
         # Build cover.
index e79b439..e94c67d 100644 (file)
@@ -4,6 +4,7 @@
 #
 from collections import defaultdict
 import hashlib
+import os.path
 import random
 import re
 import time
@@ -345,3 +346,11 @@ class AppSettings(object):
 
 def delete_from_cache_by_language(cache, key_template):
     cache.delete_many([key_template % lc for lc, ln in settings.LANGUAGES])
+
+
+def gallery_path(slug):
+    return os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR, slug)
+
+
+def gallery_url(slug):
+    return '%s%s%s/' % (settings.MEDIA_URL, settings.IMAGE_DIR, slug)
index e876ec8..6f7938d 100644 (file)
@@ -17,6 +17,8 @@ SEARCH_INDEX = path.join(VAR_DIR, 'search_index/')
 MEDIA_URL = '/media/'
 STATIC_URL = '/static/'
 
+IMAGE_DIR = 'book/pictures/'
+
 # CSS and JavaScript file groups
 
 PIPELINE = {