a930b8dc19323fc7fd3fea925fbd4faf3ceca9cb
[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 datetime import datetime, date, timedelta
6 import logging
7 import os
8 from urllib.parse import quote_plus, unquote, urlsplit, urlunsplit
9
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
25
26 from apiclient import api_call, NotAuthorizedError
27 from . import forms
28 from . import helpers
29 from .helpers import active_tab
30 from .models import (Book, Chunk, Image, BookPublishRecord, 
31         ChunkPublishRecord, ImagePublishRecord, Project)
32 from fileupload.views import UploadView
33
34 #
35 # Quick hack around caching problems, TODO: use ETags
36 #
37 from django.views.decorators.cache import never_cache
38
39 logger = logging.getLogger("fnp.documents")
40
41
42 @active_tab('all')
43 @never_cache
44 def document_list(request):
45     return render(request, 'documents/document_list.html')
46
47
48 @active_tab('images')
49 @never_cache
50 def image_list(request, user=None):
51     return render(request, 'documents/image_list.html')
52
53
54 @never_cache
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})
58
59
60 @login_required
61 @active_tab('my')
62 @never_cache
63 def my(request):
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     try:
69         resp = api_call(request.user, 'username/')
70     except NotAuthorizedError:
71         wllogin = None
72     else:
73         wllogin = resp['username']
74
75     return render(request, 'documents/my_page.html', {
76         'last_books': last_books,
77         "logout_to": '/',
78         "wllogin": wllogin,
79         })
80
81
82 @active_tab('users')
83 def users(request):
84     return render(request, 'documents/user_list.html', {
85         'users': User.objects.all().annotate(count=Count('chunk')).order_by(
86             '-count', 'last_name', 'first_name'),
87     })
88
89
90 @active_tab('activity')
91 def activity(request, isodate=None):
92     today = date.today()
93     try:
94         day = helpers.parse_isodate(isodate)
95     except ValueError:
96         day = today
97
98     if day > today:
99         raise Http404
100     if day != today:
101         next_day = day + timedelta(1)
102     prev_day = day - timedelta(1)
103
104     return render(request, 'documents/activity.html', locals())
105
106
107 @never_cache
108 def logout_then_redirect(request):
109     auth.logout(request)
110     return http.HttpResponseRedirect(quote_plus(request.GET.get('next', '/'), safe='/?='))
111
112
113 @permission_required('documents.add_book')
114 @active_tab('create')
115 def create_missing(request, slug=None):
116     if slug is None:
117         slug = ''
118     slug = slug.replace(' ', '-')
119
120     if request.method == "POST":
121         form = forms.DocumentCreateForm(request.POST, request.FILES)
122         if form.is_valid():
123             
124             if request.user.is_authenticated:
125                 creator = request.user
126             else:
127                 creator = None
128             book = Book.create(
129                 text=form.cleaned_data['text'],
130                 creator=creator,
131                 slug=form.cleaned_data['slug'],
132                 title=form.cleaned_data['title'],
133                 gallery=form.cleaned_data['gallery'],
134             )
135
136             return http.HttpResponseRedirect(reverse("documents_book", args=[book.slug]))
137     else:
138         form = forms.DocumentCreateForm(initial={
139                 "slug": slug,
140                 "title": slug.replace('-', ' ').title(),
141                 "gallery": slug,
142         })
143
144     return render(request, "documents/document_create_missing.html", {
145         "slug": slug,
146         "form": form,
147
148         "logout_to": '/',
149     })
150
151
152 @permission_required('documents.add_book')
153 @active_tab('upload')
154 def upload(request):
155     if request.method == "POST":
156         form = forms.DocumentsUploadForm(request.POST, request.FILES)
157         if form.is_valid():
158             from slugify import slugify
159
160             if request.user.is_authenticated:
161                 creator = request.user
162             else:
163                 creator = None
164
165             zip = form.cleaned_data['zip']
166             skipped_list = []
167             ok_list = []
168             error_list = []
169             slugs = {}
170             existing = [book.slug for book in Book.objects.all()]
171             for filename in zip.namelist():
172                 if filename[-1] == '/':
173                     continue
174                 title = os.path.basename(filename)[:-4]
175                 slug = slugify(title)
176                 if not (slug and filename.endswith('.xml')):
177                     skipped_list.append(filename)
178                 elif slug in slugs:
179                     error_list.append((filename, slug, _('Slug already used for %s' % slugs[slug])))
180                 elif slug in existing:
181                     error_list.append((filename, slug, _('Slug already used in repository.')))
182                 else:
183                     try:
184                         zip.read(filename).decode('utf-8') # test read
185                         ok_list.append((filename, slug, title))
186                     except UnicodeDecodeError:
187                         error_list.append((filename, title, _('File should be UTF-8 encoded.')))
188                     slugs[slug] = filename
189
190             if not error_list:
191                 for filename, slug, title in ok_list:
192                     book = Book.create(
193                         text=zip.read(filename).decode('utf-8'),
194                         creator=creator,
195                         slug=slug,
196                         title=title,
197                     )
198
199             return render(request, "documents/document_upload.html", {
200                 "form": form,
201                 "ok_list": ok_list,
202                 "skipped_list": skipped_list,
203                 "error_list": error_list,
204
205                 "logout_to": '/',
206             })
207     else:
208         form = forms.DocumentsUploadForm()
209
210     return render(request, "documents/document_upload.html", {
211         "form": form,
212
213         "logout_to": '/',
214     })
215
216
217 def serve_xml(request, book, slug):
218     if not book.accessible(request):
219         return HttpResponseForbidden("Not authorized.")
220     xml = book.materialize(publishable=True)
221     response = http.HttpResponse(xml, content_type='application/xml')
222     response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
223     return response
224
225
226 @never_cache
227 def book_xml(request, slug):
228     book = get_object_or_404(Book, slug=slug)
229     return serve_xml(request, book, slug)
230
231
232 @never_cache
233 def book_xml_dc(request, slug):
234     book = get_object_or_404(Book, catalogue_book_id=slug)
235     return serve_xml(request, book, slug)
236
237
238 @never_cache
239 def book_txt(request, slug):
240     book = get_object_or_404(Book, slug=slug)
241     if not book.accessible(request):
242         return HttpResponseForbidden("Not authorized.")
243
244     doc = book.wldocument()
245     text = doc.as_text().get_bytes()
246     response = http.HttpResponse(text, content_type='text/plain')
247     response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug
248     return response
249
250
251 @never_cache
252 def book_html(request, slug):
253     book = get_object_or_404(Book, slug=slug)
254     if not book.accessible(request):
255         return HttpResponseForbidden("Not authorized.")
256
257     doc = book.wldocument(parse_dublincore=False)
258     html = doc.as_html(options={'gallery': "'%s'" % book.gallery_url()})
259
260     html = html.get_bytes().decode('utf-8') if html is not None else ''
261     # response = http.HttpResponse(html, content_type='text/html')
262     # return response
263     # book_themes = {}
264     # for fragment in book.fragments.all().iterator():
265     #     for theme in fragment.tags.filter(category='theme').iterator():
266     #         book_themes.setdefault(theme, []).append(fragment)
267
268     # book_themes = book_themes.items()
269     # book_themes.sort(key=lambda s: s[0].sort_key)
270     return render(request, 'documents/book_text.html', locals())
271
272
273 @login_required
274 @never_cache
275 def book_pdf(request, slug, mobile=False):
276     book = get_object_or_404(Book, slug=slug)
277     if not book.accessible(request):
278         return HttpResponseForbidden("Not authorized.")
279
280     # TODO: move to celery
281     doc = book.wldocument()
282     # TODO: error handling
283     customizations = ['26pt', 'nothemes', 'nomargins', 'notoc'] if mobile else None
284     pdf_file = doc.as_pdf(cover=True, base_url=request.build_absolute_uri(book.gallery_path()), customizations=customizations)
285     from .ebook_utils import serve_file
286     return serve_file(pdf_file.get_filename(),
287                 book.slug + '.pdf', 'application/pdf')
288
289
290 @login_required
291 @never_cache
292 def book_epub(request, slug):
293     book = get_object_or_404(Book, slug=slug)
294     if not book.accessible(request):
295         return HttpResponseForbidden("Not authorized.")
296
297     # TODO: move to celery
298     doc = book.wldocument()
299     # TODO: error handling
300
301     #### Problemas: images in children.
302     epub = doc.as_epub(base_url='file://' + book.gallery_path() + '/').get_bytes()
303     response = HttpResponse(content_type='application/epub+zip')
304     response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
305     response.write(epub)
306     return response
307
308
309 @login_required
310 @never_cache
311 def book_mobi(request, slug):
312     book = get_object_or_404(Book, slug=slug)
313     if not book.accessible(request):
314         return HttpResponseForbidden("Not authorized.")
315
316     # TODO: move to celery
317     doc = book.wldocument()
318     # TODO: error handling
319     mobi = doc.as_mobi(base_url='file://' + book.gallery_path() + '/').get_bytes()
320     response = HttpResponse(content_type='application/x-mobipocket-ebook')
321     response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.mobi'
322     response.write(mobi)
323     return response
324
325
326 @never_cache
327 def revision(request, slug, chunk=None):
328     try:
329         doc = Chunk.get(slug, chunk)
330     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
331         raise Http404
332     if not doc.book.accessible(request):
333         return HttpResponseForbidden("Not authorized.")
334     return http.HttpResponse(str(doc.revision()))
335
336
337 def book(request, slug):
338     book = get_object_or_404(Book, slug=slug)
339     if not book.accessible(request):
340         return HttpResponseForbidden("Not authorized.")
341
342     if request.user.has_perm('documents.change_book'):
343         if request.method == "POST":
344             form = forms.BookForm(request.POST, instance=book)
345             if form.is_valid():
346                 form.save()
347                 return http.HttpResponseRedirect(book.get_absolute_url())
348         else:
349             form = forms.BookForm(instance=book)
350         publish_options_form = forms.PublishOptionsForm()
351         editable = True
352     else:
353         form = forms.ReadonlyBookForm(instance=book)
354         publish_options_form = forms.PublishOptionsForm()
355         editable = False
356
357     publish_error = book.publishable_error()
358     publishable = publish_error is None
359
360     try:
361         doc = book.wldocument()
362     except:
363         doc = None
364     
365     return render(request, "documents/book_detail.html", {
366         "book": book,
367         "doc": doc,
368         "publishable": publishable,
369         "publishable_error": publish_error,
370         "form": form,
371         "publish_options_form": publish_options_form,
372         "editable": editable,
373     })
374
375
376 def image(request, slug):
377     image = get_object_or_404(Image, slug=slug)
378     if not image.accessible(request):
379         return HttpResponseForbidden("Not authorized.")
380
381     if request.user.has_perm('documents.change_image'):
382         if request.method == "POST":
383             form = forms.ImageForm(request.POST, instance=image)
384             if form.is_valid():
385                 form.save()
386                 return http.HttpResponseRedirect(image.get_absolute_url())
387         else:
388             form = forms.ImageForm(instance=image)
389         editable = True
390     else:
391         form = forms.ReadonlyImageForm(instance=image)
392         editable = False
393
394     publish_error = image.publishable_error()
395     publishable = publish_error is None
396
397     return render(request, "documents/image_detail.html", {
398         "object": image,
399         "publishable": publishable,
400         "publishable_error": publish_error,
401         "form": form,
402         "editable": editable,
403     })
404
405
406 @permission_required('documents.add_chunk')
407 def chunk_add(request, slug, chunk):
408     try:
409         doc = Chunk.get(slug, chunk)
410     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
411         raise Http404
412     if not doc.book.accessible(request):
413         return HttpResponseForbidden("Not authorized.")
414
415     if request.method == "POST":
416         form = forms.ChunkAddForm(request.POST, instance=doc)
417         if form.is_valid():
418             if request.user.is_authenticated:
419                 creator = request.user
420             else:
421                 creator = None
422             doc.split(creator=creator,
423                 slug=form.cleaned_data['slug'],
424                 title=form.cleaned_data['title'],
425                 gallery_start=form.cleaned_data['gallery_start'],
426                 user=form.cleaned_data['user'],
427                 stage=form.cleaned_data['stage']
428             )
429
430             return http.HttpResponseRedirect(doc.book.get_absolute_url())
431     else:
432         form = forms.ChunkAddForm(initial={
433                 "slug": str(doc.number + 1),
434                 "title": "cz. %d" % (doc.number + 1, ),
435         })
436
437     return render(request, "documents/chunk_add.html", {
438         "chunk": doc,
439         "form": form,
440     })
441
442
443 @login_required
444 def chunk_edit(request, slug, chunk):
445     try:
446         doc = Chunk.get(slug, chunk)
447     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
448         raise Http404
449     if not doc.book.accessible(request):
450         return HttpResponseForbidden("Not authorized.")
451
452     if request.method == "POST":
453         form = forms.ChunkForm(request.POST, instance=doc)
454         if form.is_valid():
455             form.save()
456             go_next = request.GET.get('next', None)
457             if go_next:
458                 go_next = quote_plus(unquote(iri_to_uri(go_next)), safe='/?=&')
459             else:
460                 go_next = doc.book.get_absolute_url()
461             return http.HttpResponseRedirect(go_next)
462     else:
463         form = forms.ChunkForm(instance=doc)
464
465     referer = request.META.get('HTTP_REFERER')
466     if referer:
467         parts = urlsplit(referer)
468         parts = ['', ''] + list(parts[2:])
469         go_next = quote_plus(urlunsplit(parts))
470     else:
471         go_next = ''
472
473     return render(request, "documents/chunk_edit.html", {
474         "chunk": doc,
475         "form": form,
476         "go_next": go_next,
477     })
478
479
480 @transaction.atomic
481 @login_required
482 @require_POST
483 def chunk_mass_edit(request):
484     ids = [int(i) for i in request.POST.get('ids').split(',') if i.strip()]
485     chunks = list(Chunk.objects.filter(id__in=ids))
486     
487     stage = request.POST.get('stage')
488     if stage:
489         try:
490             stage = Chunk.tag_model.objects.get(slug=stage)
491         except Chunk.DoesNotExist as e:
492             stage = None
493        
494         for c in chunks: c.stage = stage
495
496     username = request.POST.get('user')
497     logger.info("username: %s" % username)
498     logger.info(request.POST)
499     if username:
500         try:
501             user = User.objects.get(username=username)
502         except User.DoesNotExist as e:
503             user = None
504             
505         for c in chunks: c.user = user
506
507     project_id = request.POST.get('project')
508     if project_id:
509         try:
510             project = Project.objects.get(pk=int(project_id))
511         except (Project.DoesNotExist, ValueError) as e:
512             project = None
513         for c in chunks:
514             book = c.book
515             book.project = project
516             book.save()
517
518     for c in chunks: c.save()
519
520     return HttpResponse("", content_type="text/plain")
521
522
523 @transaction.atomic
524 @login_required
525 @require_POST
526 def image_mass_edit(request):
527     ids = map(int, filter(lambda i: i.strip()!='', request.POST.get('ids').split(',')))
528     images = map(lambda i: Image.objects.get(id=i), ids)
529     
530     stage = request.POST.get('stage')
531     if stage:
532         try:
533             stage = Image.tag_model.objects.get(slug=stage)
534         except Image.DoesNotExist as e:
535             stage = None
536        
537         for c in images: c.stage = stage
538
539     username = request.POST.get('user')
540     logger.info("username: %s" % username)
541     logger.info(request.POST)
542     if username:
543         try:
544             user = User.objects.get(username=username)
545         except User.DoesNotExist as e:
546             user = None
547             
548         for c in images: c.user = user
549
550     project_id = request.POST.get('project')
551     if project_id:
552         try:
553             project = Project.objects.get(pk=int(project_id))
554         except (Project.DoesNotExist, ValueError) as e:
555             project = None
556         for c in images:
557             c.project = project
558
559     for c in images: c.save()
560
561     return HttpResponse("", content_type="text/plain")
562
563
564 @permission_required('documents.change_book')
565 def book_append(request, slug):
566     book = get_object_or_404(Book, slug=slug)
567     if not book.accessible(request):
568         return HttpResponseForbidden("Not authorized.")
569
570     if request.method == "POST":
571         form = forms.BookAppendForm(book, request.POST)
572         if form.is_valid():
573             append_to = form.cleaned_data['append_to']
574             append_to.append(book)
575             return http.HttpResponseRedirect(append_to.get_absolute_url())
576     else:
577         form = forms.BookAppendForm(book)
578     return render(request, "documents/book_append_to.html", {
579         "book": book,
580         "form": form,
581
582         "logout_to": '/',
583     })
584
585
586 @require_POST
587 @login_required
588 def publish(request, slug):
589     form = forms.PublishOptionsForm(request.POST)
590     if form.is_valid():
591         days = form.cleaned_data['days']
592         beta = form.cleaned_data['beta']
593         hidden = form.cleaned_data['hidden']
594     else:
595         days = 0
596         beta = False
597         hidden = False
598     book = get_object_or_404(Book, slug=slug)
599     if not book.accessible(request):
600         return HttpResponseForbidden("Not authorized.")
601
602     try:
603         protocol = 'https://' if request.is_secure() else 'http://'
604         book.publish(request.user, host=protocol + request.get_host(), days=days, beta=beta, hidden=hidden)
605     except NotAuthorizedError:
606         return http.HttpResponseRedirect(reverse('apiclient_oauth' if not beta else 'apiclient_beta_oauth'))
607     except BaseException as e:
608         return http.HttpResponse(repr(e))
609     else:
610         return http.HttpResponseRedirect(book.get_absolute_url())
611
612
613 @require_POST
614 @login_required
615 def publish_image(request, slug):
616     image = get_object_or_404(Image, slug=slug)
617     if not image.accessible(request):
618         return HttpResponseForbidden("Not authorized.")
619
620     try:
621         image.publish(request.user)
622     except NotAuthorizedError:
623         return http.HttpResponseRedirect(reverse('apiclient_oauth'))
624     except BaseException as e:
625         return http.HttpResponse(e)
626     else:
627         return http.HttpResponseRedirect(image.get_absolute_url())
628
629
630 class GalleryView(UploadView):
631     def get_object(self, request, slug):
632         book = get_object_or_404(Book, slug=slug)
633         if not book.gallery:
634             raise Http404
635         return book
636
637     def breadcrumbs(self):
638         return [
639             (_('books'), reverse('documents_document_list')),
640             (self.object.title, self.object.get_absolute_url()),
641             (_('scan gallery'),),
642         ]
643
644     def get_directory(self):
645         return "%s%s/" % (settings.IMAGE_DIR, self.object.gallery)
646
647
648 def active_users_list(request, csv=False):
649     year = int(request.GET.get('y', date.today().year))
650     by_user = defaultdict(lambda: 0)
651     by_email = defaultdict(lambda: 0)
652     names_by_email = defaultdict(set)
653     for change_model in (Chunk.change_model, Image.change_model):
654         for c in change_model.objects.filter(
655                 created_at__year=year).order_by(
656                 'author', 'author_email', 'author_name').values(
657                 'author', 'author_name', 'author_email').annotate(
658                 c=Count('author'), ce=Count('author_email')).distinct():
659             if c['author']:
660                 by_user[c['author']] += c['c']
661             else:
662                 by_email[c['author_email']] += c['ce']
663                 if (c['author_name'] or '').strip():
664                     names_by_email[c['author_email']].add(c['author_name'])
665     for user in User.objects.filter(pk__in=by_user):
666         by_email[user.email] += by_user[user.pk]
667         names_by_email[user.email].add("%s %s" % (user.first_name, user.last_name))
668
669     active_users = []
670     for email, count in by_email.items():
671         active_users.append((email, names_by_email[email], count))
672     active_users.sort(key=lambda x: -x[2])
673     if csv:
674         return http.HttpResponse(
675             '\n'.join((
676                 ','.join(
677                     (str(x[2]), x[0], ','.join(x[1]))
678                 )
679                 for x in active_users
680             )),
681             content_type='text/csv',
682             headers={
683                 'Content-Disposition': f'attachment; filename=redakcja-{year}.csv',
684             }
685         )
686     else:
687         return render(request, 'documents/active_users_list.html', {
688             'users': active_users,
689             'year': year,
690         })
691
692
693 @user_passes_test(lambda u: u.is_superuser)
694 def mark_final(request):
695     if request.method == 'POST':
696         form = forms.MarkFinalForm(data=request.POST)
697         if form.is_valid():
698             form.save()
699             return HttpResponseRedirect(reverse('mark_final_completed'))
700     else:
701         form = forms.MarkFinalForm()
702     return render(request, 'documents/mark_final.html', {'form': form})
703
704
705 def mark_final_completed(request):
706     return render(request, 'documents/mark_final_completed.html')