fix
[redakcja.git] / src / documents / views.py
index a5b9971..905e885 100644 (file)
@@ -23,7 +23,8 @@ from django.utils.translation import gettext_lazy as _
 from django.views.decorators.http import require_POST
 from django_cas_ng.decorators import user_passes_test
 
-from apiclient import NotAuthorizedError
+from librarian import epubcheck
+from apiclient import api_call, NotAuthorizedError
 from . import forms
 from . import helpers
 from .helpers import active_tab
@@ -65,9 +66,17 @@ def my(request):
         key=lambda x: x[1]['time'], reverse=True)
     for k, v in last_books:
         v['time'] = datetime.fromtimestamp(v['time'])
+    try:
+        resp = api_call(request.user, 'username/')
+    except NotAuthorizedError:
+        wllogin = None
+    else:
+        wllogin = resp['username']
+
     return render(request, 'documents/my_page.html', {
         'last_books': last_books,
         "logout_to": '/',
+        "wllogin": wllogin,
         })
 
 
@@ -262,6 +271,7 @@ def book_html(request, slug):
     return render(request, 'documents/book_text.html', locals())
 
 
+@login_required
 @never_cache
 def book_pdf(request, slug, mobile=False):
     book = get_object_or_404(Book, slug=slug)
@@ -278,6 +288,7 @@ def book_pdf(request, slug, mobile=False):
                 book.slug + '.pdf', 'application/pdf')
 
 
+@login_required
 @never_cache
 def book_epub(request, slug):
     book = get_object_or_404(Book, slug=slug)
@@ -285,17 +296,50 @@ def book_epub(request, slug):
         return HttpResponseForbidden("Not authorized.")
 
     # TODO: move to celery
-    doc = book.wldocument()
+    doc = book.wldocument(librarian2=True)
     # TODO: error handling
 
-    #### Problemas: images in children.
-    epub = doc.as_epub(base_url='file://' + book.gallery_path() + '/').get_bytes()
+    from librarian.builders import EpubBuilder
+    epub = EpubBuilder(
+        base_url='file://' + book.gallery_path() + '/',
+        debug=True
+    ).build(doc).get_bytes()
     response = HttpResponse(content_type='application/epub+zip')
     response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
     response.write(epub)
     return response
 
 
+@login_required
+@never_cache
+def book_epubcheck(request, slug):
+    book = get_object_or_404(Book, slug=slug)
+    if not book.accessible(request):
+        return HttpResponseForbidden("Not authorized.")
+
+    # TODO: move to celery
+    doc = book.wldocument(librarian2=True)
+    # TODO: error handling
+
+    from librarian.builders import EpubBuilder
+    epub = EpubBuilder(
+        base_url='file://' + book.gallery_path() + '/',
+        debug=True
+    ).build(doc)
+    fname = epub.get_filename()
+
+    messages = epubcheck.epubcheck(fname)
+    for message in messages:
+        for location in message.get('locations', []):
+            if 'wl_chunk' in location:
+                location['wl_chunk'] = book[location['wl_chunk']]
+    return render(request, 'documents/book_epubcheck.html', {
+        'messages': messages,
+        'book': book,
+    })
+
+
+@login_required
 @never_cache
 def book_mobi(request, slug):
     book = get_object_or_404(Book, slug=slug)
@@ -303,9 +347,12 @@ def book_mobi(request, slug):
         return HttpResponseForbidden("Not authorized.")
 
     # TODO: move to celery
-    doc = book.wldocument()
+    doc = book.wldocument(librarian2=True)
     # TODO: error handling
-    mobi = doc.as_mobi(base_url='file://' + book.gallery_path() + '/').get_bytes()
+    from librarian.builders import MobiBuilder
+    mobi = MobiBuilder(
+        base_url='file://' + book.gallery_path() + '/'
+    ).build(doc).get_bytes()
     response = HttpResponse(content_type='application/x-mobipocket-ebook')
     response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.mobi'
     response.write(mobi)
@@ -346,14 +393,21 @@ def book(request, slug):
     publish_error = book.publishable_error()
     publishable = publish_error is None
 
+    stats = None
     try:
-        doc = book.wldocument()
+        doc = book.wldocument(librarian2=True)
     except:
         doc = None
-    
+    else:
+        try:
+            stats = doc.get_statistics()
+        except:
+            pass
+
     return render(request, "documents/book_detail.html", {
         "book": book,
         "doc": doc,
+        "stats": stats,
         "publishable": publishable,
         "publishable_error": publish_error,
         "form": form,