+
+
+@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())