X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/fa491172e51c164fd620f73859788dfce2ebc9ac..52c41c20759ed4920d5a1f4008e7e0bfd9fa70f9:/src/documents/views.py?ds=inline diff --git a/src/documents/views.py b/src/documents/views.py index e87dc6db..fd1eef66 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -11,7 +11,7 @@ import os from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit from django.conf import settings -from django.contrib import auth +from django.contrib import auth, messages from django.contrib.auth.models import User from django.contrib.auth.decorators import login_required, permission_required from django.urls import reverse @@ -37,6 +37,7 @@ from . import helpers from .helpers import active_tab from .models import (Book, Chunk, Image, BookPublishRecord, ChunkPublishRecord, ImagePublishRecord, Project) +from .remote_build import remote_build import catalogue.models from fileupload.views import UploadView @@ -264,7 +265,10 @@ def book_html(request, slug): return HttpResponseForbidden("Not authorized.") doc = book.wldocument(parse_dublincore=False) - html = doc.as_html(options={'gallery': "'%s'" % book.gallery_url()}) + html = doc.as_html( + gallery_url='%stmp/html/' % settings.MEDIA_URL, + gallery_path='%s/tmp/html/' % settings.MEDIA_ROOT, + ) html = html.get_bytes().decode('utf-8') if html is not None else '' # response = http.HttpResponse(html, content_type='text/html') @@ -286,14 +290,24 @@ def book_pdf(request, slug, mobile=False): if not book.accessible(request): return HttpResponseForbidden("Not authorized.") - # TODO: move to celery - doc = book.wldocument() - # TODO: error handling - customizations = ['26pt', 'nothemes', 'nomargins', 'notoc'] if mobile else None - pdf_file = doc.as_pdf(cover=True, base_url=request.build_absolute_uri(book.gallery_path()), customizations=customizations) - from .ebook_utils import serve_file - return serve_file(pdf_file.get_filename(), - book.slug + '.pdf', 'application/pdf') + customizations = ['notoc'] + if mobile: + customizations += ['26pt', 'nothemes', 'nomargins'] + options = { + 'customizations': customizations + } + if book.project is not None and (logo := book.project.logo_for_cover()): + options['cover_logo'] = logo + try: + content = remote_build(book, 'pdf', options=options) + except RuntimeError as e: + return HttpResponse(str(e)) + + response = HttpResponse(content_type='application/pdf') + response['Content-Disposition'] = f'attachment; filename={book.dc_slug}.pdf' + response.write(content) + return response + @login_required @@ -302,51 +316,22 @@ def book_epub(request, slug): book = get_object_or_404(Book, slug=slug) if not book.accessible(request): return HttpResponseForbidden("Not authorized.") + options = { + #'debug': True + } + if book.project is not None and (logo := book.project.logo_for_cover()): + options['cover_logo'] = logo + try: + content = remote_build(book, 'epub', options=options) + except RuntimeError as e: + return HttpResponse(str(e)) - # TODO: move to celery - doc = book.wldocument(librarian2=True) - # TODO: error handling - - from librarian.builders import EpubBuilder - epub = EpubBuilder( - base_url='file://' + book.gallery_path() + '/', - debug=True - ).build(doc).get_bytes() response = HttpResponse(content_type='application/epub+zip') response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub' - response.write(epub) + response.write(content) return response -@login_required -@never_cache -def book_epubcheck(request, slug): - book = get_object_or_404(Book, slug=slug) - if not book.accessible(request): - return HttpResponseForbidden("Not authorized.") - - # TODO: move to celery - doc = book.wldocument(librarian2=True) - # TODO: error handling - - from librarian.builders import EpubBuilder - epub = EpubBuilder( - base_url='file://' + book.gallery_path() + '/', - debug=True - ).build(doc) - fname = epub.get_filename() - - messages = epubcheck.epubcheck(fname) - for message in messages: - for location in message.get('locations', []): - if 'wl_chunk' in location: - location['wl_chunk'] = book[location['wl_chunk']] - return render(request, 'documents/book_epubcheck.html', { - 'messages': messages, - 'book': book, - }) - - @login_required @never_cache def book_mobi(request, slug): @@ -354,16 +339,16 @@ def book_mobi(request, slug): if not book.accessible(request): return HttpResponseForbidden("Not authorized.") - # TODO: move to celery - doc = book.wldocument(librarian2=True) - # TODO: error handling - from librarian.builders import MobiBuilder - mobi = MobiBuilder( - base_url='file://' + book.gallery_path() + '/' - ).build(doc).get_bytes() + options = {} + if book.project is not None and (logo := book.project.logo_for_cover()): + options['cover_logo'] = logo + try: + content = remote_build(book, 'mobi', options=options) + except RuntimeError as e: + return HttpResponse(str(e)) response = HttpResponse(content_type='application/x-mobipocket-ebook') - response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.mobi' - response.write(mobi) + response['Content-Disposition'] = f'attachment; filename={book.dc_slug}.mobi' + response.write(content) return response @@ -414,9 +399,14 @@ def book(request, slug): cbook_by_slug = None if book.dc_slug: - audio_items = requests.get(f'https://audio.wolnelektury.pl/archive/book/{book.dc_slug}.json').json()['items'] - has_audio = bool(audio_items) - can_sell_audio = has_audio and all(x['project']['can_sell'] for x in audio_items) + try: + audio_items = requests.get(f'https://audio.wolnelektury.pl/archive/book/{book.dc_slug}.json').json()['items'] + except: + has_audio = None + can_sell_audio = None + else: + has_audio = bool(audio_items) + can_sell_audio = has_audio and all(x['project']['can_sell'] for x in audio_items) if book.catalogue_book is None or book.dc_slug != book.catalogue_book.slug: cbook_by_slug = catalogue.models.Book.objects.filter(slug=book.dc_slug).first() @@ -676,6 +666,22 @@ def publish(request, slug): return http.HttpResponseRedirect(book.get_absolute_url()) +@require_POST +@login_required +def fix_ilustr(request, slug): + from .ilustr import IlustrFixer + book = get_object_or_404(Book, slug=slug) + protocol = 'https://' if request.is_secure() else 'http://' + fixer = IlustrFixer(protocol + request.get_host(), author=request.user) + with transaction.atomic(): + fixer.fix_book(book) + if fixer.stats['unresolved'] or fixer.stats['conflicts']: + messages.warning(request, 'Nie wszystkie ilustracje udało się naprawić.') + elif fixer.stats['fixed']: + messages.success(request, 'Naprawiono adresy ilustracji.') + return http.HttpResponseRedirect(book.get_absolute_url()) + + @require_POST @login_required def publish_image(request, slug):