-Subproject commit fc7a8368b6033616780d563149d78db33b6b91a3
+Subproject commit 3acdb97ed96bb54a04c031e017c2059f86db57e4
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
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)
# 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 _
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
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]
#
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
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()
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
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
@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))
cls.repopulate_ancestors()
tasks.update_counters.delay()
+ if remote_gallery_url:
+ book.download_pictures(remote_gallery_url)
+
# No saves beyond this point.
# Build cover.
#
from collections import defaultdict
import hashlib
+import os.path
import random
import re
import time
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)
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
+IMAGE_DIR = 'book/pictures/'
+
# CSS and JavaScript file groups
PIPELINE = {