genre-thema mapping
[redakcja.git] / src / documents / views.py
1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from collections import defaultdict
5 from copy import deepcopy
6 from datetime import datetime, date, timedelta
7 from itertools import zip_longest
8 import logging
9 import os
10 from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit
11
12 from django.conf import settings
13 from django.contrib import auth
14 from django.contrib.auth.models import User
15 from django.contrib.auth.decorators import login_required, permission_required
16 from django.urls import reverse
17 from django.db.models import Count, Q
18 from django.db import transaction
19 from django import http
20 from django.http import Http404, HttpResponse, HttpResponseForbidden
21 from django.http.response import HttpResponseRedirect
22 from django.shortcuts import get_object_or_404, render
23 from django.utils.encoding import iri_to_uri
24 from django.utils.translation import gettext_lazy as _
25 from django.views.decorators.http import require_POST
26 from django_cas_ng.decorators import user_passes_test
27 import requests
28
29 from librarian import epubcheck
30 from librarian.html import raw_printable_text
31
32 from apiclient import api_call, NotAuthorizedError
33 from . import forms
34 from . import helpers
35 from .helpers import active_tab
36 from .models import (Book, Chunk, Image, BookPublishRecord, 
37         ChunkPublishRecord, ImagePublishRecord, Project)
38 from fileupload.views import UploadView
39
40 #
41 # Quick hack around caching problems, TODO: use ETags
42 #
43 from django.views.decorators.cache import never_cache
44
45 logger = logging.getLogger("fnp.documents")
46
47
48 @active_tab('all')
49 @never_cache
50 def document_list(request):
51     return render(request, 'documents/document_list.html')
52
53
54 @active_tab('images')
55 @never_cache
56 def image_list(request, user=None):
57     return render(request, 'documents/image_list.html')
58
59
60 @never_cache
61 def user(request, username):
62     user = get_object_or_404(User, username=username)
63     return render(request, 'documents/user_page.html', {"viewed_user": user})
64
65
66 @login_required
67 @active_tab('my')
68 @never_cache
69 def my(request):
70     last_books = sorted(request.session.get("wiki_last_books", {}).items(),
71         key=lambda x: x[1]['time'], reverse=True)
72     for k, v in last_books:
73         v['time'] = datetime.fromtimestamp(v['time'])
74     try:
75         resp = api_call(request.user, 'username/')
76     except NotAuthorizedError:
77         wllogin = None
78     else:
79         wllogin = resp['username']
80
81     return render(request, 'documents/my_page.html', {
82         'last_books': last_books,
83         "logout_to": '/',
84         "wllogin": wllogin,
85         })
86
87
88 @active_tab('users')
89 def users(request):
90     return render(request, 'documents/user_list.html', {
91         'users': User.objects.all().annotate(count=Count('chunk')).order_by(
92             '-count', 'last_name', 'first_name'),
93     })
94
95
96 @active_tab('activity')
97 def activity(request, isodate=None):
98     today = date.today()
99     try:
100         day = helpers.parse_isodate(isodate)
101     except ValueError:
102         day = today
103
104     if day > today:
105         raise Http404
106     if day != today:
107         next_day = day + timedelta(1)
108     prev_day = day - timedelta(1)
109
110     return render(request, 'documents/activity.html', locals())
111
112
113 @never_cache
114 def logout_then_redirect(request):
115     auth.logout(request)
116     return http.HttpResponseRedirect(quote_plus(request.GET.get('next', '/'), safe='/?='))
117
118
119 @permission_required('documents.add_book')
120 @active_tab('create')
121 def create_missing(request, slug=None):
122     if slug is None:
123         slug = ''
124     slug = slug.replace(' ', '-')
125
126     if request.method == "POST":
127         form = forms.DocumentCreateForm(request.POST, request.FILES)
128         if form.is_valid():
129             
130             if request.user.is_authenticated:
131                 creator = request.user
132             else:
133                 creator = None
134             book = Book.create(
135                 text=form.cleaned_data['text'],
136                 creator=creator,
137                 slug=form.cleaned_data['slug'],
138                 title=form.cleaned_data['title'],
139                 gallery=form.cleaned_data['gallery'],
140             )
141
142             return http.HttpResponseRedirect(reverse("documents_book", args=[book.slug]))
143     else:
144         form = forms.DocumentCreateForm(initial={
145                 "slug": slug,
146                 "title": slug.replace('-', ' ').title(),
147                 "gallery": slug,
148         })
149
150     return render(request, "documents/document_create_missing.html", {
151         "slug": slug,
152         "form": form,
153
154         "logout_to": '/',
155     })
156
157
158 @permission_required('documents.add_book')
159 @active_tab('upload')
160 def upload(request):
161     if request.method == "POST":
162         form = forms.DocumentsUploadForm(request.POST, request.FILES)
163         if form.is_valid():
164             from slugify import slugify
165
166             if request.user.is_authenticated:
167                 creator = request.user
168             else:
169                 creator = None
170
171             zip = form.cleaned_data['zip']
172             skipped_list = []
173             ok_list = []
174             error_list = []
175             slugs = {}
176             existing = [book.slug for book in Book.objects.all()]
177             for filename in zip.namelist():
178                 if filename[-1] == '/':
179                     continue
180                 title = os.path.basename(filename)[:-4]
181                 slug = slugify(title)
182                 if not (slug and filename.endswith('.xml')):
183                     skipped_list.append(filename)
184                 elif slug in slugs:
185                     error_list.append((filename, slug, _('Slug already used for %s' % slugs[slug])))
186                 elif slug in existing:
187                     error_list.append((filename, slug, _('Slug already used in repository.')))
188                 else:
189                     try:
190                         zip.read(filename).decode('utf-8') # test read
191                         ok_list.append((filename, slug, title))
192                     except UnicodeDecodeError:
193                         error_list.append((filename, title, _('File should be UTF-8 encoded.')))
194                     slugs[slug] = filename
195
196             if not error_list:
197                 for filename, slug, title in ok_list:
198                     book = Book.create(
199                         text=zip.read(filename).decode('utf-8'),
200                         creator=creator,
201                         slug=slug,
202                         title=title,
203                     )
204
205             return render(request, "documents/document_upload.html", {
206                 "form": form,
207                 "ok_list": ok_list,
208                 "skipped_list": skipped_list,
209                 "error_list": error_list,
210
211                 "logout_to": '/',
212             })
213     else:
214         form = forms.DocumentsUploadForm()
215
216     return render(request, "documents/document_upload.html", {
217         "form": form,
218
219         "logout_to": '/',
220     })
221
222
223 def serve_xml(request, book, slug):
224     if not book.accessible(request):
225         return HttpResponseForbidden("Not authorized.")
226     xml = book.materialize(publishable=True)
227     response = http.HttpResponse(xml, content_type='application/xml')
228     response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
229     return response
230
231
232 @never_cache
233 def book_xml(request, slug):
234     book = get_object_or_404(Book, slug=slug)
235     return serve_xml(request, book, slug)
236
237
238 @never_cache
239 def book_xml_dc(request, slug):
240     book = get_object_or_404(Book, catalogue_book_id=slug)
241     return serve_xml(request, book, slug)
242
243
244 @never_cache
245 def book_txt(request, slug):
246     book = get_object_or_404(Book, slug=slug)
247     if not book.accessible(request):
248         return HttpResponseForbidden("Not authorized.")
249
250     doc = book.wldocument()
251     text = doc.as_text().get_bytes()
252     response = http.HttpResponse(text, content_type='text/plain')
253     response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug
254     return response
255
256
257 @never_cache
258 def book_html(request, slug):
259     book = get_object_or_404(Book, slug=slug)
260     if not book.accessible(request):
261         return HttpResponseForbidden("Not authorized.")
262
263     doc = book.wldocument(parse_dublincore=False)
264     html = doc.as_html(options={'gallery': "'%s'" % book.gallery_url()})
265
266     html = html.get_bytes().decode('utf-8') if html is not None else ''
267     # response = http.HttpResponse(html, content_type='text/html')
268     # return response
269     # book_themes = {}
270     # for fragment in book.fragments.all().iterator():
271     #     for theme in fragment.tags.filter(category='theme').iterator():
272     #         book_themes.setdefault(theme, []).append(fragment)
273
274     # book_themes = book_themes.items()
275     # book_themes.sort(key=lambda s: s[0].sort_key)
276     return render(request, 'documents/book_text.html', locals())
277
278
279 @login_required
280 @never_cache
281 def book_pdf(request, slug, mobile=False):
282     book = get_object_or_404(Book, slug=slug)
283     if not book.accessible(request):
284         return HttpResponseForbidden("Not authorized.")
285
286     # TODO: move to celery
287     doc = book.wldocument()
288     # TODO: error handling
289     customizations = ['26pt', 'nothemes', 'nomargins', 'notoc'] if mobile else None
290     pdf_file = doc.as_pdf(cover=True, base_url=request.build_absolute_uri(book.gallery_path()), customizations=customizations)
291     from .ebook_utils import serve_file
292     return serve_file(pdf_file.get_filename(),
293                 book.slug + '.pdf', 'application/pdf')
294
295
296 @login_required
297 @never_cache
298 def book_epub(request, slug):
299     book = get_object_or_404(Book, slug=slug)
300     if not book.accessible(request):
301         return HttpResponseForbidden("Not authorized.")
302
303     # TODO: move to celery
304     doc = book.wldocument(librarian2=True)
305     # TODO: error handling
306
307     from librarian.builders import EpubBuilder
308     epub = EpubBuilder(
309         base_url='file://' + book.gallery_path() + '/',
310         debug=True
311     ).build(doc).get_bytes()
312     response = HttpResponse(content_type='application/epub+zip')
313     response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
314     response.write(epub)
315     return response
316
317
318 @login_required
319 @never_cache
320 def book_epubcheck(request, slug):
321     book = get_object_or_404(Book, slug=slug)
322     if not book.accessible(request):
323         return HttpResponseForbidden("Not authorized.")
324
325     # TODO: move to celery
326     doc = book.wldocument(librarian2=True)
327     # TODO: error handling
328
329     from librarian.builders import EpubBuilder
330     epub = EpubBuilder(
331         base_url='file://' + book.gallery_path() + '/',
332         debug=True
333     ).build(doc)
334     fname = epub.get_filename()
335
336     messages = epubcheck.epubcheck(fname)
337     for message in messages:
338         for location in message.get('locations', []):
339             if 'wl_chunk' in location:
340                 location['wl_chunk'] = book[location['wl_chunk']]
341     return render(request, 'documents/book_epubcheck.html', {
342         'messages': messages,
343         'book': book,
344     })
345
346
347 @login_required
348 @never_cache
349 def book_mobi(request, slug):
350     book = get_object_or_404(Book, slug=slug)
351     if not book.accessible(request):
352         return HttpResponseForbidden("Not authorized.")
353
354     # TODO: move to celery
355     doc = book.wldocument(librarian2=True)
356     # TODO: error handling
357     from librarian.builders import MobiBuilder
358     mobi = MobiBuilder(
359         base_url='file://' + book.gallery_path() + '/'
360     ).build(doc).get_bytes()
361     response = HttpResponse(content_type='application/x-mobipocket-ebook')
362     response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.mobi'
363     response.write(mobi)
364     return response
365
366
367 @never_cache
368 def revision(request, slug, chunk=None):
369     try:
370         doc = Chunk.get(slug, chunk)
371     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
372         raise Http404
373     if not doc.book.accessible(request):
374         return HttpResponseForbidden("Not authorized.")
375     return http.HttpResponse(str(doc.revision()))
376
377
378 def book(request, slug):
379     book = get_object_or_404(Book, slug=slug)
380     if not book.accessible(request):
381         return HttpResponseForbidden("Not authorized.")
382
383     if request.user.has_perm('documents.change_book'):
384         if request.method == "POST":
385             form = forms.BookForm(request.POST, instance=book)
386             if form.is_valid():
387                 form.save()
388                 return http.HttpResponseRedirect(book.get_absolute_url())
389         else:
390             form = forms.BookForm(instance=book)
391         publish_options_form = forms.PublishOptionsForm()
392         editable = True
393     else:
394         form = forms.ReadonlyBookForm(instance=book)
395         publish_options_form = forms.PublishOptionsForm()
396         editable = False
397
398     publish_error = book.publishable_error()
399     publishable = publish_error is None
400
401     stats = None
402     try:
403         doc = book.wldocument(librarian2=True)
404     except:
405         doc = None
406     else:
407         try:
408             stats = doc.get_statistics()
409         except:
410             pass
411
412     if book.catalogue_book_id:
413         audio_items = requests.get(f'https://audio.wolnelektury.pl/archive/book/{book.catalogue_book_id}.json').json()['items']
414         has_audio = bool(audio_items)
415         can_sell_audio = has_audio and all(x['project']['can_sell'] for x in audio_items)
416     else:
417         has_audio = None
418         can_sell_audio = None
419         
420     return render(request, "documents/book_detail.html", {
421         "book": book,
422         "doc": doc,
423         "stats": stats,
424         "publishable": publishable,
425         "publishable_error": publish_error,
426         "form": form,
427         "publish_options_form": publish_options_form,
428         "editable": editable,
429         "has_audio": has_audio,
430         "can_sell_audio": can_sell_audio,
431     })
432
433
434 def image(request, slug):
435     image = get_object_or_404(Image, slug=slug)
436     if not image.accessible(request):
437         return HttpResponseForbidden("Not authorized.")
438
439     if request.user.has_perm('documents.change_image'):
440         if request.method == "POST":
441             form = forms.ImageForm(request.POST, instance=image)
442             if form.is_valid():
443                 form.save()
444                 return http.HttpResponseRedirect(image.get_absolute_url())
445         else:
446             form = forms.ImageForm(instance=image)
447         editable = True
448     else:
449         form = forms.ReadonlyImageForm(instance=image)
450         editable = False
451
452     publish_error = image.publishable_error()
453     publishable = publish_error is None
454
455     return render(request, "documents/image_detail.html", {
456         "object": image,
457         "publishable": publishable,
458         "publishable_error": publish_error,
459         "form": form,
460         "editable": editable,
461     })
462
463
464 @permission_required('documents.add_chunk')
465 def chunk_add(request, slug, chunk):
466     try:
467         doc = Chunk.get(slug, chunk)
468     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
469         raise Http404
470     if not doc.book.accessible(request):
471         return HttpResponseForbidden("Not authorized.")
472
473     if request.method == "POST":
474         form = forms.ChunkAddForm(request.POST, instance=doc)
475         if form.is_valid():
476             if request.user.is_authenticated:
477                 creator = request.user
478             else:
479                 creator = None
480             doc.split(creator=creator,
481                 slug=form.cleaned_data['slug'],
482                 title=form.cleaned_data['title'],
483                 gallery_start=form.cleaned_data['gallery_start'],
484                 user=form.cleaned_data['user'],
485                 stage=form.cleaned_data['stage']
486             )
487
488             return http.HttpResponseRedirect(doc.book.get_absolute_url())
489     else:
490         form = forms.ChunkAddForm(initial={
491                 "slug": str(doc.number + 1),
492                 "title": "cz. %d" % (doc.number + 1, ),
493         })
494
495     return render(request, "documents/chunk_add.html", {
496         "chunk": doc,
497         "form": form,
498     })
499
500
501 @login_required
502 def chunk_edit(request, slug, chunk):
503     try:
504         doc = Chunk.get(slug, chunk)
505     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
506         raise Http404
507     if not doc.book.accessible(request):
508         return HttpResponseForbidden("Not authorized.")
509
510     if request.method == "POST":
511         form = forms.ChunkForm(request.POST, instance=doc)
512         if form.is_valid():
513             form.save()
514             go_next = request.GET.get('next', None)
515             if go_next:
516                 go_next = quote_plus(unquote(iri_to_uri(go_next)), safe='/?=&')
517             else:
518                 go_next = doc.book.get_absolute_url()
519             return http.HttpResponseRedirect(go_next)
520     else:
521         form = forms.ChunkForm(instance=doc)
522
523     referer = request.META.get('HTTP_REFERER')
524     if referer:
525         parts = urlsplit(referer)
526         parts = ['', ''] + list(parts[2:])
527         go_next = quote_plus(urlunsplit(parts))
528     else:
529         go_next = ''
530
531     return render(request, "documents/chunk_edit.html", {
532         "chunk": doc,
533         "form": form,
534         "go_next": go_next,
535     })
536
537
538 @transaction.atomic
539 @login_required
540 @require_POST
541 def chunk_mass_edit(request):
542     ids = [int(i) for i in request.POST.get('ids').split(',') if i.strip()]
543     chunks = list(Chunk.objects.filter(id__in=ids))
544     
545     stage = request.POST.get('stage')
546     if stage:
547         try:
548             stage = Chunk.tag_model.objects.get(slug=stage)
549         except Chunk.DoesNotExist as e:
550             stage = None
551        
552         for c in chunks: c.stage = stage
553
554     username = request.POST.get('user')
555     logger.info("username: %s" % username)
556     logger.info(request.POST)
557     if username:
558         try:
559             user = User.objects.get(username=username)
560         except User.DoesNotExist as e:
561             user = None
562             
563         for c in chunks: c.user = user
564
565     project_id = request.POST.get('project')
566     if project_id:
567         try:
568             project = Project.objects.get(pk=int(project_id))
569         except (Project.DoesNotExist, ValueError) as e:
570             project = None
571         for c in chunks:
572             book = c.book
573             book.project = project
574             book.save()
575
576     for c in chunks: c.save()
577
578     return HttpResponse("", content_type="text/plain")
579
580
581 @transaction.atomic
582 @login_required
583 @require_POST
584 def image_mass_edit(request):
585     ids = map(int, filter(lambda i: i.strip()!='', request.POST.get('ids').split(',')))
586     images = map(lambda i: Image.objects.get(id=i), ids)
587     
588     stage = request.POST.get('stage')
589     if stage:
590         try:
591             stage = Image.tag_model.objects.get(slug=stage)
592         except Image.DoesNotExist as e:
593             stage = None
594        
595         for c in images: c.stage = stage
596
597     username = request.POST.get('user')
598     logger.info("username: %s" % username)
599     logger.info(request.POST)
600     if username:
601         try:
602             user = User.objects.get(username=username)
603         except User.DoesNotExist as e:
604             user = None
605             
606         for c in images: c.user = user
607
608     project_id = request.POST.get('project')
609     if project_id:
610         try:
611             project = Project.objects.get(pk=int(project_id))
612         except (Project.DoesNotExist, ValueError) as e:
613             project = None
614         for c in images:
615             c.project = project
616
617     for c in images: c.save()
618
619     return HttpResponse("", content_type="text/plain")
620
621
622 @permission_required('documents.change_book')
623 def book_append(request, slug):
624     book = get_object_or_404(Book, slug=slug)
625     if not book.accessible(request):
626         return HttpResponseForbidden("Not authorized.")
627
628     if request.method == "POST":
629         form = forms.BookAppendForm(book, request.POST)
630         if form.is_valid():
631             append_to = form.cleaned_data['append_to']
632             append_to.append(book)
633             return http.HttpResponseRedirect(append_to.get_absolute_url())
634     else:
635         form = forms.BookAppendForm(book)
636     return render(request, "documents/book_append_to.html", {
637         "book": book,
638         "form": form,
639
640         "logout_to": '/',
641     })
642
643
644 @require_POST
645 @login_required
646 def publish(request, slug):
647     form = forms.PublishOptionsForm(request.POST)
648     if form.is_valid():
649         days = form.cleaned_data['days']
650         beta = form.cleaned_data['beta']
651         hidden = form.cleaned_data['hidden']
652     else:
653         days = 0
654         beta = False
655         hidden = False
656     book = get_object_or_404(Book, slug=slug)
657     if not book.accessible(request):
658         return HttpResponseForbidden("Not authorized.")
659
660     try:
661         protocol = 'https://' if request.is_secure() else 'http://'
662         book.publish(request.user, host=protocol + request.get_host(), days=days, beta=beta, hidden=hidden)
663     except NotAuthorizedError:
664         return http.HttpResponseRedirect(reverse('apiclient_oauth' if not beta else 'apiclient_beta_oauth'))
665     except BaseException as e:
666         return http.HttpResponse(repr(e))
667     else:
668         return http.HttpResponseRedirect(book.get_absolute_url())
669
670
671 @require_POST
672 @login_required
673 def publish_image(request, slug):
674     image = get_object_or_404(Image, slug=slug)
675     if not image.accessible(request):
676         return HttpResponseForbidden("Not authorized.")
677
678     try:
679         image.publish(request.user)
680     except NotAuthorizedError:
681         return http.HttpResponseRedirect(reverse('apiclient_oauth'))
682     except BaseException as e:
683         return http.HttpResponse(e)
684     else:
685         return http.HttpResponseRedirect(image.get_absolute_url())
686
687
688 class GalleryView(UploadView):
689     def get_object(self, request, slug):
690         book = get_object_or_404(Book, slug=slug)
691         if not book.gallery:
692             raise Http404
693         return book
694
695     def breadcrumbs(self):
696         return [
697             (_('books'), reverse('documents_document_list')),
698             (self.object.title, self.object.get_absolute_url()),
699             (_('scan gallery'),),
700         ]
701
702     def get_directory(self):
703         return "%s%s/" % (settings.IMAGE_DIR, self.object.gallery)
704
705
706 def active_users_list(request, csv=False):
707     year = int(request.GET.get('y', date.today().year))
708     by_user = defaultdict(lambda: 0)
709     by_email = defaultdict(lambda: 0)
710     names_by_email = defaultdict(set)
711     for change_model in (Chunk.change_model, Image.change_model):
712         for c in change_model.objects.filter(
713                 created_at__year=year).order_by(
714                 'author', 'author_email', 'author_name').values(
715                 'author', 'author_name', 'author_email').annotate(
716                 c=Count('author'), ce=Count('author_email')).distinct():
717             if c['author']:
718                 by_user[c['author']] += c['c']
719             else:
720                 by_email[c['author_email']] += c['ce']
721                 if (c['author_name'] or '').strip():
722                     names_by_email[c['author_email']].add(c['author_name'])
723     for user in User.objects.filter(pk__in=by_user):
724         by_email[user.email] += by_user[user.pk]
725         names_by_email[user.email].add("%s %s" % (user.first_name, user.last_name))
726
727     active_users = []
728     for email, count in by_email.items():
729         active_users.append((email, names_by_email[email], count))
730     active_users.sort(key=lambda x: -x[2])
731     if csv:
732         return http.HttpResponse(
733             '\n'.join((
734                 ','.join(
735                     (str(x[2]), x[0], ','.join(x[1]))
736                 )
737                 for x in active_users
738             )),
739             content_type='text/csv',
740             headers={
741                 'Content-Disposition': f'attachment; filename=redakcja-{year}.csv',
742             }
743         )
744     else:
745         return render(request, 'documents/active_users_list.html', {
746             'users': active_users,
747             'year': year,
748         })
749
750
751 @user_passes_test(lambda u: u.is_superuser)
752 def mark_final(request):
753     if request.method == 'POST':
754         form = forms.MarkFinalForm(data=request.POST)
755         if form.is_valid():
756             form.save()
757             return HttpResponseRedirect(reverse('mark_final_completed'))
758     else:
759         form = forms.MarkFinalForm()
760     return render(request, 'documents/mark_final.html', {'form': form})
761
762
763 def mark_final_completed(request):
764     return render(request, 'documents/mark_final_completed.html')
765
766
767 def synchro(request, slug):
768     book = get_object_or_404(Book, slug=slug)
769     if not book.accessible(request):
770         return HttpResponseForbidden("Not authorized.")
771
772     document = book.wldocument(librarian2=True)
773     slug = document.meta.url.slug
774     print(f'https://audio.wolnelektury.pl/archive/book/{slug}.json')
775     error = None
776     try:
777         items = requests.get(f'https://audio.wolnelektury.pl/archive/book/{slug}.json').json()['items']
778     except:
779         error = 'Błąd połączenia z repozytorium audio.'
780         items = []
781     else:
782         mp3 = [
783             item['part'] for item in items
784         ]
785
786     split_on = (
787         'naglowek_rozdzial',
788         'naglowek_scena',
789         )
790     
791     if split_on:
792         documents = []
793         headers = [('Początek', 0, 0)]
794         present = True
795         n = 0
796         while present:
797             present = False
798             n += 1
799             newdoc = deepcopy(document)
800             newdoc.tree.getroot().document = newdoc
801             
802             master = newdoc.tree.getroot()[-1]
803             i = 0
804             for item in list(master):
805                 #chunkno, sourceline = 0, self.sourceline
806                 #if builder.splits:
807                 #    chunkno, sourceline = len(builder.splits), sourceline - builder.splits[-1]
808
809                 if 'forcesplit' in item.attrib or (item.tag in split_on and 'nosplit' not in item.attrib):
810                     # TODO: clear
811                     i += 1
812                     if n > 1 and i == n:
813                         headers.append((
814                             raw_printable_text(item),
815                             0,
816                             item.sourceline,
817                         ))
818                 if i != n and not (n == 1 and not i):
819                     master.remove(item)
820                 else:
821                     present = True
822                 if present:
823                     documents.append(newdoc)
824     else:
825         documents = [document]
826         headers = [(
827             document.meta.title, 0 ,0
828         )]
829
830     length_ok = len(headers) == len(mp3)
831     table = zip_longest(headers, mp3)
832
833     
834     return render(request, 'documents/synchro.html', {
835         'book': book,
836         'documents': documents,
837         'headers': headers,
838         'mp3': mp3,
839         'length_ok': length_ok,
840         'table': table,
841         'error': error,
842     })