X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/c51d32b7b97016260e4c60dda9fbbd9eb25420ce..ac5de8695878cb6aabf9161930af189cf5d15316:/src/documents/views.py?ds=sidebyside diff --git a/src/documents/views.py b/src/documents/views.py index 97d2ad6d..1fe7b054 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -5,12 +5,13 @@ from collections import defaultdict from copy import deepcopy from datetime import datetime, date, timedelta from itertools import zip_longest +import json import logging 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 @@ -25,6 +26,7 @@ from django.utils.translation import gettext_lazy as _ from django.views.decorators.http import require_POST from django_cas_ng.decorators import user_passes_test import requests +from lxml import etree from librarian import epubcheck from librarian.html import raw_printable_text @@ -262,7 +264,11 @@ 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, + base_url=request.build_absolute_uri(book.gallery_url()), + options={'gallery': "'%s'" % book.gallery_url()}) html = html.get_bytes().decode('utf-8') if html is not None else '' # response = http.HttpResponse(html, content_type='text/html') @@ -288,7 +294,7 @@ def book_pdf(request, slug, mobile=False): 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) + pdf_file = doc.as_pdf(cover=True, base_url=request.build_absolute_uri(book.gallery_url()), customizations=customizations) from .ebook_utils import serve_file return serve_file(pdf_file.get_filename(), book.slug + '.pdf', 'application/pdf') @@ -412,9 +418,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() @@ -674,6 +685,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): @@ -770,14 +797,29 @@ def mark_final_completed(request): return render(request, 'documents/mark_final_completed.html') +@login_required def synchro(request, slug): book = get_object_or_404(Book, slug=slug) if not book.accessible(request): return HttpResponseForbidden("Not authorized.") - document = book.wldocument(librarian2=True) + if request.method == 'POST': + #hints = json.loads(request.POST.get('hints')) + chunk = book[0] + tree = etree.fromstring(chunk.head.materialize()) + m = tree.find('.//meta[@id="synchro"]') + if m is None: + rdf = tree.find('.//{http://www.w3.org/1999/02/22-rdf-syntax-ns#}Description') + m = etree.SubElement(rdf, 'meta', id="synchro") + m.tail = '\n' + m.text = request.POST.get('hints') + text = etree.tostring(tree, encoding='unicode') + chunk.commit(text, author=request.user, description='Synchronizacja') + return HttpResponseRedirect('') + + document = book.wldocument(librarian2=True, publishable=False) + slug = document.meta.url.slug - print(f'https://audio.wolnelektury.pl/archive/book/{slug}.json') error = None try: items = requests.get(f'https://audio.wolnelektury.pl/archive/book/{slug}.json').json()['items'] @@ -792,59 +834,49 @@ def synchro(request, slug): split_on = ( 'naglowek_rozdzial', 'naglowek_scena', - ) - - if split_on: - documents = [] - headers = [('Początek', 0, 0)] - present = True - n = 0 - while present: - present = False - n += 1 - newdoc = deepcopy(document) - newdoc.tree.getroot().document = newdoc - - master = newdoc.tree.getroot()[-1] - i = 0 - for item in list(master): - #chunkno, sourceline = 0, self.sourceline - #if builder.splits: - # chunkno, sourceline = len(builder.splits), sourceline - builder.splits[-1] - - if 'forcesplit' in item.attrib or (item.tag in split_on and 'nosplit' not in item.attrib): - # TODO: clear - i += 1 - if n > 1 and i == n: - headers.append(( - raw_printable_text(item), - 0, - item.sourceline, - )) - if i != n and not (n == 1 and not i): - master.remove(item) - else: - present = True - if present: - documents.append(newdoc) - else: - documents = [document] - headers = [( - document.meta.title, 0 ,0 - )] - - length_ok = len(headers) == len(mp3) - table = zip_longest(headers, mp3) - + ) + split_other = ( + 'naglowek_czesc', + 'naglowek_akt', + 'naglowek_podrozdzial', + 'srodtytul', + ) + + headers = [] + headers_other = [] + master = document.tree.getroot()[-1] + for item in master: + if item.tag in split_on: + headers.append([ + item.tag, + raw_printable_text(item), + 0, + item.sourceline, + ]) + if item.tag in split_other: + headers_other.append([ + item.tag, + raw_printable_text(item), + 0, + item.sourceline, + ]) + + hints = [] + m = document.tree.find('.//meta[@id="synchro"]') + if m is not None: + try: + hints = json.loads(m.text) + except: + raise + pass return render(request, 'documents/synchro.html', { 'book': book, - 'documents': documents, 'headers': headers, + 'headers_other': headers_other, 'mp3': mp3, - 'length_ok': length_ok, - 'table': table, 'error': error, + 'hints': hints, })