]> git.parisson.com Git - telemeta.git/commitdiff
epub: add path setup
authorGuillaume Pellerin <yomguy@parisson.com>
Thu, 23 Jul 2015 16:10:50 +0000 (18:10 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Thu, 23 Jul 2015 16:10:50 +0000 (18:10 +0200)
telemeta/templates/telemeta/resource_epub_password.html
telemeta/views/collection.py
telemeta/views/epub.py
telemeta/views/resource.py

index 6614059b5f53a58a811547cef0aef3270d1023b9..98314c8a1b32fb36cdce8c6e0511a7159b05d8d0 100644 (file)
@@ -20,7 +20,7 @@ $(document).ready(function() {
 
 
 <div class="intro">
-{% trans "Pour télécharger les livrets de sonores au format EPUB3, merci de fournir le premier mot de la page 411 du livre." %}
+{% trans "Pour télécharger les livrets-sons au format EPUB3, merci de fournir le premier mot de la page 411 du livre." %}
 </div>
 
 <div style="background-color: white; align: center; padding: 1em;">
index bd950a74a798cd814521d11cbc6dfceacdc024d8..6833557e917f921a63e93eba9bda5bdfbc5b780b 100644 (file)
@@ -384,7 +384,9 @@ class CollectionEpubView(BaseEpubMixin, View):
     def get(self, request, *args, **kwargs):
         collection = self.get_object()
         corpus = collection.corpus.all()[0]
-        self.write_book(corpus, collection=collection)
+        self.setup_epub(corpus, collection=collection)
+        if not os.path.exists(self.path):
+            self.write_book()
         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'
index 76e21d300d883414831dbc09c58a757be2e98aa4..05f0c9541c26e22d4cdacfca9c50ab7192dd9767 100644 (file)
@@ -52,17 +52,18 @@ class BaseEpubMixin(TelemetaBaseMixin):
     template_preamble = os.sep.join([local_path, '..', 'templates', 'telemeta', 'inc', 'epub_preamble.html'])
     template_cover = os.sep.join([local_path, '..', 'templates', 'telemeta', 'inc', 'epub_cover.html'])
 
-    def write_book(self, corpus, collection=None, path=None):
+    def setup_epub(self, corpus, collection=None, path=None):
         self.book = epub.EpubBook()
         self.corpus = corpus
-        site = Site.objects.get_current()
+        self.collection = collection
+        self.site = Site.objects.get_current()
         self.chapters = []
-        default_image_added = False
+        self.default_image_added = False
 
         if not collection:
             self.filename = corpus.code
             self.book.set_title(corpus.title)
-            full_title = corpus.title
+            self.full_title = corpus.title
         else:
             self.filename = collection.code
             short_title = collection.title.split(' ')
@@ -71,28 +72,31 @@ class BaseEpubMixin(TelemetaBaseMixin):
             else:
                 short_title = 'Intro'
             self.book.set_title(corpus.title[:15] + '... ' + short_title)
-            full_title = corpus.title + ' - ' + collection.title
+            self.full_title = corpus.title + ' - ' + collection.title
 
         self.path = self.cache_data.dir + os.sep + self.filename + '.epub'
+        return self.path
+
+    def write_book(self):
 
         # add metadata
-        self.book.set_identifier(corpus.public_id)
+        self.book.set_identifier(self.corpus.public_id)
         self.book.set_language('fr')
-        self.book.add_author(corpus.descriptions)
+        self.book.add_author(self.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)
 
-        if collection:
-            self.collections = [collection]
+        if self.collection:
+            self.collections = [self.collection]
             mode_single = True
-            instance = collection
-            if ' 0' in collection.title:
+            instance = self.collection
+            if ' 0' in self.collection.title:
                 chap_num = "d'introduction"
             else:
-                chap_num = collection.code.split('_')[-1]
+                chap_num = self.collection.code.split('_')[-1]
             context = {'title': 'chapitre ' + chap_num,
                         'mode_single': mode_single}
         else:
@@ -110,7 +114,7 @@ class BaseEpubMixin(TelemetaBaseMixin):
         preamble = epub.EpubHtml(title='Copyright', file_name='copyright' + '.xhtml', lang='fr')
         preamble.content = render_to_string(self.template_preamble, context)
         preamble.is_chapter = True
-        default_image_added = False
+        self.default_image_added = False
         default_image_relative_path = ''
         self.book.add_item(preamble)
         self.chapters.append(preamble)
@@ -149,13 +153,13 @@ class BaseEpubMixin(TelemetaBaseMixin):
                             image = open(related.file.path, 'r')
                             epub_item = epub.EpubItem(file_name=str(related.file), content=image.read())
                             self.book.add_item(epub_item)
-                elif not default_image_added:
+                elif not self.default_image_added:
                     image = open(self.default_image, 'r')
                     default_image_relative_path = 'images' + os.sep + os.path.split(self.default_image)[-1]
                     epub_item = epub.EpubItem(file_name=default_image_relative_path,
                                         content=image.read())
                     self.book.add_item(epub_item)
-                    default_image_added = True
+                    self.default_image_added = True
 
             title_split = collection.title.split(' - ')
             if len(title_split) > 1:
@@ -177,7 +181,7 @@ class BaseEpubMixin(TelemetaBaseMixin):
                 last_collection = True
 
             context = {'collection': collection, 'title': title, 'subtitle': subtitle, 'mode_single': mode_single,
-                        'site': site, 'items': items, 'default_image': default_image_relative_path,
+                        'site': self.site, 'items': items, 'default_image': default_image_relative_path,
                         'default_image_end': default_image_end_relative_path, 'last_collection': last_collection}
             c = epub.EpubHtml(title=chapter_title, file_name=collection.code + '.xhtml', lang='fr')
             c.content = render_to_string(self.template, context)
@@ -201,7 +205,7 @@ class BaseEpubMixin(TelemetaBaseMixin):
             self.book.spine.insert(0,'nav')
 
         # create spin, add cover page as first page
-        cover = epub.EpubHtml(title=full_title, file_name='cover-bis' + '.xhtml')
+        cover = epub.EpubHtml(title=self.full_title, file_name='cover-bis' + '.xhtml')
         cover.content = render_to_string(self.template_cover, {'image': cover_filename})
         self.book.add_item(cover)
         self.book.spine.insert(0, cover)
index e2e9375c66b77be865b0772f4c9b0ac652601043..a67e1780fcc3f0c6dc4b910f225edcd8074ac0f3 100644 (file)
@@ -345,7 +345,9 @@ class ResourceEpubView(ResourceSingleMixin, BaseEpubMixin, View):
     "Download corpus data embedded in an EPUB3 file"
 
     def get(self, request, *args, **kwargs):
-        self.write_book(self.get_object())
+        self.setup_epub(self.get_object())
+        if not os.path.exists(self.path):
+            self.write_book()
         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'