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.
4 from collections import defaultdict
5 from copy import deepcopy
6 from datetime import datetime, date, timedelta
7 from itertools import zip_longest
10 from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit
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
29 from librarian import epubcheck
30 from librarian.html import raw_printable_text
32 from apiclient import api_call, NotAuthorizedError
35 from .helpers import active_tab
36 from .models import (Book, Chunk, Image, BookPublishRecord,
37 ChunkPublishRecord, ImagePublishRecord, Project)
38 from fileupload.views import UploadView
41 # Quick hack around caching problems, TODO: use ETags
43 from django.views.decorators.cache import never_cache
45 logger = logging.getLogger("fnp.documents")
50 def document_list(request):
51 return render(request, 'documents/document_list.html')
56 def image_list(request, user=None):
57 return render(request, 'documents/image_list.html')
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})
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'])
75 resp = api_call(request.user, 'username/')
76 except NotAuthorizedError:
79 wllogin = resp['username']
81 return render(request, 'documents/my_page.html', {
82 'last_books': last_books,
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'),
96 @active_tab('activity')
97 def activity(request, isodate=None):
100 day = helpers.parse_isodate(isodate)
107 next_day = day + timedelta(1)
108 prev_day = day - timedelta(1)
110 return render(request, 'documents/activity.html', locals())
114 def logout_then_redirect(request):
116 return http.HttpResponseRedirect(quote_plus(request.GET.get('next', '/'), safe='/?='))
119 @permission_required('documents.add_book')
120 @active_tab('create')
121 def create_missing(request, slug=None):
124 slug = slug.replace(' ', '-')
126 if request.method == "POST":
127 form = forms.DocumentCreateForm(request.POST, request.FILES)
130 if request.user.is_authenticated:
131 creator = request.user
135 text=form.cleaned_data['text'],
137 slug=form.cleaned_data['slug'],
138 title=form.cleaned_data['title'],
139 gallery=form.cleaned_data['gallery'],
142 return http.HttpResponseRedirect(reverse("documents_book", args=[book.slug]))
144 form = forms.DocumentCreateForm(initial={
146 "title": slug.replace('-', ' ').title(),
150 return render(request, "documents/document_create_missing.html", {
158 @permission_required('documents.add_book')
159 @active_tab('upload')
161 if request.method == "POST":
162 form = forms.DocumentsUploadForm(request.POST, request.FILES)
164 from slugify import slugify
166 if request.user.is_authenticated:
167 creator = request.user
171 zip = form.cleaned_data['zip']
176 existing = [book.slug for book in Book.objects.all()]
177 for filename in zip.namelist():
178 if filename[-1] == '/':
180 title = os.path.basename(filename)[:-4]
181 slug = slugify(title)
182 if not (slug and filename.endswith('.xml')):
183 skipped_list.append(filename)
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.')))
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
197 for filename, slug, title in ok_list:
199 text=zip.read(filename).decode('utf-8'),
205 return render(request, "documents/document_upload.html", {
208 "skipped_list": skipped_list,
209 "error_list": error_list,
214 form = forms.DocumentsUploadForm()
216 return render(request, "documents/document_upload.html", {
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
233 def book_xml(request, slug):
234 book = get_object_or_404(Book, slug=slug)
235 return serve_xml(request, book, slug)
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)
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.")
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
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.")
263 doc = book.wldocument(parse_dublincore=False)
264 html = doc.as_html(options={'gallery': "'%s'" % book.gallery_url()})
266 html = html.get_bytes().decode('utf-8') if html is not None else ''
267 # response = http.HttpResponse(html, content_type='text/html')
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)
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())
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.")
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')
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.")
303 # TODO: move to celery
304 doc = book.wldocument(librarian2=True)
305 # TODO: error handling
307 from librarian.builders import EpubBuilder
309 base_url='file://' + book.gallery_path() + '/',
311 ).build(doc).get_bytes()
312 response = HttpResponse(content_type='application/epub+zip')
313 response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
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.")
325 # TODO: move to celery
326 doc = book.wldocument(librarian2=True)
327 # TODO: error handling
329 from librarian.builders import EpubBuilder
331 base_url='file://' + book.gallery_path() + '/',
334 fname = epub.get_filename()
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,
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.")
354 # TODO: move to celery
355 doc = book.wldocument(librarian2=True)
356 # TODO: error handling
357 from librarian.builders import 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'
368 def revision(request, slug, chunk=None):
370 doc = Chunk.get(slug, chunk)
371 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
373 if not doc.book.accessible(request):
374 return HttpResponseForbidden("Not authorized.")
375 return http.HttpResponse(str(doc.revision()))
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.")
383 if request.user.has_perm('documents.change_book'):
384 if request.method == "POST":
385 form = forms.BookForm(request.POST, instance=book)
388 return http.HttpResponseRedirect(book.get_absolute_url())
390 form = forms.BookForm(instance=book)
391 publish_options_form = forms.PublishOptionsForm()
394 form = forms.ReadonlyBookForm(instance=book)
395 publish_options_form = forms.PublishOptionsForm()
398 publish_error = book.publishable_error()
399 publishable = publish_error is None
403 doc = book.wldocument(librarian2=True)
408 stats = doc.get_statistics()
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)
418 can_sell_audio = None
420 return render(request, "documents/book_detail.html", {
424 "publishable": publishable,
425 "publishable_error": publish_error,
427 "publish_options_form": publish_options_form,
428 "editable": editable,
429 "has_audio": has_audio,
430 "can_sell_audio": can_sell_audio,
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.")
439 if request.user.has_perm('documents.change_image'):
440 if request.method == "POST":
441 form = forms.ImageForm(request.POST, instance=image)
444 return http.HttpResponseRedirect(image.get_absolute_url())
446 form = forms.ImageForm(instance=image)
449 form = forms.ReadonlyImageForm(instance=image)
452 publish_error = image.publishable_error()
453 publishable = publish_error is None
455 return render(request, "documents/image_detail.html", {
457 "publishable": publishable,
458 "publishable_error": publish_error,
460 "editable": editable,
464 @permission_required('documents.add_chunk')
465 def chunk_add(request, slug, chunk):
467 doc = Chunk.get(slug, chunk)
468 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
470 if not doc.book.accessible(request):
471 return HttpResponseForbidden("Not authorized.")
473 if request.method == "POST":
474 form = forms.ChunkAddForm(request.POST, instance=doc)
476 if request.user.is_authenticated:
477 creator = request.user
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']
488 return http.HttpResponseRedirect(doc.book.get_absolute_url())
490 form = forms.ChunkAddForm(initial={
491 "slug": str(doc.number + 1),
492 "title": "cz. %d" % (doc.number + 1, ),
495 return render(request, "documents/chunk_add.html", {
502 def chunk_edit(request, slug, chunk):
504 doc = Chunk.get(slug, chunk)
505 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
507 if not doc.book.accessible(request):
508 return HttpResponseForbidden("Not authorized.")
510 if request.method == "POST":
511 form = forms.ChunkForm(request.POST, instance=doc)
514 go_next = request.GET.get('next', None)
516 go_next = quote_plus(unquote(iri_to_uri(go_next)), safe='/?=&')
518 go_next = doc.book.get_absolute_url()
519 return http.HttpResponseRedirect(go_next)
521 form = forms.ChunkForm(instance=doc)
523 referer = request.META.get('HTTP_REFERER')
525 parts = urlsplit(referer)
526 parts = ['', ''] + list(parts[2:])
527 go_next = quote_plus(urlunsplit(parts))
531 return render(request, "documents/chunk_edit.html", {
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))
545 stage = request.POST.get('stage')
548 stage = Chunk.tag_model.objects.get(slug=stage)
549 except Chunk.DoesNotExist as e:
552 for c in chunks: c.stage = stage
554 username = request.POST.get('user')
555 logger.info("username: %s" % username)
556 logger.info(request.POST)
559 user = User.objects.get(username=username)
560 except User.DoesNotExist as e:
563 for c in chunks: c.user = user
565 project_id = request.POST.get('project')
568 project = Project.objects.get(pk=int(project_id))
569 except (Project.DoesNotExist, ValueError) as e:
573 book.project = project
576 for c in chunks: c.save()
578 return HttpResponse("", content_type="text/plain")
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)
588 stage = request.POST.get('stage')
591 stage = Image.tag_model.objects.get(slug=stage)
592 except Image.DoesNotExist as e:
595 for c in images: c.stage = stage
597 username = request.POST.get('user')
598 logger.info("username: %s" % username)
599 logger.info(request.POST)
602 user = User.objects.get(username=username)
603 except User.DoesNotExist as e:
606 for c in images: c.user = user
608 project_id = request.POST.get('project')
611 project = Project.objects.get(pk=int(project_id))
612 except (Project.DoesNotExist, ValueError) as e:
617 for c in images: c.save()
619 return HttpResponse("", content_type="text/plain")
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.")
628 if request.method == "POST":
629 form = forms.BookAppendForm(book, request.POST)
631 append_to = form.cleaned_data['append_to']
632 append_to.append(book)
633 return http.HttpResponseRedirect(append_to.get_absolute_url())
635 form = forms.BookAppendForm(book)
636 return render(request, "documents/book_append_to.html", {
646 def publish(request, slug):
647 form = forms.PublishOptionsForm(request.POST)
649 days = form.cleaned_data['days']
650 beta = form.cleaned_data['beta']
651 hidden = form.cleaned_data['hidden']
656 book = get_object_or_404(Book, slug=slug)
657 if not book.accessible(request):
658 return HttpResponseForbidden("Not authorized.")
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))
668 return http.HttpResponseRedirect(book.get_absolute_url())
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.")
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)
685 return http.HttpResponseRedirect(image.get_absolute_url())
688 class GalleryView(UploadView):
689 def get_object(self, request, slug):
690 book = get_object_or_404(Book, slug=slug)
695 def breadcrumbs(self):
697 (_('books'), reverse('documents_document_list')),
698 (self.object.title, self.object.get_absolute_url()),
699 (_('scan gallery'),),
702 def get_directory(self):
703 return "%s%s/" % (settings.IMAGE_DIR, self.object.gallery)
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():
718 by_user[c['author']] += c['c']
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))
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])
732 return http.HttpResponse(
735 (str(x[2]), x[0], ','.join(x[1]))
737 for x in active_users
739 content_type='text/csv',
741 'Content-Disposition': f'attachment; filename=redakcja-{year}.csv',
745 return render(request, 'documents/active_users_list.html', {
746 'users': active_users,
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)
757 return HttpResponseRedirect(reverse('mark_final_completed'))
759 form = forms.MarkFinalForm()
760 return render(request, 'documents/mark_final.html', {'form': form})
763 def mark_final_completed(request):
764 return render(request, 'documents/mark_final_completed.html')
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.")
772 document = book.wldocument(librarian2=True)
773 slug = document.meta.url.slug
774 print(f'https://audio.wolnelektury.pl/archive/book/{slug}.json')
777 items = requests.get(f'https://audio.wolnelektury.pl/archive/book/{slug}.json').json()['items']
779 error = 'Błąd połączenia z repozytorium audio.'
783 item['part'] for item in items
793 headers = [('Początek', 0, 0)]
799 newdoc = deepcopy(document)
800 newdoc.tree.getroot().document = newdoc
802 master = newdoc.tree.getroot()[-1]
804 for item in list(master):
805 #chunkno, sourceline = 0, self.sourceline
807 # chunkno, sourceline = len(builder.splits), sourceline - builder.splits[-1]
809 if 'forcesplit' in item.attrib or (item.tag in split_on and 'nosplit' not in item.attrib):
814 raw_printable_text(item),
818 if i != n and not (n == 1 and not i):
823 documents.append(newdoc)
825 documents = [document]
827 document.meta.title, 0 ,0
830 length_ok = len(headers) == len(mp3)
831 table = zip_longest(headers, mp3)
834 return render(request, 'documents/synchro.html', {
836 'documents': documents,
839 'length_ok': length_ok,