Catalogue: wikidata suggestions
[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 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     return render(request, 'documents/my_page.html', {
69         'last_books': last_books,
70         "logout_to": '/',
71         })
72
73
74 @active_tab('users')
75 def users(request):
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'),
79     })
80
81
82 @active_tab('activity')
83 def activity(request, isodate=None):
84     today = date.today()
85     try:
86         day = helpers.parse_isodate(isodate)
87     except ValueError:
88         day = today
89
90     if day > today:
91         raise Http404
92     if day != today:
93         next_day = day + timedelta(1)
94     prev_day = day - timedelta(1)
95
96     return render(request, 'documents/activity.html', locals())
97
98
99 @never_cache
100 def logout_then_redirect(request):
101     auth.logout(request)
102     return http.HttpResponseRedirect(quote_plus(request.GET.get('next', '/'), safe='/?='))
103
104
105 @permission_required('documents.add_book')
106 @active_tab('create')
107 def create_missing(request, slug=None):
108     if slug is None:
109         slug = ''
110     slug = slug.replace(' ', '-')
111
112     if request.method == "POST":
113         form = forms.DocumentCreateForm(request.POST, request.FILES)
114         if form.is_valid():
115             
116             if request.user.is_authenticated:
117                 creator = request.user
118             else:
119                 creator = None
120             book = Book.create(
121                 text=form.cleaned_data['text'],
122                 creator=creator,
123                 slug=form.cleaned_data['slug'],
124                 title=form.cleaned_data['title'],
125                 gallery=form.cleaned_data['gallery'],
126             )
127
128             return http.HttpResponseRedirect(reverse("documents_book", args=[book.slug]))
129     else:
130         form = forms.DocumentCreateForm(initial={
131                 "slug": slug,
132                 "title": slug.replace('-', ' ').title(),
133                 "gallery": slug,
134         })
135
136     return render(request, "documents/document_create_missing.html", {
137         "slug": slug,
138         "form": form,
139
140         "logout_to": '/',
141     })
142
143
144 @permission_required('documents.add_book')
145 @active_tab('upload')
146 def upload(request):
147     if request.method == "POST":
148         form = forms.DocumentsUploadForm(request.POST, request.FILES)
149         if form.is_valid():
150             from slugify import slugify
151
152             if request.user.is_authenticated:
153                 creator = request.user
154             else:
155                 creator = None
156
157             zip = form.cleaned_data['zip']
158             skipped_list = []
159             ok_list = []
160             error_list = []
161             slugs = {}
162             existing = [book.slug for book in Book.objects.all()]
163             for filename in zip.namelist():
164                 if filename[-1] == '/':
165                     continue
166                 title = os.path.basename(filename)[:-4]
167                 slug = slugify(title)
168                 if not (slug and filename.endswith('.xml')):
169                     skipped_list.append(filename)
170                 elif slug in slugs:
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.')))
174                 else:
175                     try:
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
181
182             if not error_list:
183                 for filename, slug, title in ok_list:
184                     book = Book.create(
185                         text=zip.read(filename).decode('utf-8'),
186                         creator=creator,
187                         slug=slug,
188                         title=title,
189                     )
190
191             return render(request, "documents/document_upload.html", {
192                 "form": form,
193                 "ok_list": ok_list,
194                 "skipped_list": skipped_list,
195                 "error_list": error_list,
196
197                 "logout_to": '/',
198             })
199     else:
200         form = forms.DocumentsUploadForm()
201
202     return render(request, "documents/document_upload.html", {
203         "form": form,
204
205         "logout_to": '/',
206     })
207
208
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
215     return response
216
217
218 @never_cache
219 def book_xml(request, slug):
220     book = get_object_or_404(Book, slug=slug)
221     return serve_xml(request, book, slug)
222
223
224 @never_cache
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)
228
229
230 @never_cache
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.")
235
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
240     return response
241
242
243 @never_cache
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.")
248
249     doc = book.wldocument(parse_dublincore=False)
250     html = doc.as_html(options={'gallery': "'%s'" % book.gallery_url()})
251
252     html = html.get_bytes().decode('utf-8') if html is not None else ''
253     # response = http.HttpResponse(html, content_type='text/html')
254     # return response
255     # book_themes = {}
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)
259
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())
263
264
265 @never_cache
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.")
270
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')
279
280
281 @never_cache
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.")
286
287     # TODO: move to celery
288     doc = book.wldocument()
289     # TODO: error handling
290
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'
295     response.write(epub)
296     return response
297
298
299 @never_cache
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.")
304
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'
311     response.write(mobi)
312     return response
313
314
315 @never_cache
316 def revision(request, slug, chunk=None):
317     try:
318         doc = Chunk.get(slug, chunk)
319     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
320         raise Http404
321     if not doc.book.accessible(request):
322         return HttpResponseForbidden("Not authorized.")
323     return http.HttpResponse(str(doc.revision()))
324
325
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.")
330
331     if request.user.has_perm('documents.change_book'):
332         if request.method == "POST":
333             form = forms.BookForm(request.POST, instance=book)
334             if form.is_valid():
335                 form.save()
336                 return http.HttpResponseRedirect(book.get_absolute_url())
337         else:
338             form = forms.BookForm(instance=book)
339         publish_options_form = forms.PublishOptionsForm()
340         editable = True
341     else:
342         form = forms.ReadonlyBookForm(instance=book)
343         publish_options_form = forms.PublishOptionsForm()
344         editable = False
345
346     publish_error = book.publishable_error()
347     publishable = publish_error is None
348
349     try:
350         doc = book.wldocument()
351     except:
352         doc = None
353     
354     return render(request, "documents/book_detail.html", {
355         "book": book,
356         "doc": doc,
357         "publishable": publishable,
358         "publishable_error": publish_error,
359         "form": form,
360         "publish_options_form": publish_options_form,
361         "editable": editable,
362     })
363
364
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.")
369
370     if request.user.has_perm('documents.change_image'):
371         if request.method == "POST":
372             form = forms.ImageForm(request.POST, instance=image)
373             if form.is_valid():
374                 form.save()
375                 return http.HttpResponseRedirect(image.get_absolute_url())
376         else:
377             form = forms.ImageForm(instance=image)
378         editable = True
379     else:
380         form = forms.ReadonlyImageForm(instance=image)
381         editable = False
382
383     publish_error = image.publishable_error()
384     publishable = publish_error is None
385
386     return render(request, "documents/image_detail.html", {
387         "object": image,
388         "publishable": publishable,
389         "publishable_error": publish_error,
390         "form": form,
391         "editable": editable,
392     })
393
394
395 @permission_required('documents.add_chunk')
396 def chunk_add(request, slug, chunk):
397     try:
398         doc = Chunk.get(slug, chunk)
399     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
400         raise Http404
401     if not doc.book.accessible(request):
402         return HttpResponseForbidden("Not authorized.")
403
404     if request.method == "POST":
405         form = forms.ChunkAddForm(request.POST, instance=doc)
406         if form.is_valid():
407             if request.user.is_authenticated:
408                 creator = request.user
409             else:
410                 creator = None
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']
417             )
418
419             return http.HttpResponseRedirect(doc.book.get_absolute_url())
420     else:
421         form = forms.ChunkAddForm(initial={
422                 "slug": str(doc.number + 1),
423                 "title": "cz. %d" % (doc.number + 1, ),
424         })
425
426     return render(request, "documents/chunk_add.html", {
427         "chunk": doc,
428         "form": form,
429     })
430
431
432 @login_required
433 def chunk_edit(request, slug, chunk):
434     try:
435         doc = Chunk.get(slug, chunk)
436     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
437         raise Http404
438     if not doc.book.accessible(request):
439         return HttpResponseForbidden("Not authorized.")
440
441     if request.method == "POST":
442         form = forms.ChunkForm(request.POST, instance=doc)
443         if form.is_valid():
444             form.save()
445             go_next = request.GET.get('next', None)
446             if go_next:
447                 go_next = quote_plus(unquote(iri_to_uri(go_next)), safe='/?=&')
448             else:
449                 go_next = doc.book.get_absolute_url()
450             return http.HttpResponseRedirect(go_next)
451     else:
452         form = forms.ChunkForm(instance=doc)
453
454     referer = request.META.get('HTTP_REFERER')
455     if referer:
456         parts = urlsplit(referer)
457         parts = ['', ''] + list(parts[2:])
458         go_next = quote_plus(urlunsplit(parts))
459     else:
460         go_next = ''
461
462     return render(request, "documents/chunk_edit.html", {
463         "chunk": doc,
464         "form": form,
465         "go_next": go_next,
466     })
467
468
469 @transaction.atomic
470 @login_required
471 @require_POST
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))
475     
476     stage = request.POST.get('stage')
477     if stage:
478         try:
479             stage = Chunk.tag_model.objects.get(slug=stage)
480         except Chunk.DoesNotExist as e:
481             stage = None
482        
483         for c in chunks: c.stage = stage
484
485     username = request.POST.get('user')
486     logger.info("username: %s" % username)
487     logger.info(request.POST)
488     if username:
489         try:
490             user = User.objects.get(username=username)
491         except User.DoesNotExist as e:
492             user = None
493             
494         for c in chunks: c.user = user
495
496     project_id = request.POST.get('project')
497     if project_id:
498         try:
499             project = Project.objects.get(pk=int(project_id))
500         except (Project.DoesNotExist, ValueError) as e:
501             project = None
502         for c in chunks:
503             book = c.book
504             book.project = project
505             book.save()
506
507     for c in chunks: c.save()
508
509     return HttpResponse("", content_type="text/plain")
510
511
512 @transaction.atomic
513 @login_required
514 @require_POST
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)
518     
519     stage = request.POST.get('stage')
520     if stage:
521         try:
522             stage = Image.tag_model.objects.get(slug=stage)
523         except Image.DoesNotExist as e:
524             stage = None
525        
526         for c in images: c.stage = stage
527
528     username = request.POST.get('user')
529     logger.info("username: %s" % username)
530     logger.info(request.POST)
531     if username:
532         try:
533             user = User.objects.get(username=username)
534         except User.DoesNotExist as e:
535             user = None
536             
537         for c in images: c.user = user
538
539     project_id = request.POST.get('project')
540     if project_id:
541         try:
542             project = Project.objects.get(pk=int(project_id))
543         except (Project.DoesNotExist, ValueError) as e:
544             project = None
545         for c in images:
546             c.project = project
547
548     for c in images: c.save()
549
550     return HttpResponse("", content_type="text/plain")
551
552
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.")
558
559     if request.method == "POST":
560         form = forms.BookAppendForm(book, request.POST)
561         if form.is_valid():
562             append_to = form.cleaned_data['append_to']
563             append_to.append(book)
564             return http.HttpResponseRedirect(append_to.get_absolute_url())
565     else:
566         form = forms.BookAppendForm(book)
567     return render(request, "documents/book_append_to.html", {
568         "book": book,
569         "form": form,
570
571         "logout_to": '/',
572     })
573
574
575 @require_POST
576 @login_required
577 def publish(request, slug):
578     form = forms.PublishOptionsForm(request.POST)
579     if form.is_valid():
580         days = form.cleaned_data['days']
581         beta = form.cleaned_data['beta']
582         hidden = form.cleaned_data['hidden']
583     else:
584         days = 0
585         beta = False
586         hidden = False
587     book = get_object_or_404(Book, slug=slug)
588     if not book.accessible(request):
589         return HttpResponseForbidden("Not authorized.")
590
591     try:
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))
598     else:
599         return http.HttpResponseRedirect(book.get_absolute_url())
600
601
602 @require_POST
603 @login_required
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.")
608
609     try:
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)
615     else:
616         return http.HttpResponseRedirect(image.get_absolute_url())
617
618
619 class GalleryView(UploadView):
620     def get_object(self, request, slug):
621         book = get_object_or_404(Book, slug=slug)
622         if not book.gallery:
623             raise Http404
624         return book
625
626     def breadcrumbs(self):
627         return [
628             (_('books'), reverse('documents_document_list')),
629             (self.object.title, self.object.get_absolute_url()),
630             (_('scan gallery'),),
631         ]
632
633     def get_directory(self):
634         return "%s%s/" % (settings.IMAGE_DIR, self.object.gallery)
635
636
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():
648             if c['author']:
649                 by_user[c['author']] += c['c']
650             else:
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))
657
658     active_users = []
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])
662     if csv:
663         return http.HttpResponse(
664             '\n'.join((
665                 ','.join(
666                     (str(x[2]), x[0], ','.join(x[1]))
667                 )
668                 for x in active_users
669             )),
670             content_type='text/csv',
671             headers={
672                 'Content-Disposition': f'attachment; filename=redakcja-{year}.csv',
673             }
674         )
675     else:
676         return render(request, 'documents/active_users_list.html', {
677             'users': active_users,
678             'year': year,
679         })
680
681
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)
686         if form.is_valid():
687             form.save()
688             return HttpResponseRedirect(reverse('mark_final_completed'))
689     else:
690         form = forms.MarkFinalForm()
691     return render(request, 'documents/mark_final.html', {'form': form})
692
693
694 def mark_final_completed(request):
695     return render(request, 'documents/mark_final_completed.html')