From: Jan Szejko Date: Tue, 15 Mar 2016 15:58:37 +0000 (+0100) Subject: publish PDF with images X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/c3fc1fa1087b5c12e287f6a7194d7a98fc27817e?hp=fdc9d830c69a5db8944e0978a9494068b6c0a8cd publish PDF with images --- diff --git a/lib/librarian b/lib/librarian index fc7a8368b..3acdb97ed 160000 --- a/lib/librarian +++ b/lib/librarian @@ -1 +1 @@ -Subproject commit fc7a8368b6033616780d563149d78db33b6b91a3 +Subproject commit 3acdb97ed96bb54a04c031e017c2059f86db57e4 diff --git a/src/catalogue/fields.py b/src/catalogue/fields.py index 42612522c..bc05aa35c 100644 --- a/src/catalogue/fields.py +++ b/src/catalogue/fields.py @@ -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) diff --git a/src/catalogue/forms.py b/src/catalogue/forms.py index 82a5d1f1a..1b034551f 100644 --- a/src/catalogue/forms.py +++ b/src/catalogue/forms.py @@ -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] diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 5194fe1d6..6e66f2ee4 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -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. diff --git a/src/catalogue/utils.py b/src/catalogue/utils.py index e79b4392c..e94c67d0b 100644 --- a/src/catalogue/utils.py +++ b/src/catalogue/utils.py @@ -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) diff --git a/src/wolnelektury/settings/static.py b/src/wolnelektury/settings/static.py index e876ec801..6f7938ded 100644 --- a/src/wolnelektury/settings/static.py +++ b/src/wolnelektury/settings/static.py @@ -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 = {