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 datetime import datetime, date, timedelta
8 from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit
10 from django.conf import settings
11 from django.contrib import auth
12 from django.contrib.auth.models import User
13 from django.contrib.auth.decorators import login_required, permission_required
14 from django.urls import reverse
15 from django.db.models import Count, Q
16 from django.db import transaction
17 from django import http
18 from django.http import Http404, HttpResponse, HttpResponseForbidden
19 from django.http.response import HttpResponseRedirect
20 from django.shortcuts import get_object_or_404, render
21 from django.utils.encoding import iri_to_uri
22 from django.utils.translation import gettext_lazy as _
23 from django.views.decorators.http import require_POST
24 from django_cas_ng.decorators import user_passes_test
26 from apiclient import NotAuthorizedError
29 from .helpers import active_tab
30 from .models import (Book, Chunk, Image, BookPublishRecord,
31 ChunkPublishRecord, ImagePublishRecord, Project)
32 from fileupload.views import UploadView
35 # Quick hack around caching problems, TODO: use ETags
37 from django.views.decorators.cache import never_cache
39 logger = logging.getLogger("fnp.documents")
44 def document_list(request):
45 return render(request, 'documents/document_list.html')
50 def image_list(request, user=None):
51 return render(request, 'documents/image_list.html')
55 def user(request, username):
56 user = get_object_or_404(User, username=username)
57 return render(request, 'documents/user_page.html', {"viewed_user": user})
64 last_books = sorted(request.session.get("wiki_last_books", {}).items(),
65 key=lambda x: x[1]['time'], reverse=True)
66 for k, v in last_books:
67 v['time'] = datetime.fromtimestamp(v['time'])
68 return render(request, 'documents/my_page.html', {
69 'last_books': last_books,
76 return render(request, 'documents/user_list.html', {
77 'users': User.objects.all().annotate(count=Count('chunk')).order_by(
78 '-count', 'last_name', 'first_name'),
82 @active_tab('activity')
83 def activity(request, isodate=None):
86 day = helpers.parse_isodate(isodate)
93 next_day = day + timedelta(1)
94 prev_day = day - timedelta(1)
96 return render(request, 'documents/activity.html', locals())
100 def logout_then_redirect(request):
102 return http.HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
105 @permission_required('documents.add_book')
106 @active_tab('create')
107 def create_missing(request, slug=None):
110 slug = slug.replace(' ', '-')
112 if request.method == "POST":
113 form = forms.DocumentCreateForm(request.POST, request.FILES)
116 if request.user.is_authenticated:
117 creator = request.user
121 text=form.cleaned_data['text'],
123 slug=form.cleaned_data['slug'],
124 title=form.cleaned_data['title'],
125 gallery=form.cleaned_data['gallery'],
128 return http.HttpResponseRedirect(reverse("documents_book", args=[book.slug]))
130 form = forms.DocumentCreateForm(initial={
132 "title": slug.replace('-', ' ').title(),
136 return render(request, "documents/document_create_missing.html", {
144 @permission_required('documents.add_book')
145 @active_tab('upload')
147 if request.method == "POST":
148 form = forms.DocumentsUploadForm(request.POST, request.FILES)
150 from slugify import slugify
152 if request.user.is_authenticated:
153 creator = request.user
157 zip = form.cleaned_data['zip']
162 existing = [book.slug for book in Book.objects.all()]
163 for filename in zip.namelist():
164 if filename[-1] == '/':
166 title = os.path.basename(filename)[:-4]
167 slug = slugify(title)
168 if not (slug and filename.endswith('.xml')):
169 skipped_list.append(filename)
171 error_list.append((filename, slug, _('Slug already used for %s' % slugs[slug])))
172 elif slug in existing:
173 error_list.append((filename, slug, _('Slug already used in repository.')))
176 zip.read(filename).decode('utf-8') # test read
177 ok_list.append((filename, slug, title))
178 except UnicodeDecodeError:
179 error_list.append((filename, title, _('File should be UTF-8 encoded.')))
180 slugs[slug] = filename
183 for filename, slug, title in ok_list:
185 text=zip.read(filename).decode('utf-8'),
191 return render(request, "documents/document_upload.html", {
194 "skipped_list": skipped_list,
195 "error_list": error_list,
200 form = forms.DocumentsUploadForm()
202 return render(request, "documents/document_upload.html", {
209 def serve_xml(request, book, slug):
210 if not book.accessible(request):
211 return HttpResponseForbidden("Not authorized.")
212 xml = book.materialize(publishable=True)
213 response = http.HttpResponse(xml, content_type='application/xml')
214 response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
219 def book_xml(request, slug):
220 book = get_object_or_404(Book, slug=slug)
221 return serve_xml(request, book, slug)
225 def book_xml_dc(request, slug):
226 book = get_object_or_404(Book, catalogue_book_id=slug)
227 return serve_xml(request, book, slug)
231 def book_txt(request, slug):
232 book = get_object_or_404(Book, slug=slug)
233 if not book.accessible(request):
234 return HttpResponseForbidden("Not authorized.")
236 doc = book.wldocument()
237 text = doc.as_text().get_bytes()
238 response = http.HttpResponse(text, content_type='text/plain')
239 response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug
244 def book_html(request, slug):
245 book = get_object_or_404(Book, slug=slug)
246 if not book.accessible(request):
247 return HttpResponseForbidden("Not authorized.")
249 doc = book.wldocument(parse_dublincore=False)
250 html = doc.as_html(options={'gallery': "'%s'" % book.gallery_url()})
252 html = html.get_bytes().decode('utf-8') if html is not None else ''
253 # response = http.HttpResponse(html, content_type='text/html')
256 # for fragment in book.fragments.all().iterator():
257 # for theme in fragment.tags.filter(category='theme').iterator():
258 # book_themes.setdefault(theme, []).append(fragment)
260 # book_themes = book_themes.items()
261 # book_themes.sort(key=lambda s: s[0].sort_key)
262 return render(request, 'documents/book_text.html', locals())
266 def book_pdf(request, slug, mobile=False):
267 book = get_object_or_404(Book, slug=slug)
268 if not book.accessible(request):
269 return HttpResponseForbidden("Not authorized.")
271 # TODO: move to celery
272 doc = book.wldocument()
273 # TODO: error handling
274 customizations = ['26pt', 'nothemes', 'nomargins', 'notoc'] if mobile else None
275 pdf_file = doc.as_pdf(cover=True, base_url=request.build_absolute_uri(book.gallery_path()), customizations=customizations)
276 from .ebook_utils import serve_file
277 return serve_file(pdf_file.get_filename(),
278 book.slug + '.pdf', 'application/pdf')
282 def book_epub(request, slug):
283 book = get_object_or_404(Book, slug=slug)
284 if not book.accessible(request):
285 return HttpResponseForbidden("Not authorized.")
287 # TODO: move to celery
288 doc = book.wldocument()
289 # TODO: error handling
291 #### Problemas: images in children.
292 epub = doc.as_epub(base_url='file://' + book.gallery_path() + '/').get_bytes()
293 response = HttpResponse(content_type='application/epub+zip')
294 response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
300 def book_mobi(request, slug):
301 book = get_object_or_404(Book, slug=slug)
302 if not book.accessible(request):
303 return HttpResponseForbidden("Not authorized.")
305 # TODO: move to celery
306 doc = book.wldocument()
307 # TODO: error handling
308 mobi = doc.as_mobi(base_url='file://' + book.gallery_path() + '/').get_bytes()
309 response = HttpResponse(content_type='application/x-mobipocket-ebook')
310 response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.mobi'
316 def revision(request, slug, chunk=None):
318 doc = Chunk.get(slug, chunk)
319 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
321 if not doc.book.accessible(request):
322 return HttpResponseForbidden("Not authorized.")
323 return http.HttpResponse(str(doc.revision()))
326 def book(request, slug):
327 book = get_object_or_404(Book, slug=slug)
328 if not book.accessible(request):
329 return HttpResponseForbidden("Not authorized.")
331 if request.user.has_perm('documents.change_book'):
332 if request.method == "POST":
333 form = forms.BookForm(request.POST, instance=book)
336 return http.HttpResponseRedirect(book.get_absolute_url())
338 form = forms.BookForm(instance=book)
339 publish_options_form = forms.PublishOptionsForm()
342 form = forms.ReadonlyBookForm(instance=book)
343 publish_options_form = forms.PublishOptionsForm()
346 publish_error = book.publishable_error()
347 publishable = publish_error is None
350 doc = book.wldocument()
354 return render(request, "documents/book_detail.html", {
357 "publishable": publishable,
358 "publishable_error": publish_error,
360 "publish_options_form": publish_options_form,
361 "editable": editable,
365 def image(request, slug):
366 image = get_object_or_404(Image, slug=slug)
367 if not image.accessible(request):
368 return HttpResponseForbidden("Not authorized.")
370 if request.user.has_perm('documents.change_image'):
371 if request.method == "POST":
372 form = forms.ImageForm(request.POST, instance=image)
375 return http.HttpResponseRedirect(image.get_absolute_url())
377 form = forms.ImageForm(instance=image)
380 form = forms.ReadonlyImageForm(instance=image)
383 publish_error = image.publishable_error()
384 publishable = publish_error is None
386 return render(request, "documents/image_detail.html", {
388 "publishable": publishable,
389 "publishable_error": publish_error,
391 "editable": editable,
395 @permission_required('documents.add_chunk')
396 def chunk_add(request, slug, chunk):
398 doc = Chunk.get(slug, chunk)
399 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
401 if not doc.book.accessible(request):
402 return HttpResponseForbidden("Not authorized.")
404 if request.method == "POST":
405 form = forms.ChunkAddForm(request.POST, instance=doc)
407 if request.user.is_authenticated:
408 creator = request.user
411 doc.split(creator=creator,
412 slug=form.cleaned_data['slug'],
413 title=form.cleaned_data['title'],
414 gallery_start=form.cleaned_data['gallery_start'],
415 user=form.cleaned_data['user'],
416 stage=form.cleaned_data['stage']
419 return http.HttpResponseRedirect(doc.book.get_absolute_url())
421 form = forms.ChunkAddForm(initial={
422 "slug": str(doc.number + 1),
423 "title": "cz. %d" % (doc.number + 1, ),
426 return render(request, "documents/chunk_add.html", {
433 def chunk_edit(request, slug, chunk):
435 doc = Chunk.get(slug, chunk)
436 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
438 if not doc.book.accessible(request):
439 return HttpResponseForbidden("Not authorized.")
441 if request.method == "POST":
442 form = forms.ChunkForm(request.POST, instance=doc)
445 go_next = request.GET.get('next', None)
447 go_next = urlquote_plus(unquote(iri_to_uri(go_next)), safe='/?=&')
449 go_next = doc.book.get_absolute_url()
450 return http.HttpResponseRedirect(go_next)
452 form = forms.ChunkForm(instance=doc)
454 referer = request.META.get('HTTP_REFERER')
456 parts = urlsplit(referer)
457 parts = ['', ''] + list(parts[2:])
458 go_next = urlquote_plus(urlunsplit(parts))
462 return render(request, "documents/chunk_edit.html", {
472 def chunk_mass_edit(request):
473 ids = [int(i) for i in request.POST.get('ids').split(',') if i.strip()]
474 chunks = list(Chunk.objects.filter(id__in=ids))
476 stage = request.POST.get('stage')
479 stage = Chunk.tag_model.objects.get(slug=stage)
480 except Chunk.DoesNotExist as e:
483 for c in chunks: c.stage = stage
485 username = request.POST.get('user')
486 logger.info("username: %s" % username)
487 logger.info(request.POST)
490 user = User.objects.get(username=username)
491 except User.DoesNotExist as e:
494 for c in chunks: c.user = user
496 project_id = request.POST.get('project')
499 project = Project.objects.get(pk=int(project_id))
500 except (Project.DoesNotExist, ValueError) as e:
504 book.project = project
507 for c in chunks: c.save()
509 return HttpResponse("", content_type="text/plain")
515 def image_mass_edit(request):
516 ids = map(int, filter(lambda i: i.strip()!='', request.POST.get('ids').split(',')))
517 images = map(lambda i: Image.objects.get(id=i), ids)
519 stage = request.POST.get('stage')
522 stage = Image.tag_model.objects.get(slug=stage)
523 except Image.DoesNotExist as e:
526 for c in images: c.stage = stage
528 username = request.POST.get('user')
529 logger.info("username: %s" % username)
530 logger.info(request.POST)
533 user = User.objects.get(username=username)
534 except User.DoesNotExist as e:
537 for c in images: c.user = user
539 project_id = request.POST.get('project')
542 project = Project.objects.get(pk=int(project_id))
543 except (Project.DoesNotExist, ValueError) as e:
548 for c in images: c.save()
550 return HttpResponse("", content_type="text/plain")
553 @permission_required('documents.change_book')
554 def book_append(request, slug):
555 book = get_object_or_404(Book, slug=slug)
556 if not book.accessible(request):
557 return HttpResponseForbidden("Not authorized.")
559 if request.method == "POST":
560 form = forms.BookAppendForm(book, request.POST)
562 append_to = form.cleaned_data['append_to']
563 append_to.append(book)
564 return http.HttpResponseRedirect(append_to.get_absolute_url())
566 form = forms.BookAppendForm(book)
567 return render(request, "documents/book_append_to.html", {
577 def publish(request, slug):
578 form = forms.PublishOptionsForm(request.POST)
580 days = form.cleaned_data['days']
581 beta = form.cleaned_data['beta']
582 hidden = form.cleaned_data['hidden']
587 book = get_object_or_404(Book, slug=slug)
588 if not book.accessible(request):
589 return HttpResponseForbidden("Not authorized.")
592 protocol = 'https://' if request.is_secure() else 'http://'
593 book.publish(request.user, host=protocol + request.get_host(), days=days, beta=beta, hidden=hidden)
594 except NotAuthorizedError:
595 return http.HttpResponseRedirect(reverse('apiclient_oauth' if not beta else 'apiclient_beta_oauth'))
596 except BaseException as e:
597 return http.HttpResponse(repr(e))
599 return http.HttpResponseRedirect(book.get_absolute_url())
604 def publish_image(request, slug):
605 image = get_object_or_404(Image, slug=slug)
606 if not image.accessible(request):
607 return HttpResponseForbidden("Not authorized.")
610 image.publish(request.user)
611 except NotAuthorizedError:
612 return http.HttpResponseRedirect(reverse('apiclient_oauth'))
613 except BaseException as e:
614 return http.HttpResponse(e)
616 return http.HttpResponseRedirect(image.get_absolute_url())
619 class GalleryView(UploadView):
620 def get_object(self, request, slug):
621 book = get_object_or_404(Book, slug=slug)
626 def breadcrumbs(self):
628 (_('books'), reverse('documents_document_list')),
629 (self.object.title, self.object.get_absolute_url()),
630 (_('scan gallery'),),
633 def get_directory(self):
634 return "%s%s/" % (settings.IMAGE_DIR, self.object.gallery)
637 def active_users_list(request, csv=False):
638 year = int(request.GET.get('y', date.today().year))
639 by_user = defaultdict(lambda: 0)
640 by_email = defaultdict(lambda: 0)
641 names_by_email = defaultdict(set)
642 for change_model in (Chunk.change_model, Image.change_model):
643 for c in change_model.objects.filter(
644 created_at__year=year).order_by(
645 'author', 'author_email', 'author_name').values(
646 'author', 'author_name', 'author_email').annotate(
647 c=Count('author'), ce=Count('author_email')).distinct():
649 by_user[c['author']] += c['c']
651 by_email[c['author_email']] += c['ce']
652 if (c['author_name'] or '').strip():
653 names_by_email[c['author_email']].add(c['author_name'])
654 for user in User.objects.filter(pk__in=by_user):
655 by_email[user.email] += by_user[user.pk]
656 names_by_email[user.email].add("%s %s" % (user.first_name, user.last_name))
659 for email, count in by_email.items():
660 active_users.append((email, names_by_email[email], count))
661 active_users.sort(key=lambda x: -x[2])
663 return http.HttpResponse(
666 (str(x[2]), x[0], ','.join(x[1]))
668 for x in active_users
670 content_type='text/csv',
672 'Content-Disposition': f'attachment; filename=redakcja-{year}.csv',
676 return render(request, 'documents/active_users_list.html', {
677 'users': active_users,
682 @user_passes_test(lambda u: u.is_superuser)
683 def mark_final(request):
684 if request.method == 'POST':
685 form = forms.MarkFinalForm(data=request.POST)
688 return HttpResponseRedirect(reverse('mark_final_completed'))
690 form = forms.MarkFinalForm()
691 return render(request, 'documents/mark_final.html', {'form': form})
694 def mark_final_completed(request):
695 return render(request, 'documents/mark_final_completed.html')