]> git.parisson.com Git - telemeta.git/commitdiff
add EPUB3 collection downloader
authorGuillaume Pellerin <yomguy@parisson.com>
Wed, 6 May 2015 22:48:42 +0000 (00:48 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Wed, 6 May 2015 22:49:17 +0000 (00:49 +0200)
telemeta/templates/telemeta/collection_detail.html
telemeta/templates/telemeta/collection_epub.html [deleted file]
telemeta/urls.py
telemeta/views/collection.py
telemeta/views/core.py
telemeta/views/resource.py

index d78d03af19f9e28edb903fe7fb4a029e8a7fea48..c9804fef0d917fc475dbf24a5f3eac360c5aef19 100644 (file)
       </button>
     </a>
     {% if audio_export_enabled or perms.telemeta.can_download_all_items or user.is_superuser %}
-    <a href="{% url "telemeta-collection-package" collection.public_id %}" id="zip_package">
+    <a href="{% url "telemeta-collection-zip" collection.public_id %}" id="zip_package">
       <button type="button" class="btn btn-default">
         <span class="glyphicon glyphicon-compressed"></span> {% trans "Zip" %}
       </button>
     </a>
     {% endif %}
+    {% if user.is_superuser %}
+    <a id="_export_epub" href="{% url "telemeta-collection-epub" collection.public_id %}">
+      <button type="button" class="btn btn-default">
+        <span class="glyphicon glyphicon-book"></span> {% trans "Epub" %}
+      </button>
+    </a>
+    {% endif %}
     {% endif %}
    </div>
 {% endblock %}
diff --git a/telemeta/templates/telemeta/collection_epub.html b/telemeta/templates/telemeta/collection_epub.html
deleted file mode 100644 (file)
index 3702024..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-<link rel="stylesheet" type="text/css" href="style/nav.css" />
-
-<h2>{{ collection.title }}</h2>
-
-{% for item in items %}
-<div class="item">
-  <h3>
-  <b>Son {{ item.old_code }}</b> : {{ item.title }}. {{ item.comment }} ({{ item.track }})
-  </h3>
-
-  <div class="item-audio">
-   <audio class="item-player" src="{{ item.file }}" controls="controls"></audio>
-  </div>
-
-  {% for image in item.related.all %}
-    {% if 'image' in image.mime_type %}
-       <div class="item-image">
-        <img src="{{ image.file }}"/>
-       </div>
-    {% endif %}
-   {% endfor %}
-
-</div>
-{% endfor %}
index 59cc23af513c4fdf0d3a2519b104bd3c5d980000..7ebc6a2dad634636ef40a1fc82fd50ccef36e8ba 100644 (file)
@@ -122,8 +122,10 @@ urlpatterns = patterns('',
 
     # FIXME: need all paths
     url(r'^collections/(?P<path>[A-Za-z0-9._-s/]+)/$', RedirectView.as_view(), {'url': '/archives/collections/%(path)s/', 'permanent': False}, name="telemeta-collection-redir"),
-    url(r'^archives/collections/(?P<public_id>[A-Za-z0-9._-]+)/package/$', CollectionPackageView.as_view(),
-        name="telemeta-collection-package"),
+    url(r'^archives/collections/(?P<public_id>[A-Za-z0-9._-]+)/zip/$', CollectionZipView.as_view(),
+        name="telemeta-collection-zip"),
+    url(r'^archives/collections/(?P<public_id>[A-Za-z0-9._-]+)/epub/$', CollectionEpubView.as_view(),
+        name="telemeta-collection-epub"),
 
     # Generic resources
     url(r'^archives/(?P<type>[A-Za-z0-9._-]+)/$', ResourceListView.as_view(), name="telemeta-resource-list"),
index 3365c4609737b59de50aec9a296e50eea123ed1c..643b1a1d8d6ae10045b90caf93c9193ff75f8596 100644 (file)
@@ -172,7 +172,7 @@ class CollectionView(object):
         return render(request, template, {'collection': collection, 'formset': formset,})
 
 
-class CollectionPackageView(View):
+class CollectionZipView(View):
 
     model = MediaCollection
 
@@ -230,7 +230,7 @@ class CollectionPackageView(View):
 
     @method_decorator(login_required)
     def dispatch(self, *args, **kwargs):
-        return super(CollectionPackageView, self).dispatch(*args, **kwargs)
+        return super(CollectionZipView, self).dispatch(*args, **kwargs)
 
 
 class CollectionViewMixin(object):
@@ -372,3 +372,24 @@ class CollectionCopyView(CollectionAddView):
         return super(CollectionCopyView, self).dispatch(*args, **kwargs)
 
 
+class CollectionEpubView(BaseEpubMixin, View):
+    "Download collection data embedded in an EPUB3 file"
+
+    model = MediaCollection
+
+    def get_object(self):
+        return MediaCollection.objects.get(public_id=self.kwargs['public_id'])
+
+    def get(self, request, *args, **kwargs):
+        collection = self.get_object()
+        corpus = collection.corpus.all()[0]
+        self.write_book(corpus, collection=collection)
+        epub_file = open(self.path, 'rb')
+        response = HttpResponse(epub_file.read(), content_type='application/epub+zip')
+        response['Content-Disposition'] = "attachment; filename=%s" % self.name
+        return response
+
+    @method_decorator(login_required)
+    def dispatch(self, *args, **kwargs):
+        return super(CollectionEpubView, self).dispatch(*args, **kwargs)
+
index 28fccc2fc25bdbd21fe8091b1f3de5ce5cc01e8a..26486c2d8fc371dc4443c4b8144d7fb0976cb332 100644 (file)
@@ -304,3 +304,96 @@ def get_kwargs_or_none(key, kwargs):
     else:
         return None
 
+
+class BaseEpubMixin(TelemetaBaseMixin):
+    "Download corpus data embedded in an EPUB3 file"
+
+    abstract = True
+    local_path = os.path.dirname(__file__)
+    css = os.sep.join([local_path, '..', 'static', 'telemeta', 'css', 'telemeta_epub.css'])
+    template = os.sep.join([local_path, '..', 'templates', 'telemeta', 'inc', 'collection_epub.html'])
+
+    def write_book(self, corpus, collection=None, path=None):
+        from collections import OrderedDict
+        from ebooklib import epub
+        from django.template.loader import render_to_string
+
+        self.book = epub.EpubBook()
+        self.corpus = corpus
+        site = Site.objects.get_current()
+        self.chapters = []
+        self.name = self.corpus.code + '.epub'
+        self.path = self.cache_data.dir + os.sep + self.name
+
+        # add metadata
+        self.book.set_identifier(self.corpus.public_id)
+        self.book.set_title(self.corpus.title)
+        self.book.set_language('fr')
+        self.book.add_author(self.corpus.descriptions)
+
+        # add cover image
+        for media in self.corpus.related.all():
+            if 'cover' in media.title or 'Cover' in media.title:
+                self.book.set_cover("cover.jpg", open(media.file.path, 'r').read())
+                break
+
+        if collection:
+            self.collections = [collection]
+        else:
+            self.collections = self.corpus.children.all()
+
+        for collection in self.collections:
+            items = {}
+            for item in collection.items.all():
+                if '.' in item.old_code:
+                    id = item.old_code.split('.')[1]
+                else:
+                    id = item.old_code
+                for c in id:
+                    if c.isalpha():
+                        id = id.replace(c, '.' + str(ord(c)-96))
+                items[item] = float(id)
+            items = OrderedDict(sorted(items.items(), key=lambda t: t[1]))
+
+            for item in items:
+                if item.file:
+                    audio = open(item.file.path, 'r')
+                    filename = str(item.file)
+                    epub_item = epub.EpubItem(file_name=str(item.file), content=audio.read())
+                    self.book.add_item(epub_item)
+                for related in item.related.all():
+                    if 'image' in related.mime_type:
+                        image = open(related.file.path, 'r')
+                        epub_item = epub.EpubItem(file_name=str(related.file), content=image.read())
+                        self.book.add_item(epub_item)
+            context = {'collection': collection, 'site': site, 'items': items}
+            c = epub.EpubHtml(title=collection.title, file_name=collection.code + '.xhtml', lang='fr')
+            c.content = render_to_string(self.template, context)
+            self.chapters.append(c)
+            # add self.chapters to the self.book
+            self.book.add_item(c)
+
+        # create table of contents
+        # - add manual link
+        # - add section
+        # - add auto created links to chaptersfesse
+
+        self.book.toc = (( self.chapters ))
+
+        # add navigation files
+        self.book.add_item(epub.EpubNcx())
+        self.book.add_item(epub.EpubNav())
+
+        # add css style
+        style = open(self.css, 'r')
+        nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style.read())
+        self.book.add_item(nav_css)
+
+        # create spin, add cover page as first page
+        self.chapters.insert(0,'nav')
+        self.chapters.insert(0,'cover')
+        self.book.spine = self.chapters
+
+        # write epub file
+        epub.write_epub(self.path, self.book, {})
+
index 1230fcee49dc5aea06b5a687f7094acc4ad56762..bda4bc6aa0ca3ea9dfc55333e807636b431eb1a7 100644 (file)
@@ -356,7 +356,7 @@ def cleanup_path(path):
     return os.sep.join(new_path)
 
 
-class CorpusEpubView(TelemetaBaseMixin, View):
+class CorpusEpubView(BaseEpubMixin, View):
     "Download corpus data embedded in an EPUB3 file"
 
     model = MediaCorpus
@@ -365,91 +365,10 @@ class CorpusEpubView(TelemetaBaseMixin, View):
         return MediaCorpus.objects.get(public_id=self.kwargs['public_id'])
 
     def get(self, request, *args, **kwargs):
-        from collections import OrderedDict
-        from ebooklib import epub
-        from django.template.loader import render_to_string
-
-        book = epub.EpubBook()
-        corpus = self.get_object()
-        local_path = os.path.dirname(__file__)
-        css = os.sep.join([local_path, '..', 'static', 'telemeta', 'css', 'telemeta_epub.css'])
-        collection_template = os.sep.join([local_path, '..', 'templates', 'telemeta', 'collection_epub.html'])
-        site = Site.objects.get_current()
-
-        # add metadata
-        book.set_identifier(corpus.public_id)
-        book.set_title(corpus.title)
-        book.set_language('fr')
-        book.add_author(corpus.descriptions)
-
-        # add cover image
-        for media in corpus.related.all():
-            if 'cover' in media.title or 'Cover' in media.title:
-                book.set_cover("cover.jpg", open(media.file.path, 'r').read())
-                break
-
-        chapters = []
-        for collection in corpus.children.all():
-            items = {}
-            for item in collection.items.all():
-                if '.' in item.old_code:
-                    id = item.old_code.split('.')[1]
-                else:
-                    id = item.old_code
-                for c in id:
-                    if c.isalpha():
-                       id = id.replace(c, '.' + str(ord(c)-96))
-                items[item] = float(id)
-            items = OrderedDict(sorted(items.items(), key=lambda t: t[1]))
-
-            for item in items:
-                if item.file:
-                    audio = open(item.file.path, 'r')
-                    filename = str(item.file)
-                    epub_item = epub.EpubItem(file_name=str(item.file), content=audio.read())
-                    book.add_item(epub_item)
-                for related in item.related.all():
-                    if 'image' in related.mime_type:
-                        image = open(related.file.path, 'r')
-                        epub_item = epub.EpubItem(file_name=str(related.file), content=image.read())
-                        book.add_item(epub_item)
-            context = {'collection': collection, 'site': site, 'items': items}
-            c = epub.EpubHtml(title=collection.title, file_name=collection.code + '.xhtml', lang='fr')
-            c.content = render_to_string(collection_template, context)
-            chapters.append(c)
-            # add chapters to the book
-            book.add_item(c)
-
-        # create table of contents
-        # - add manual link
-        # - add section
-        # - add auto created links to chaptersfesse
-
-        book.toc = (( chapters ))
-
-        # add navigation files
-        book.add_item(epub.EpubNcx())
-        book.add_item(epub.EpubNav())
-
-        # add css style
-        style = open(css, 'r')
-        nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style.read())
-        book.add_item(nav_css)
-
-        # create spin, add cover page as first page
-        chapters.insert(0,'nav')
-        chapters.insert(0,'cover')
-        book.spine = chapters
-
-        # create epub file
-        epub_name = corpus.code + '.epub'
-        path = self.cache_data.dir + os.sep + epub_name
-        epub.write_epub(path, book, {})
-        epub_file = open(path, 'rb')
-
+        self.write_book(self.get_object())
+        epub_file = open(self.path, 'rb')
         response = HttpResponse(epub_file.read(), content_type='application/epub+zip')
-        response['Content-Disposition'] = "attachment; filename=%s" % epub_name
-
+        response['Content-Disposition'] = "attachment; filename=%s" % self.name
         return response
 
     @method_decorator(login_required)