]> git.parisson.com Git - telemeta.git/commitdiff
rearrange epub objects
authorGuillaume Pellerin <yomguy@parisson.com>
Thu, 21 May 2015 15:58:29 +0000 (17:58 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Thu, 21 May 2015 15:58:46 +0000 (17:58 +0200)
telemeta/static/telemeta/css/telemeta_epub.css
telemeta/templates/telemeta/collection_epub_urls.html [deleted file]
telemeta/templates/telemeta/inc/collection_epub.html [deleted file]
telemeta/templates/telemeta/inc/epub_collection.html [new file with mode: 0644]
telemeta/templates/telemeta/inc/epub_preamble.html [new file with mode: 0644]
telemeta/urls.py
telemeta/views/__init__.py
telemeta/views/collection.py
telemeta/views/core.py
telemeta/views/epub.py [new file with mode: 0644]
telemeta/views/resource.py

index c3fb4e5771e368e38f38ce1634320953a15c16a8..80304b4dbe684ca36a42723135ed5a0ef662a876 100644 (file)
@@ -6,18 +6,24 @@ body {
 
 h2 {
     text-align: left;
+    font-size: 1.5em;
     text-transform: uppercase;
     font-weight: 200;
 }
 
-
 h3 {
     text-align: left;
-    font-size: 0.8em;
+    font-size: 1.25em;
     margin-top: 0px;
     font-weight: normal;
 }
 
+p {
+    text-align: left;
+    font-size: 0.9em;
+    margin-top: 0px;
+    font-weight: normal;
+}
 
 ol {
     list-style-type: none;
@@ -27,28 +33,22 @@ 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;
 }
 
 .item {
-    page-break-before: always;
+    page-break-after: always;
 }
 
 .item-image {
     text-align: center;
 }
 
-.item-audio{
-    page-break-after: avoid;
-}
-
 .item-player {
     width: 100%;
 }
diff --git a/telemeta/templates/telemeta/collection_epub_urls.html b/telemeta/templates/telemeta/collection_epub_urls.html
deleted file mode 100644 (file)
index 0fa92b5..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-
-{% 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 %}
diff --git a/telemeta/templates/telemeta/inc/collection_epub.html b/telemeta/templates/telemeta/inc/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 %}
diff --git a/telemeta/templates/telemeta/inc/epub_collection.html b/telemeta/templates/telemeta/inc/epub_collection.html
new file mode 100644 (file)
index 0000000..8df56ca
--- /dev/null
@@ -0,0 +1,25 @@
+<link rel="stylesheet" type="text/css" href="style/epub.css" />
+
+<h2>{{ title }}</h2>
+<h3>{{ subtitle }}</h3>
+
+{% for item in items %}
+<div class="item">
+  <p>
+  <b>Son {{ item.old_code }}</b> : {{ item.title }}. {{ item.comment }} ({{ item.track }})
+  </p>
+
+  <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 %}
diff --git a/telemeta/templates/telemeta/inc/epub_preamble.html b/telemeta/templates/telemeta/inc/epub_preamble.html
new file mode 100644 (file)
index 0000000..d901b11
--- /dev/null
@@ -0,0 +1,24 @@
+{% load telemeta_utils %}
+{% load i18n %}
+
+<link rel="stylesheet" type="text/css" href="style/epub.css" />
+
+<h2>{% trans 'Preamble' %}</h2>
+
+<p>Cet AudioBook est destiné à la lecture des sons présenté dans le livre "Écoute musicale et acoustique" de Michèle Castellengo édité aux éditions Eyrolles.</p>
+
+<p>
+Conception et réalisation: Eyrolles / Parisson
+</p>
+
+<p>
+Copyright (C) 2015 Michèle Castellengo<br/>
+Édition (E) 2015 Eyrolles<br/>
+Publication (P) 2015 Parisson<br/>
+</p>
+
+<!--
+<p>
+Cette oeuvre, création, site ou texte est sous licence Creative Commons Attribution - Pas d'Utilisation Commerciale - Pas de Modification 4.0 International. Pour accéder à une copie de cette licence, merci de vous rendre à l'adresse suivante http://creativecommons.org/licenses/by-nc-nd/4.0/ ou envoyez un courrier à Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
+</p>
+-->
index e26ae6103967c18fd8f0bfa38bf465c2abbbd44c..59e18d066bac88fe980db6b6cdd1494d2b4ca257 100644 (file)
@@ -40,9 +40,6 @@ from django.views.generic.base import RedirectView
 from django.views.generic.list import ListView
 from telemeta.models import MediaItem, MediaCollection, MediaItemMarker, MediaCorpus, MediaFonds
 from telemeta.views import *
-#from telemeta.views import HomeView, AdminView, CollectionView, ItemView, \
-#                            InstrumentView, InstrumentAliasView, PlaylistView, ProfileView, GeoView, \
-#                            LastestRevisionsFeed, ResourceView, UserRevisionsFeed, CollectionPackageView
 from jsonrpc import jsonrpc_site
 import os.path
 import telemeta.config
index 4a57ae37a24a76911b3b38acf4acc40e786f3d83..81519b5a8bca4c44d6dda19688d8075c950dd436 100644 (file)
@@ -43,4 +43,5 @@ from telemeta.views.playlist import *
 from telemeta.views.profile import *
 from telemeta.views.feed import *
 from telemeta.views.resource import *
+from telemeta.views.epub import *
 
index 9e47888b81e663543c6c05724eb9c5b3b36002d5..614e0a7bff0a5010bd0b532c42faa3add5ebe300 100644 (file)
@@ -372,24 +372,3 @@ 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.filename + '.epub'
-        return response
-
-    @method_decorator(login_required)
-    def dispatch(self, *args, **kwargs):
-        return super(CollectionEpubView, self).dispatch(*args, **kwargs)
-
index 9544be1ff75f2d888ac8b48573caadc666fee939..ef682f2291299ceb80f7217eb73e950791dc1d14 100644 (file)
@@ -305,102 +305,8 @@ def get_kwargs_or_none(key, kwargs):
         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 = []
-
-        if not collection:
-            self.filename = self.corpus.code
-            self.book.set_title(corpus.title)
-        else:
-            self.filename = collection.code
-            self.book.set_title(corpus.title + ' - ' + collection.title)
-
-        self.path = self.cache_data.dir + os.sep + self.filename + '.epub'
-
-        # add metadata
-        self.book.set_identifier(corpus.public_id)
-        #self.book.set_title(corpus.title + ' - ' + collection.title)
-        self.book.set_language('fr')
-        self.book.add_author(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.png", 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, {})
-
+def cleanup_path(path):
+    new_path = []
+    for dir in path.split(os.sep):
+        new_path.append(slugify(dir))
+    return os.sep.join(new_path)
diff --git a/telemeta/views/epub.py b/telemeta/views/epub.py
new file mode 100644 (file)
index 0000000..bf64c0b
--- /dev/null
@@ -0,0 +1,203 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2015 Parisson SARL
+
+# This software is a computer program whose purpose is to backup, analyse,
+# transcode and stream any audio content with its metadata over a web frontend.
+
+# This software is governed by the CeCILL  license under French law and
+# abiding by the rules of distribution of free software.  You can  use,
+# modify and/ or redistribute the software under the terms of the CeCILL
+# license as circulated by CEA, CNRS and INRIA at the following URL
+# "http://www.cecill.info".
+
+# As a counterpart to the access to the source code and  rights to copy,
+# modify and redistribute granted by the license, users are provided only
+# with a limited warranty  and the software's author,  the holder of the
+# economic rights,  and the successive licensors  have only  limited
+# liability.
+
+# In this respect, the user's attention is drawn to the risks associated
+# with loading,  using,  modifying and/or developing or reproducing the
+# software by the user in light of its specific status of free software,
+# that may mean  that it is complicated to manipulate,  and  that  also
+# therefore means  that it is reserved for developers  and  experienced
+# professionals having in-depth computer knowledge. Users are therefore
+# encouraged to load and test the software's suitability as regards their
+# requirements in conditions enabling the security of their systems and/or
+# data to be ensured and,  more generally, to use and operate it in the
+# same conditions as regards security.
+
+# The fact that you are presently reading this means that you have had
+# knowledge of the CeCILL license and that you accept its terms.
+
+# Authors: Guillaume Pellerin <yomguy@parisson.com>
+
+
+from telemeta.views.core import *
+from telemeta.models import *
+from collections import OrderedDict
+from ebooklib import epub
+from django.template.loader import render_to_string
+
+
+class BaseEpubMixin(TelemetaBaseMixin):
+    "Download corpus data embedded in an EPUB3 file"
+
+    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', 'epub_collection.html'])
+    template_preamble = os.sep.join([local_path, '..', 'templates', 'telemeta', 'inc', 'epub_preamble.html'])
+
+    def write_book(self, corpus, collection=None, path=None):
+        self.book = epub.EpubBook()
+        self.corpus = corpus
+        site = Site.objects.get_current()
+        self.chapters = []
+
+        if not collection:
+            self.filename = self.corpus.code
+            self.book.set_title(corpus.title)
+        else:
+            self.filename = collection.code
+            self.book.set_title(corpus.title + ' - ' + collection.title)
+
+        self.path = self.cache_data.dir + os.sep + self.filename + '.epub'
+
+        # add metadata
+        self.book.set_identifier(corpus.public_id)
+        #self.book.set_title(corpus.title + ' - ' + collection.title)
+        self.book.set_language('fr')
+        self.book.add_author(corpus.descriptions)
+
+        # add css style
+        style = open(self.css, 'r')
+        css = epub.EpubItem(uid="style_nav", file_name="style/epub.css", media_type="text/css", content=style.read())
+        self.book.add_item(css)
+
+        # add cover image
+        for media in self.corpus.related.all():
+            if 'cover' in media.title or 'Cover' in media.title:
+                filename = os.path.split(media.file.path)[-1]
+                self.book.set_cover(filename, open(media.file.path, 'r').read())
+                break
+
+        context = {}
+        preamble = epub.EpubHtml(title='Preamble', file_name='preamble' + '.xhtml', lang='fr')
+        preamble.content = render_to_string(self.template_preamble, context)
+        self.book.add_item(preamble)
+        self.chapters.append(preamble)
+
+        if collection:
+            self.collections = [collection]
+            mode_single = True
+        else:
+            self.collections = self.corpus.children.all()
+            mode_single = False
+
+        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)
+
+            title_split = collection.title.split(' - ')
+            if len(title_split) > 1:
+                if '0' in title_split[0]:
+                    title = title_split[1]
+                    subtitle = ''
+                else:
+                    title = title_split[0]
+                    subtitle = title_split[1]
+            else:
+                title = collection.title
+                subtitle = ''
+
+            context = {'collection': collection, 'title': title, 'subtitle': subtitle,
+                        'site': site, 'items': items}
+            c = epub.EpubHtml(title=' - '.join([title, subtitle]), 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 chapters
+        self.book.toc = (( self.chapters ))
+
+        # add navigation files
+        self.book.add_item(epub.EpubNcx())
+        if not mode_single:
+            self.book.add_item(epub.EpubNav())
+            self.chapters.insert(0,'nav')
+
+        # create spin, add cover page as first page
+        self.chapters.insert(0,'cover')
+        self.book.spine = self.chapters
+
+        # write epub file
+        epub.write_epub(self.path, self.book, {})
+
+
+class CorpusEpubView(BaseEpubMixin, View):
+    "Download corpus data embedded in an EPUB3 file"
+
+    model = MediaCorpus
+
+    def get_object(self):
+        return MediaCorpus.objects.get(public_id=self.kwargs['public_id'])
+
+    def get(self, request, *args, **kwargs):
+        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" % self.filename + '.epub'
+        return response
+
+    @method_decorator(login_required)
+    def dispatch(self, *args, **kwargs):
+        return super(CorpusEpubView, 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.filename + '.epub'
+        return response
+
+    @method_decorator(login_required)
+    def dispatch(self, *args, **kwargs):
+        return super(CollectionEpubView, self).dispatch(*args, **kwargs)
+
index 20bb43ce1dcff4795b5b0c84d9962c83279dc7de..8cefcea7fdfe948535da2249633587e183836d6e 100644 (file)
@@ -342,30 +342,3 @@ class ResourceEditView(ResourceSingleMixin, UpdateWithInlinesView):
     def dispatch(self, *args, **kwargs):
         return super(ResourceEditView, self).dispatch(*args, **kwargs)
 
-
-def cleanup_path(path):
-    new_path = []
-    for dir in path.split(os.sep):
-        new_path.append(slugify(dir))
-    return os.sep.join(new_path)
-
-
-class CorpusEpubView(BaseEpubMixin, View):
-    "Download corpus data embedded in an EPUB3 file"
-
-    model = MediaCorpus
-
-    def get_object(self):
-        return MediaCorpus.objects.get(public_id=self.kwargs['public_id'])
-
-    def get(self, request, *args, **kwargs):
-        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" % self.filename + '.epub'
-        return response
-
-    @method_decorator(login_required)
-    def dispatch(self, *args, **kwargs):
-        return super(CorpusEpubView, self).dispatch(*args, **kwargs)
-