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