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