]> git.parisson.com Git - telemeta.git/commitdiff
get first corpus epub generator (needs ebooklib)
authorGuillaume Pellerin <yomguy@parisson.com>
Mon, 23 Feb 2015 17:34:44 +0000 (18:34 +0100)
committerGuillaume Pellerin <yomguy@parisson.com>
Mon, 23 Feb 2015 17:34:44 +0000 (18:34 +0100)
telemeta/models/corpus.py
telemeta/static/telemeta/css/telemeta_epub.css [new file with mode: 0644]
telemeta/templates/telemeta/collection_epub.html [new file with mode: 0644]
telemeta/templates/telemeta/resource_detail.html
telemeta/urls.py
telemeta/views/collection.py
telemeta/views/resource.py

index 067b82a5571a7308c6dd54909d9190ab1553d041..a1f28f96f861637e2cd534c705831e045860fa21 100644 (file)
@@ -90,3 +90,4 @@ class MediaCorpusRelated(MediaRelated):
         verbose_name = _('corpus related media')
         verbose_name_plural = _('corpus related media')
 
+
diff --git a/telemeta/static/telemeta/css/telemeta_epub.css b/telemeta/static/telemeta/css/telemeta_epub.css
new file mode 100644 (file)
index 0000000..f4d3b69
--- /dev/null
@@ -0,0 +1,29 @@
+@namespace epub "http://www.idpf.org/2007/ops";
+
+body {
+    font-family: Cambria, Liberation Serif, Bitstream Vera Serif, Georgia, Times, Times New Roman, serif;
+}
+
+h2 {
+     text-align: left;
+     text-transform: uppercase;
+     font-weight: 200;
+}
+
+ol {
+        list-style-type: none;
+}
+
+ol > li:first-child {
+        margin-top: 0.3em;
+}
+
+
+nav[epub|type~='toc'] > ol > li > ol  {
+    list-style-type:square;
+}
+
+
+nav[epub|type~='toc'] > ol > li > ol > li {
+        margin-top: 0.3em;
+}
diff --git a/telemeta/templates/telemeta/collection_epub.html b/telemeta/templates/telemeta/collection_epub.html
new file mode 100644 (file)
index 0000000..0fa92b5
--- /dev/null
@@ -0,0 +1,8 @@
+
+{% for item in collection.items.all %}
+<p>
+ {{ item.title }}
+ <br>
+ <audio src="http://{{ site.domain }}{% url "telemeta-item-export" item.public_id 'mp3' %}" controls="controls"/>
+</p>
+{% endfor %}
index 69568be060c9468d884929798858b573220cd991..e80d37ac33c9aca9fe87cc0bae1d13e88e41fac1 100644 (file)
@@ -57,7 +57,13 @@ jQuery(document).ready(function(){
       </button>
     </a>
     {% endif %}
-
+    {% if type == 'corpus' %}
+    <a id="_add_to_playlist" href="{% url "telemeta-corpus-epub" resource.public_id %}">
+      <button type="button" class="btn btn-default">
+        <span class="glyphicon glyphicon-book"></span> {% trans "Epub" %}
+      </button>
+    </a>
+    {% endif %}
 </div>
 {% endblock %}
 
index 8550588b4ef74f1240eec4718968ce3c606f3a05..bd86ba0d014c8e546dfdc1ccc9bc9d32c7860d18 100644 (file)
@@ -135,6 +135,7 @@ urlpatterns = patterns('',
     url(r'^archives/(?P<type>[A-Za-z0-9._-]+)/(?P<public_id>[A-Za-z0-9._-]+)/delete/$', ResourceDeleteView.as_view(), name="telemeta-resource-delete"),
     url(r'^archives/(?P<type>[A-Za-z0-9._-]+)/(?P<public_id>[A-Za-z0-9._-]+)/related/(?P<media_id>[A-Za-z0-9._-]+)/view/$', resource_view.related_stream, name="telemeta-resource-related"),
     url(r'^archives/(?P<type>[A-Za-z0-9._-]+)/(?P<public_id>[A-Za-z0-9._-]+)/related/(?P<media_id>[A-Za-z0-9._-]+)/download/$', resource_view.related_download, name="telemeta-resource-related-download"),
+    url(r'^archives/corpus/(?P<public_id>[A-Za-z0-9._-]+)/epub/$', CorpusEpubView.as_view(), name="telemeta-corpus-epub"),
 
     # search
     # url(r'^archives/$', home_view.search, name="telemeta-archives"),
index a6dca61ea6bb07aaf204ea52e24b9334f71436dc..00dca63547abd8f3333f776003c3416b72cf27e7 100644 (file)
@@ -363,3 +363,4 @@ class CollectionCopyView(CollectionAddView):
     def dispatch(self, *args, **kwargs):
         return super(CollectionCopyView, self).dispatch(*args, **kwargs)
 
+
index dc84ccb00528e85acbaa37498879f13f633e5132..50feeb266c4bb4a55b4d0c5fc9747c9ed3521053 100644 (file)
@@ -348,3 +348,83 @@ class ResourceEditView(ResourceSingleMixin, UpdateWithInlinesView):
     def dispatch(self, *args, **kwargs):
         return super(ResourceEditView, self).dispatch(*args, **kwargs)
 
+
+
+class CorpusEpubView(View):
+
+    model = MediaCorpus
+
+    def get_object(self):
+        return MediaCorpus.objects.get(public_id=self.kwargs['public_id'])
+
+    def get(self, request, *args, **kwargs):
+        """
+        Stream an Epub file of collection data
+        """
+        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():
+            context = {'collection': collection, 'site': site}
+            c = epub.EpubHtml(title=collection.title, file_name=collection.code + '.xhtml', lang='fr')
+            c.content = render_to_string(collection_template, context)
+            print c.content
+            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())
+
+        # define 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
+        filename = '/tmp/test.epub'
+        epub.write_epub(filename, book, {})
+        epub_file = open(filename, 'r')
+
+        response = StreamingHttpResponse(epub_file.read(), content_type='application/epub+zip')
+        response['Content-Disposition'] = "attachment; filename=%s.%s" % \
+                                             (collection.code, 'epub')
+        return response
+
+    @method_decorator(login_required)
+    def dispatch(self, *args, **kwargs):
+        return super(CorpusEpubView, self).dispatch(*args, **kwargs)