update editor
[redakcja.git] / apps / catalogue / views.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import logging
7 import os
8 import shutil
9 import subprocess
10 from tempfile import NamedTemporaryFile
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
16 from django.contrib.sites.models import Site
17 from django.core.urlresolvers import reverse
18 from django import http
19 from django.http import Http404, HttpResponse
20 from django.shortcuts import get_object_or_404, render, redirect
21 from django.utils.encoding import force_str
22 from django.utils.http import urlquote_plus
23 from django.views.decorators.http import require_POST
24
25 from catalogue import forms
26 from catalogue.forms import TagMultipleForm, TagSingleForm
27 from catalogue.helpers import active_tab
28 from catalogue.models import Category
29 from librarian import BuildError
30 from redakcja.utlis import send_notify_email
31 from .constants import STAGES
32 from .models import Document, Plan
33 from dvcs.models import Revision
34 from organizations.models import Organization
35 from fileupload.views import UploadView
36
37 #
38 # Quick hack around caching problems, TODO: use ETags
39 #
40 from django.views.decorators.cache import never_cache
41 # from fnpdjango.utils.text.slughifi import slughifi
42
43 logger = logging.getLogger("fnp.catalogue")
44
45
46 @active_tab('all')
47 @never_cache
48 def document_list(request):
49     return render(request, 'catalogue/document_list.html')
50
51
52 @never_cache
53 def user(request, username):
54     user = get_object_or_404(User, username=username)
55     return render(request, 'catalogue/user_page.html', {"viewed_user": user})
56
57
58 @login_required
59 @active_tab('my')
60 @never_cache
61 def my(request):
62     return render(request, 'catalogue/my_page.html', {
63         'last_books': sorted(
64             request.session.get("wiki_last_books", {}).items(), key=lambda x: x[1]['time'], reverse=True),
65
66         "logout_to": '/',
67         })
68
69
70 @never_cache
71 def logout_then_redirect(request):
72     auth.logout(request)
73     return http.HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
74
75
76 @login_required
77 @active_tab('create')
78 def create_missing(request):
79     if request.method == "POST":
80         form = forms.DocumentCreateForm(request.POST, request.FILES)
81         # tag_forms = [
82         #     (TagMultipleForm if category.multiple else TagSingleForm)(
83         #         category=category, data=request.POST, prefix=category.dc_tag)
84         #     for category in Category.objects.all()]
85         if form.is_valid():  # and all(tag_form.is_valid() for tag_form in tag_forms):
86             
87             if request.user.is_authenticated():
88                 creator = request.user
89             else:
90                 creator = None
91
92             title = form.cleaned_data['title']
93             try:
94                 org = request.user.membership_set.get(
95                     organization=int(form.cleaned_data['owner_organization'])).organization
96                 kwargs = {'owner_organization': org}
97             except:
98                 kwargs = {'owner_user': request.user}
99
100             doc = Document.objects.create(**kwargs)
101
102             cover = request.FILES.get('cover')
103             if cover:
104                 uppath = 'uploads/%d/' % doc.pk
105                 path = settings.MEDIA_ROOT + uppath
106                 if not os.path.isdir(path):
107                     os.makedirs(path)
108                 dest_path = path + cover.name   # UNSAFE
109                 with open(dest_path, 'w') as destination:
110                     for chunk in cover.chunks():
111                         destination.write(chunk)
112                 cover_url = 'http://milpeer.eu/media/dynamic/' + uppath + cover.name
113             else:
114                 cover_url = ''
115
116             doc.commit(
117                 text='''<section xmlns="http://nowoczesnapolska.org.pl/sst#" xmlns:dc="http://purl.org/dc/elements/1.1/">
118                 <metadata>
119                     <dc:publisher>''' + form.cleaned_data['publisher'] + '''</dc:publisher>
120                     <dc:description>''' + form.cleaned_data['description'] + '''</dc:description>
121                     <dc:language>''' + form.cleaned_data['language'] + '''</dc:language>
122                     <dc:rights>''' + form.cleaned_data['rights'] + '''</dc:rights>
123                     <dc:audience>''' + form.cleaned_data['audience'] + '''</dc:audience>
124                     <dc:relation.coverImage.url>''' + cover_url + '''</dc:relation.coverImage.url>
125                 </metadata>
126                 <header>''' + title + '''</header>
127                 <div class="p"> </div>
128                 </section>''',
129                 author=creator
130             )
131             doc.assigned_to = request.user
132             doc.save()
133
134             return http.HttpResponseRedirect(reverse("wiki_editor", args=[doc.pk]))
135     else:
136         org_pk = request.GET.get('organization')
137         if org_pk:
138             try:
139                 org = Organization.objects.get(pk=org_pk)
140             except Organization.DoesNotExist:
141                 org = None
142             else:
143                 if not org.is_member(request.user):
144                     org = None
145         else:
146             org = None
147         if org is not None:
148             org = org.pk
149
150         form = forms.DocumentCreateForm(initial={'owner_organization': org})
151
152         # tag_forms = [
153         #     (TagMultipleForm if category.multiple else TagSingleForm)(category=category, prefix=category.dc_tag)
154         #     for category in Category.objects.all()]
155
156     return render(request, "catalogue/document_create_missing.html", {
157         "form": form,
158         # "tag_forms": tag_forms,
159
160         "logout_to": '/',
161     })
162
163
164 @never_cache
165 def book_html(request, pk, rev_pk=None, preview=False):
166     from librarian.document import Document as SST
167     from librarian.formats.html import HtmlFormat
168
169     doc = get_object_or_404(Document, pk=pk, deleted=False)
170
171     try:
172         published_revision = doc.publish_log.all()[0].revision
173     except IndexError:
174         published_revision = None
175
176     if rev_pk is None:
177         if preview:
178             revision = doc.revision
179         else:
180             if published_revision is not None:
181                 revision = published_revision
182             else:
183                 # No published version, fallback to preview mode.
184                 preview = True
185                 revision = doc.revision
186     else:
187         revision = get_object_or_404(Revision, pk=rev_pk)
188
189     was_published = revision == published_revision or doc.publish_log.filter(revision=revision).exists()
190
191     try:
192         sst = SST.from_string(revision.materialize())
193     except ValueError as e:
194         html = e
195     else:
196         html = HtmlFormat(sst).build(
197             files_path='http://%s/media/dynamic/uploads/%s/' % (request.get_host(), pk)).get_string()
198
199     # response = http.HttpResponse(html, content_type='text/html', mimetype='text/html')
200     # return response
201     # book_themes = {}
202     # for fragment in book.fragments.all().iterator():
203     #     for theme in fragment.tags.filter(category='theme').iterator():
204     #         book_themes.setdefault(theme, []).append(fragment)
205
206     # book_themes = book_themes.items()
207     # book_themes.sort(key=lambda s: s[0].sort_key)
208     return render(request, 'catalogue/book_text.html', {
209         'doc': doc,
210         'preview': preview,
211         'revision': revision,
212         'published_revision': published_revision,
213         'specific': rev_pk is not None,
214         'html': html,
215         'can_edit': doc.can_edit(request.user) if doc else None,
216         'was_published': was_published,
217     })
218
219
220 @never_cache
221 def book_pdf(request, pk, rev_pk):
222     from librarian.utils import Context
223     from librarian.document import Document as SST
224     from librarian.formats.pdf import PdfFormat
225
226     doc = get_object_or_404(Document, pk=pk)
227     rev = get_object_or_404(Revision, pk=rev_pk)
228     # Test
229
230     try:
231         sst = SST.from_string(rev.materialize())
232     except ValueError as e:
233         return HttpResponse(content=force_str(e.message), content_type='text/plain', status='400')
234     
235     ctx = Context(
236         files_path='http://%s/media/dynamic/uploads/%s/' % (request.get_host(), pk),
237         source_url='http://%s%s' % (request.get_host(), reverse('catalogue_html', args=[doc.pk])),
238     )
239     if doc.owner_organization is not None and doc.owner_organization.logo:
240         ctx.cover_logo = 'http://%s%s' % (request.get_host(), doc.owner_organization.logo.url)
241     try:
242         pdf_file = PdfFormat(sst).build(ctx)
243     except BuildError as e:
244         return HttpResponse(content=force_str(e.message), content_type='text/plain', status='400')
245
246     from catalogue.ebook_utils import serve_file
247     return serve_file(pdf_file.get_filename(), '%d.pdf' % doc.pk, 'application/pdf')
248
249
250 @never_cache
251 def book_epub(request, pk, rev_pk):
252     from librarian.utils import Context
253     from librarian.document import Document as SST
254     from librarian.formats.epub import EpubFormat
255
256     doc = get_object_or_404(Document, pk=pk)
257     rev = get_object_or_404(Revision, pk=rev_pk)
258     # Test
259
260     try:
261         sst = SST.from_string(rev.materialize())
262     except ValueError as e:
263         return HttpResponse(content=force_str(e.message), content_type='text/plain', status='400')
264
265     ctx = Context(
266         files_path='http://%s/media/dynamic/uploads/%s/' % (request.get_host(), pk),
267         source_url='http://%s%s' % (request.get_host(), reverse('catalogue_html', args=[doc.pk])),
268     )
269     if doc.owner_organization is not None and doc.owner_organization.logo:
270         ctx.cover_logo = 'http://%s%s' % (request.get_host(), doc.owner_organization.logo.url)
271     try:
272         epub_file = EpubFormat(sst).build(ctx)
273     except BuildError as e:
274         return HttpResponse(content=force_str(e.message), content_type='text/plain', status='400')
275
276     from catalogue.ebook_utils import serve_file
277     return serve_file(epub_file.get_filename(), '%d.epub' % doc.pk, 'application/epub+zip')
278
279
280 @never_cache
281 def book_mobi(request, pk, rev_pk):
282     from librarian.utils import Context
283     from librarian.document import Document as SST
284     from librarian.formats.epub import EpubFormat
285
286     doc = get_object_or_404(Document, pk=pk)
287     rev = get_object_or_404(Revision, pk=rev_pk)
288
289     try:
290         sst = SST.from_string(rev.materialize())
291     except ValueError as e:
292         return HttpResponse(content=force_str(e.message), content_type='text/plain', status='400')
293
294     ctx = Context(
295         files_path='http://%s/media/dynamic/uploads/%s/' % (request.get_host(), pk),
296         source_url='http://%s%s' % (request.get_host(), reverse('catalogue_html', args=[doc.pk])),
297     )
298     if doc.owner_organization is not None and doc.owner_organization.logo:
299         ctx.cover_logo = 'http://%s%s' % (request.get_host(), doc.owner_organization.logo.url)
300     try:
301         epub_file = EpubFormat(sst).build(ctx)
302     except BuildError as e:
303         return HttpResponse(content=force_str(e.message), content_type='text/plain', status='400')
304
305     output_file = NamedTemporaryFile(prefix='librarian', suffix='.mobi', delete=False)
306     output_file.close()
307     subprocess.check_call(
308         ['ebook-convert', epub_file.get_filename(), output_file.name, '--no-inline-toc'])
309
310     from catalogue.ebook_utils import serve_file
311     return serve_file(output_file.name, '%d.mobi' % doc.pk, 'application/epub+zip')
312
313
314 # @never_cache
315 # def revision(request, slug, chunk=None):
316 #     try:
317 #         doc = Chunk.get(slug, chunk)
318 #     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
319 #         raise Http404
320 #     if not doc.book.accessible(request):
321 #         return HttpResponseForbidden("Not authorized.")
322 #     return http.HttpResponse(str(doc.revision()))
323
324
325 @login_required
326 def book_schedule(request, pk):
327     book = get_object_or_404(Document, pk=pk, deleted=False)
328     if request.method == 'POST':
329         Plan.objects.filter(document=book).delete()
330         for i, s in enumerate(STAGES):
331             user_id = request.POST.get('s%d-user' % i)
332             deadline = request.POST.get('s%d-deadline' % i) or None
333             Plan.objects.create(document=book, stage=s, user_id=user_id, deadline=deadline)
334
335         book.set_stage(request.POST.get('stage', ''))
336         return redirect('catalogue_user')
337
338     current = {}
339     for p in Plan.objects.filter(document=book):
340         current[p.stage] = (getattr(p.user, 'pk', None), (p.deadline.isoformat() if p.deadline else None))
341
342     schedule = [(i, s, current.get(s, ())) for (i, s) in enumerate(STAGES)]
343     
344     if book.owner_organization:
345         people = [m.user for m in book.owner_organization.membership_set.exclude(status='pending')]
346     else:
347         people = [book.owner_user]
348     return render(request, 'catalogue/book_schedule.html', {
349         'book': book,
350         'schedule': schedule,
351         'people': people,
352     })
353
354
355 @login_required
356 def book_owner(request, pk):
357     doc = get_object_or_404(Document, pk=pk, deleted=False)
358     user_is_owner = doc.owner_organization and doc.owner_organization.is_member(request.user)
359     if not (doc.owner_user == request.user or user_is_owner):
360         raise Http404
361
362     error = ''
363
364     if request.method == 'POST':
365         # TODO: real form
366         new_org_pk = request.POST.get('owner_organization')
367         if not new_org_pk:
368             doc.owner_organization = None
369             doc.owner_user = request.user
370             doc.save()
371         else:
372             org = Organization.objects.get(pk=new_org_pk)
373             if not org.is_member(request.user):
374                 error = 'Bad organization'
375             else:
376                 doc.owner_organization = org
377                 doc.owner_user = None
378                 doc.save()
379         if not error:
380             return redirect('catalogue_user')
381
382     return render(request, 'catalogue/book_owner.html', {
383         'doc': doc,
384         'error': error,
385     })
386
387
388 @login_required
389 def book_delete(request, pk):
390     doc = get_object_or_404(Document, pk=pk, deleted=False)
391     if not (doc.owner_user == request.user or doc.owner_organization.is_member(request.user)):
392         raise Http404
393
394     if request.method == 'POST':
395         doc.deleted = True
396         doc.save()
397         return redirect('catalogue_user')
398
399     return render(request, 'catalogue/book_delete.html', {
400         'doc': doc,
401     })
402
403
404 @require_POST
405 @login_required
406 def publish(request, pk):
407     from wiki import forms
408     from .models import PublishRecord
409     from dvcs.models import Revision
410
411     # FIXME: check permissions
412
413     doc = get_object_or_404(Document, pk=pk, deleted=False)
414     form = forms.DocumentTextPublishForm(request.POST, prefix="textpublish")
415     if form.is_valid():
416         rev = Revision.objects.get(pk=form.cleaned_data['revision'])
417         # FIXME: check if in tree
418         # if PublishRecord.objects.filter(revision=rev, document=doc).exists():
419         #     return http.HttpResponse('exists')
420         if not doc.published:
421             site = Site.objects.get_current()
422             send_notify_email(
423                 'New published document in MIL/PEER',
424                 '''New published document in MIL/PEER: %s. View it in browser: https://%s%s.
425
426 --
427 MIL/PEER team.''' % (doc.meta()['title'], site.domain, reverse('catalogue_html', args=[doc.pk])))
428         PublishRecord.objects.create(revision=rev, document=doc, user=request.user)
429         doc.published = True
430         doc.save()
431         if request.is_ajax():
432             return http.HttpResponse('ok')
433         else:
434             return redirect('catalogue_html', doc.pk)
435     else:
436         if request.is_ajax():
437             return http.HttpResponse('error')
438         else:
439             try:
440                 return redirect('catalogue_preview_rev', doc.pk, form.cleaned_data['revision'])
441             except KeyError:
442                 return redirect('catalogue_preview', doc.pk)
443
444
445 @require_POST
446 @login_required
447 def unpublish(request, pk):
448     # FIXME: check permissions
449
450     doc = get_object_or_404(Document, pk=pk, deleted=False)
451     doc.publish_log.all().delete()
452     if request.is_ajax():
453         return http.HttpResponse('ok')
454     else:
455         return redirect('catalogue_html', doc.pk)
456
457
458 class GalleryMixin(object):
459     def get_directory(self):
460         # return "%s%s/" % (settings.IMAGE_DIR, 'org%d' % self.org.pk if self.org is not None else self.request.user.pk)
461         return "uploads/%d/" % self.doc.pk
462
463
464 class GalleryView(GalleryMixin, UploadView):
465
466     def breadcrumbs(self):
467         return [
468                 (self.doc.meta()['title'], '/documents/%d/' % self.doc.pk),
469             ]
470
471     def get_object(self, request, pk=None):
472         self.doc = Document.objects.get(pk=pk, deleted=False)
473
474
475 @login_required
476 def fork(request, pk):
477     doc = get_object_or_404(Document, pk=pk, deleted=False)
478     if request.method == "POST":
479         form = forms.DocumentForkForm(request.POST, request.FILES)
480         if form.is_valid():
481             try:
482                 org = request.user.membership_set.get(
483                     organization=int(form.cleaned_data['owner_organization'])).organization
484                 kwargs = {'owner_organization': org}
485             except:
486                 kwargs = {'owner_user': request.user}
487
488             new_doc = Document.objects.create(revision=doc.revision, **kwargs)
489
490             if os.path.isdir(settings.MEDIA_ROOT + "uploads/%d" % doc.pk):
491                 shutil.copytree(
492                     settings.MEDIA_ROOT + "uploads/%d" % doc.pk,
493                     settings.MEDIA_ROOT + "uploads/%d" % new_doc.pk
494                 )
495
496             new_doc.assigned_to = request.user
497             new_doc.save()
498
499             return http.HttpResponseRedirect(reverse("wiki_editor", args=[new_doc.pk]))
500     else:
501         form = forms.DocumentForkForm()
502
503     return render(request, "catalogue/document_fork.html", {
504         "form": form,
505
506         "logout_to": '/',
507     })
508
509
510 def upcoming(request):
511     return render(request, "catalogue/upcoming.html", {
512         'objects_list': Document.objects.filter(deleted=False).filter(publish_log=None),
513     })
514
515
516 def finished(request):
517     return render(request, "catalogue/finished.html", {
518         'objects_list': Document.objects.filter(deleted=False).exclude(publish_log=None),
519     })