bc54c3ea3e4cc801a1dc709357d1d37aec34d9ce
[redakcja.git] / apps / catalogue / views.py
1 from datetime import datetime, date, timedelta
2 import logging
3 import os
4 from StringIO import StringIO
5 from urllib import unquote
6 from urlparse import urlsplit, urlunsplit
7
8 from django.contrib import auth
9 from django.contrib.auth.models import User
10 from django.contrib.auth.decorators import login_required, permission_required
11 from django.core.urlresolvers import reverse
12 from django.db.models import Count, Q
13 from django import http
14 from django.http import Http404, HttpResponse, HttpResponseForbidden
15 from django.shortcuts import get_object_or_404, render
16 from django.utils.encoding import iri_to_uri
17 from django.utils.http import urlquote_plus
18 from django.utils.translation import ugettext_lazy as _
19 from django.views.decorators.http import require_POST
20 from django.views.generic.simple import direct_to_template
21
22 from apiclient import NotAuthorizedError
23 from catalogue import forms
24 from catalogue import helpers
25 from catalogue.helpers import active_tab
26 from catalogue.models import (Book, Chunk, Image, BookPublishRecord, 
27         ChunkPublishRecord, ImagePublishRecord)
28 from catalogue.tasks import publishable_error
29
30 #
31 # Quick hack around caching problems, TODO: use ETags
32 #
33 from django.views.decorators.cache import never_cache
34
35 logger = logging.getLogger("fnp.catalogue")
36
37
38 @active_tab('all')
39 @never_cache
40 def document_list(request):
41     return render(request, 'catalogue/document_list.html')
42
43
44 @active_tab('images')
45 @never_cache
46 def image_list(request, user=None):
47     return render(request, 'catalogue/image_list.html')
48
49
50 @never_cache
51 def user(request, username):
52     user = get_object_or_404(User, username=username)
53     return render(request, 'catalogue/user_page.html', {"viewed_user": user})
54
55
56 @login_required
57 @active_tab('my')
58 @never_cache
59 def my(request):
60     return render(request, 'catalogue/my_page.html', {
61         'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
62                         key=lambda x: x[1]['time'], reverse=True),
63
64         "logout_to": '/',
65         })
66
67
68 @active_tab('users')
69 def users(request):
70     return direct_to_template(request, 'catalogue/user_list.html', extra_context={
71         'users': User.objects.all().annotate(count=Count('chunk')).order_by(
72             '-count', 'last_name', 'first_name'),
73     })
74
75
76 @active_tab('activity')
77 def activity(request, isodate=None):
78     today = date.today()
79     try:
80         day = helpers.parse_isodate(isodate)
81     except ValueError:
82         day = today
83
84     if day > today:
85         raise Http404
86     if day != today:
87         next_day = day + timedelta(1)
88     prev_day = day - timedelta(1)
89
90     return render(request, 'catalogue/activity.html', locals())
91
92
93 @never_cache
94 def logout_then_redirect(request):
95     auth.logout(request)
96     return http.HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
97
98
99 @permission_required('catalogue.add_book')
100 @active_tab('create')
101 def create_missing(request, slug=None):
102     if slug is None:
103         slug = ''
104     slug = slug.replace(' ', '-')
105
106     if request.method == "POST":
107         form = forms.DocumentCreateForm(request.POST, request.FILES)
108         if form.is_valid():
109             
110             if request.user.is_authenticated():
111                 creator = request.user
112             else:
113                 creator = None
114             book = Book.create(
115                 text=form.cleaned_data['text'],
116                 creator=creator,
117                 slug=form.cleaned_data['slug'],
118                 title=form.cleaned_data['title'],
119                 gallery=form.cleaned_data['gallery'],
120             )
121
122             return http.HttpResponseRedirect(reverse("catalogue_book", args=[book.slug]))
123     else:
124         form = forms.DocumentCreateForm(initial={
125                 "slug": slug,
126                 "title": slug.replace('-', ' ').title(),
127                 "gallery": slug,
128         })
129
130     return direct_to_template(request, "catalogue/document_create_missing.html", extra_context={
131         "slug": slug,
132         "form": form,
133
134         "logout_to": '/',
135     })
136
137
138 @permission_required('catalogue.add_book')
139 @active_tab('upload')
140 def upload(request):
141     if request.method == "POST":
142         form = forms.DocumentsUploadForm(request.POST, request.FILES)
143         if form.is_valid():
144             import slughifi
145
146             if request.user.is_authenticated():
147                 creator = request.user
148             else:
149                 creator = None
150
151             zip = form.cleaned_data['zip']
152             skipped_list = []
153             ok_list = []
154             error_list = []
155             slugs = {}
156             existing = [book.slug for book in Book.objects.all()]
157             for filename in zip.namelist():
158                 if filename[-1] == '/':
159                     continue
160                 title = os.path.basename(filename)[:-4]
161                 slug = slughifi(title)
162                 if not (slug and filename.endswith('.xml')):
163                     skipped_list.append(filename)
164                 elif slug in slugs:
165                     error_list.append((filename, slug, _('Slug already used for %s' % slugs[slug])))
166                 elif slug in existing:
167                     error_list.append((filename, slug, _('Slug already used in repository.')))
168                 else:
169                     try:
170                         zip.read(filename).decode('utf-8') # test read
171                         ok_list.append((filename, slug, title))
172                     except UnicodeDecodeError:
173                         error_list.append((filename, title, _('File should be UTF-8 encoded.')))
174                     slugs[slug] = filename
175
176             if not error_list:
177                 for filename, slug, title in ok_list:
178                     book = Book.create(
179                         text=zip.read(filename).decode('utf-8'),
180                         creator=creator,
181                         slug=slug,
182                         title=title,
183                     )
184
185             return direct_to_template(request, "catalogue/document_upload.html", extra_context={
186                 "form": form,
187                 "ok_list": ok_list,
188                 "skipped_list": skipped_list,
189                 "error_list": error_list,
190
191                 "logout_to": '/',
192             })
193     else:
194         form = forms.DocumentsUploadForm()
195
196     return direct_to_template(request, "catalogue/document_upload.html", extra_context={
197         "form": form,
198
199         "logout_to": '/',
200     })
201
202
203 @never_cache
204 def book_xml(request, slug):
205     book = get_object_or_404(Book, slug=slug)
206     if not book.accessible(request):
207         return HttpResponseForbidden("Not authorized.")
208     xml = book.materialize()
209
210     response = http.HttpResponse(xml, content_type='application/xml', mimetype='application/wl+xml')
211     response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
212     return response
213
214
215 @never_cache
216 def book_txt(request, slug):
217     book = get_object_or_404(Book, slug=slug)
218     if not book.accessible(request):
219         return HttpResponseForbidden("Not authorized.")
220     xml = book.materialize()
221     output = StringIO()
222     # errors?
223
224     import librarian.text
225     librarian.text.transform(StringIO(xml), output)
226     text = output.getvalue()
227     response = http.HttpResponse(text, content_type='text/plain', mimetype='text/plain')
228     response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug
229     return response
230
231
232 @never_cache
233 def book_html(request, slug):
234     book = get_object_or_404(Book, slug=slug)
235     if not book.accessible(request):
236         return HttpResponseForbidden("Not authorized.")
237     xml = book.materialize()
238     output = StringIO()
239     # errors?
240
241     import librarian.html
242     librarian.html.transform(StringIO(xml), output, parse_dublincore=False,
243                              flags=['full-page'])
244     html = output.getvalue()
245     response = http.HttpResponse(html, content_type='text/html', mimetype='text/html')
246     return response
247
248
249 @never_cache
250 def book_pdf(request, slug):
251     book = get_object_or_404(Book, slug=slug)
252     if not book.accessible(request):
253         return HttpResponseForbidden("Not authorized.")
254
255     from tempfile import NamedTemporaryFile
256     from os import unlink
257     from librarian import pdf
258     from catalogue.ebook_utils import RedakcjaDocProvider, serve_file
259
260     xml = book.materialize()
261     xml_file = NamedTemporaryFile()
262     xml_file.write(xml.encode('utf-8'))
263     xml_file.flush()
264
265     try:
266         pdf_file = NamedTemporaryFile(delete=False)
267         pdf.transform(RedakcjaDocProvider(publishable=True),
268                   file_path=xml_file.name,
269                   output_file=pdf_file,
270                   )
271         return serve_file(pdf_file.name, book.slug + '.pdf', 'application/pdf')
272     finally:
273         unlink(pdf_file.name)
274
275
276 @never_cache
277 def book_epub(request, slug):
278     book = get_object_or_404(Book, slug=slug)
279     if not book.accessible(request):
280         return HttpResponseForbidden("Not authorized.")
281
282     from StringIO import StringIO
283     from tempfile import NamedTemporaryFile
284     from librarian import epub
285     from catalogue.ebook_utils import RedakcjaDocProvider
286
287     xml = book.materialize()
288     xml_file = NamedTemporaryFile()
289     xml_file.write(xml.encode('utf-8'))
290     xml_file.flush()
291
292     epub_file = StringIO()
293     epub.transform(RedakcjaDocProvider(publishable=True),
294             file_path=xml_file.name,
295             output_file=epub_file)
296     response = HttpResponse(mimetype='application/epub+zip')
297     response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
298     response.write(epub_file.getvalue())
299     return response
300
301
302 @never_cache
303 def revision(request, slug, chunk=None):
304     try:
305         doc = Chunk.get(slug, chunk)
306     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
307         raise Http404
308     if not doc.book.accessible(request):
309         return HttpResponseForbidden("Not authorized.")
310     return http.HttpResponse(str(doc.revision()))
311
312
313 def book(request, slug):
314     book = get_object_or_404(Book, slug=slug)
315     if not book.accessible(request):
316         return HttpResponseForbidden("Not authorized.")
317
318     if request.user.has_perm('catalogue.change_book'):
319         if request.method == "POST":
320             form = forms.BookForm(request.POST, instance=book)
321             if form.is_valid():
322                 form.save()
323                 return http.HttpResponseRedirect(book.get_absolute_url())
324         else:
325             form = forms.BookForm(instance=book)
326             editable = True
327     else:
328         form = forms.ReadonlyBookForm(instance=book)
329         editable = False
330
331     publish_error = publishable_error(book)
332     publishable = publish_error is None
333
334     return direct_to_template(request, "catalogue/book_detail.html", extra_context={
335         "book": book,
336         "publishable": publishable,
337         "publishable_error": publish_error,
338         "form": form,
339         "editable": editable,
340     })
341
342
343 def image(request, slug):
344     image = get_object_or_404(Image, slug=slug)
345     if not image.accessible(request):
346         return HttpResponseForbidden("Not authorized.")
347
348     if request.user.has_perm('catalogue.change_image'):
349         if request.method == "POST":
350             form = forms.ImageForm(request.POST, instance=image)
351             if form.is_valid():
352                 form.save()
353                 return http.HttpResponseRedirect(image.get_absolute_url())
354         else:
355             form = forms.ImageForm(instance=image)
356             editable = True
357     else:
358         form = forms.ReadonlyImageForm(instance=image)
359         editable = False
360
361     publish_error = publishable_error(image)
362     publishable = publish_error is None
363
364     return direct_to_template(request, "catalogue/image_detail.html", extra_context={
365         "object": image,
366         "publishable": publishable,
367         "publishable_error": publish_error,
368         "form": form,
369         "editable": editable,
370     })
371
372
373 @permission_required('catalogue.add_chunk')
374 def chunk_add(request, slug, chunk):
375     try:
376         doc = Chunk.get(slug, chunk)
377     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
378         raise Http404
379     if not doc.book.accessible(request):
380         return HttpResponseForbidden("Not authorized.")
381
382     if request.method == "POST":
383         form = forms.ChunkAddForm(request.POST, instance=doc)
384         if form.is_valid():
385             if request.user.is_authenticated():
386                 creator = request.user
387             else:
388                 creator = None
389             doc.split(creator=creator,
390                 slug=form.cleaned_data['slug'],
391                 title=form.cleaned_data['title'],
392                 gallery_start=form.cleaned_data['gallery_start'],
393                 user=form.cleaned_data['user'],
394                 stage=form.cleaned_data['stage']
395             )
396
397             return http.HttpResponseRedirect(doc.book.get_absolute_url())
398     else:
399         form = forms.ChunkAddForm(initial={
400                 "slug": str(doc.number + 1),
401                 "title": "cz. %d" % (doc.number + 1, ),
402         })
403
404     return direct_to_template(request, "catalogue/chunk_add.html", extra_context={
405         "chunk": doc,
406         "form": form,
407     })
408
409
410 def chunk_edit(request, slug, chunk):
411     try:
412         doc = Chunk.get(slug, chunk)
413     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
414         raise Http404
415     if not doc.book.accessible(request):
416         return HttpResponseForbidden("Not authorized.")
417
418     if request.method == "POST":
419         form = forms.ChunkForm(request.POST, instance=doc)
420         if form.is_valid():
421             form.save()
422             go_next = request.GET.get('next', None)
423             if go_next:
424                 go_next = urlquote_plus(unquote(iri_to_uri(go_next)), safe='/?=&')
425             else:
426                 go_next = doc.book.get_absolute_url()
427             return http.HttpResponseRedirect(go_next)
428     else:
429         form = forms.ChunkForm(instance=doc)
430
431     referer = request.META.get('HTTP_REFERER')
432     if referer:
433         parts = urlsplit(referer)
434         parts = ['', ''] + list(parts[2:])
435         go_next = urlquote_plus(urlunsplit(parts))
436     else:
437         go_next = ''
438
439     return direct_to_template(request, "catalogue/chunk_edit.html", extra_context={
440         "chunk": doc,
441         "form": form,
442         "go_next": go_next,
443     })
444
445
446 @permission_required('catalogue.change_book')
447 def book_append(request, slug):
448     book = get_object_or_404(Book, slug=slug)
449     if not book.accessible(request):
450         return HttpResponseForbidden("Not authorized.")
451
452     if request.method == "POST":
453         form = forms.BookAppendForm(book, request.POST)
454         if form.is_valid():
455             append_to = form.cleaned_data['append_to']
456             append_to.append(book)
457             return http.HttpResponseRedirect(append_to.get_absolute_url())
458     else:
459         form = forms.BookAppendForm(book)
460     return direct_to_template(request, "catalogue/book_append_to.html", extra_context={
461         "book": book,
462         "form": form,
463
464         "logout_to": '/',
465     })
466
467
468 @require_POST
469 @login_required
470 def publish(request, slug):
471     book = get_object_or_404(Book, slug=slug)
472     if not book.accessible(request):
473         return HttpResponseForbidden("Not authorized.")
474
475     try:
476         book.publish(request.user)
477     except NotAuthorizedError:
478         return http.HttpResponseRedirect(reverse('apiclient_oauth'))
479     except BaseException, e:
480         return http.HttpResponse(e)
481     else:
482         return http.HttpResponseRedirect(book.get_absolute_url())
483
484
485 @require_POST
486 @login_required
487 def publish_image(request, slug):
488     image = get_object_or_404(Image, slug=slug)
489     if not image.accessible(request):
490         return HttpResponseForbidden("Not authorized.")
491
492     try:
493         image.publish(request.user)
494     except NotAuthorizedError:
495         return http.HttpResponseRedirect(reverse('apiclient_oauth'))
496     except BaseException, e:
497         return http.HttpResponse(e)
498     else:
499         return http.HttpResponseRedirect(image.get_absolute_url())