X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/fbb8f42993d02108b506028dc85466a0541be359..fa491172e51c164fd620f73859788dfce2ebc9ac:/src/documents/views.py diff --git a/src/documents/views.py b/src/documents/views.py index 905e885b..e87dc6db 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -2,7 +2,10 @@ # 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 json import logging import os from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit @@ -22,14 +25,19 @@ 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 lxml import etree from librarian import epubcheck +from librarian.html import raw_printable_text + from apiclient import api_call, NotAuthorizedError from . import forms from . import helpers from .helpers import active_tab from .models import (Book, Chunk, Image, BookPublishRecord, ChunkPublishRecord, ImagePublishRecord, Project) +import catalogue.models from fileupload.views import UploadView # @@ -232,7 +240,7 @@ def book_xml(request, slug): @never_cache def book_xml_dc(request, slug): - book = get_object_or_404(Book, catalogue_book_id=slug) + book = get_object_or_404(Book, dc_slug=slug) return serve_xml(request, book, slug) @@ -404,6 +412,18 @@ def book(request, slug): except: pass + 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) + + 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() + else: + has_audio = None + can_sell_audio = None + return render(request, "documents/book_detail.html", { "book": book, "doc": doc, @@ -413,6 +433,9 @@ def book(request, slug): "form": form, "publish_options_form": publish_options_form, "editable": editable, + "has_audio": has_audio, + "can_sell_audio": can_sell_audio, + "cbook_by_slug": cbook_by_slug, }) @@ -747,3 +770,96 @@ def mark_final(request): 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.") + + 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 + 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', + ) + 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, + 'headers': headers, + 'headers_other': headers_other, + 'mp3': mp3, + 'error': error, + 'hints': hints, + }) + + +@permission_required('documents.change_book') +def attach_book_to_catalogue(request, pk): + dbook = get_object_or_404(Book, pk=pk) + if dbook.dc_slug: + cbook = get_object_or_404(catalogue.models.Book, slug=dbook.dc_slug) + dbook.catalogue_book = cbook + dbook.save() + return http.HttpResponseRedirect(dbook.get_absolute_url())