X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/2dd4071b8858c9727ee3e2c09b73b1eadef6e31e..c46844cb5a794566d15437b0c4a809e9230baecf:/src/documents/views.py?ds=sidebyside diff --git a/src/documents/views.py b/src/documents/views.py index 0fbb3b9f..8f403034 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -2,7 +2,9 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from collections import defaultdict +from copy import deepcopy from datetime import datetime, date, timedelta +from itertools import zip_longest import logging import os from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit @@ -22,6 +24,10 @@ from django.utils.encoding import iri_to_uri 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 librarian import epubcheck +from librarian.html import raw_printable_text from apiclient import api_call, NotAuthorizedError from . import forms @@ -300,7 +306,8 @@ def book_epub(request, slug): from librarian.builders import EpubBuilder epub = EpubBuilder( - base_url='file://' + book.gallery_path() + '/' + 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' @@ -308,6 +315,35 @@ def book_epub(request, slug): 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): @@ -369,10 +405,18 @@ def book(request, slug): doc = None else: try: - stats = doc.get_statistic() + stats = doc.get_statistics() except: pass + if book.catalogue_book_id: + audio_items = requests.get(f'https://audio.wolnelektury.pl/archive/book/{book.catalogue_book_id}.json').json()['items'] + has_audio = bool(audio_items) + can_sell_audio = has_audio and all(x['project']['can_sell'] for x in audio_items) + else: + has_audio = None + can_sell_audio = None + return render(request, "documents/book_detail.html", { "book": book, "doc": doc, @@ -382,6 +426,8 @@ def book(request, slug): "form": form, "publish_options_form": publish_options_form, "editable": editable, + "has_audio": has_audio, + "can_sell_audio": can_sell_audio, }) @@ -716,3 +762,81 @@ def mark_final(request): def mark_final_completed(request): return render(request, 'documents/mark_final_completed.html') + + +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) + 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'] + except: + error = 'Błąd połączenia z repozytorium audio.' + items = [] + else: + mp3 = [ + item['part'] for item in items + ] + + 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) + + + return render(request, 'documents/synchro.html', { + 'book': book, + 'documents': documents, + 'headers': headers, + 'mp3': mp3, + 'length_ok': length_ok, + 'table': table, + 'error': error, + })