]> git.parisson.com Git - telemeta.git/commitdiff
put epub in cache, add TelemetaBaseMixin
authorGuillaume Pellerin <yomguy@parisson.com>
Fri, 1 May 2015 14:43:01 +0000 (16:43 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Fri, 1 May 2015 14:43:01 +0000 (16:43 +0200)
telemeta/views/core.py
telemeta/views/item.py
telemeta/views/resource.py

index a8f518abdefb667489531c20bfd0df87b5ed01ee..28fccc2fc25bdbd21fe8091b1f3de5ce5cc01e8a 100644 (file)
@@ -95,7 +95,12 @@ import jqchat.models
 mods = {'item': MediaItem, 'collection': MediaCollection,
         'corpus': MediaCorpus, 'fonds': MediaFonds, 'marker': MediaItemMarker, }
 
-# TOOLS
+
+class TelemetaBaseMixin(object):
+
+    cache_data = TelemetaCache(settings.TELEMETA_DATA_CACHE_DIR)
+    cache_export = TelemetaCache(settings.TELEMETA_EXPORT_CACHE_DIR)
+
 
 class FixedFileWrapper(FileWrapper):
     def __iter__(self):
index 24c91248261ad2bd3114de7f3d849d3662a799a8..cc047e7162e743c065d29e396a508bd1095360cc 100644 (file)
@@ -41,15 +41,13 @@ from telemeta.views.marker import *
 import timeside.core
 
 
-class ItemBaseMixin(object):
+class ItemBaseMixin(TelemetaBaseMixin):
 
     graphers = timeside.core.processor.processors(timeside.core.api.IGrapher)
     decoders = timeside.core.processor.processors(timeside.core.api.IDecoder)
     encoders = timeside.core.processor.processors(timeside.core.api.IEncoder)
     analyzers = timeside.core.processor.processors(timeside.core.api.IAnalyzer)
     value_analyzers = timeside.core.processor.processors(timeside.core.api.IValueAnalyzer)
-    cache_data = TelemetaCache(settings.TELEMETA_DATA_CACHE_DIR)
-    cache_export = TelemetaCache(settings.TELEMETA_EXPORT_CACHE_DIR)
 
     export_enabled = getattr(settings, 'TELEMETA_DOWNLOAD_ENABLED', True)
     export_formats = getattr(settings, 'TELEMETA_DOWNLOAD_FORMATS', ('mp3', 'wav'))
index 73ea30d0a34b939b2cda41fea9975f9372ad54cd..dba2bd4c650f00fbc53fcde953b8124856f4b354 100644 (file)
@@ -356,7 +356,8 @@ def cleanup_path(path):
     return os.sep.join(new_path)
 
 
-class CorpusEpubView(View):
+class CorpusEpubView(TelemetaBaseMixin, View):
+    "Download corpus data embedded in an EPUB3 file"
 
     model = MediaCorpus
 
@@ -364,9 +365,6 @@ class CorpusEpubView(View):
         return MediaCorpus.objects.get(public_id=self.kwargs['public_id'])
 
     def get(self, request, *args, **kwargs):
-        """
-        Stream an Epub file of collection data
-        """
         from collections import OrderedDict
         from ebooklib import epub
         from django.template.loader import render_to_string
@@ -442,15 +440,17 @@ class CorpusEpubView(View):
         book.spine = chapters
 
         # create epub file
-        filename = '/tmp/test.epub'
+        epub_name = corpus.code + '.epub'
+        path = self.cache_data.dir + os.sep + epub_name
         epub.write_epub(filename, book, {})
         epub_file = open(filename, 'rb')
 
         response = HttpResponse(epub_file.read(), content_type='application/epub+zip')
-        response['Content-Disposition'] = "attachment; filename=%s.%s" % \
-                                             (collection.code, 'epub')
+        response['Content-Disposition'] = "attachment; filename=%s" % epub_name
+
         return response
 
     @method_decorator(login_required)
     def dispatch(self, *args, **kwargs):
         return super(CorpusEpubView, self).dispatch(*args, **kwargs)
+