]> git.parisson.com Git - telemeta.git/commitdiff
fix epub import and export
authorGuillaume Pellerin <yomguy@parisson.com>
Thu, 7 May 2015 14:26:45 +0000 (16:26 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Thu, 7 May 2015 14:26:45 +0000 (16:26 +0200)
examples/sandbox/settings.py
telemeta/management/commands/telemeta-import-corpus-from-dir.py
telemeta/templates/telemeta/inc/collection_epub.html
telemeta/views/collection.py
telemeta/views/core.py
telemeta/views/resource.py

index 278a44cdad98b4120412c4a9327f08126bbf1b77..25779efd464b679d7617ef7dc43da4d3df0ebff1 100644 (file)
@@ -18,7 +18,8 @@ ADMINS = (
 MANAGERS = ADMINS
 
 # Full filesystem path to the project.
-PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
+#PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
+PROJECT_ROOT = '/home/sandbox'
 
 DATABASES = {
     'default': {
index ec3a0248a09d8424dc5e7a641a33db24a49af6ce..c5c637976d16626baa3e6c4c0c305681629701f9 100644 (file)
@@ -2,6 +2,7 @@ from optparse import make_option
 from django.conf import settings
 from django.core.management.base import BaseCommand, CommandError
 from django.core.files.base import ContentFile
+from django.contrib.auth.models import User
 from telemeta.models import *
 from telemeta.util.unaccent import unaccent
 import os, re
@@ -47,9 +48,13 @@ class Command(BaseCommand):
     media_formats = ['mp3']
     image_formats = ['png', 'jpg']
     text_formats = ['txt']
+    media_root = settings.MEDIA_ROOT
+    dry_run = False
+    user = User.objects.get(username='admin')
 
     def write_file(self, item, media):
         filename = media.split(os.sep)[-1]
+        print media
         if os.path.exists(media):
             if not item.file or self.force:
                 if not self.media_root in self.source_dir:
@@ -71,14 +76,17 @@ class Command(BaseCommand):
 
     def handle(self, *args, **options):
         # NOT4PROD!!
-        reset()
+        reset()
 
         root_dir = args[-1]
-        cleanup_dir(root_dir)
-        chapters = os.listdir(root_dir)
+        self.source_dir = root_dir
+        print self.source_dir
+        print self.media_root
+        cleanup_dir(self.source_dir)
+        chapters = os.listdir(self.source_dir)
 
         for chapter in chapters:
-            chapter_dir = os.path.join(root_dir, chapter)
+            chapter_dir = os.path.join(self.source_dir, chapter)
             metadata = {}
 
             for filename in os.listdir(chapter_dir):
@@ -126,13 +134,16 @@ class Command(BaseCommand):
                         corpus_id = slugify(unicode(corpus_name))
                         collection_id = corpus_id + '_' + slugify(unicode(collection_name))
                         item_id = collection_id + '_' + slugify(unicode(item_name))
-
-                        corpus, c = MediaCorpus.objects.get_or_create(code=corpus_id)
-                        if c:
+                        
+                        cc = MediaCorpus.objects.filter(code=corpus_id)
+                        if cc:
+                            corpus = cc[0]
+                        else:
+                            corpus = MediaCorpus(code=corpus_id)
                             corpus.title = corpus_name
                             corpus.save()
 
-                        collection_title = collection_name.replace('_', ' ') + ' : ' + chapter_title
+                        collection_title = collection_name.replace('_', ' ') + ' - ' + chapter_title
                         print collection_title
                         cc = MediaCollection.objects.filter(code=collection_id, title=collection_title)
                         if cc:
@@ -147,14 +158,14 @@ class Command(BaseCommand):
 
                         item, c = MediaItem.objects.get_or_create(collection=collection, code=item_id)
                         item.old_code = item_name
-                        self.write_file(item, media_path)
+                        self.write_file(item, path)
                         title = data[0].split('.')
                         item.title = title[0].replace('\n', '')
                         print data
                         if len(data) > 1:
                             item.track = data[1].replace('\n', '')
                         if len(title) > 1:
-                            item.descriptions = '. '.join(title[1:])
+                            item.comment = '. '.join(title[1:])
                         item.save()
 
                         for related_file in os.listdir(root):
index cddac5dab6e2851d512cca60a5bb70a0638748b2..37020245e6e472196fea600b5d7a372fa5d81315 100644 (file)
@@ -5,7 +5,7 @@
 {% for item in items %}
 <div class="item">
   <h3>
-  <b>Son {{ item.old_code }}</b> : {{ item.title }}. {{ item.descriptions }} ({{ item.track }})
+  <b>Son {{ item.old_code }}</b> : {{ item.title }}. {{ item.comment }} ({{ item.track }})
   </h3>
 
   <div class="item-audio">
index fd8b55aba072155f1bbff5d47aee7c7dd0feab19..9e47888b81e663543c6c05724eb9c5b3b36002d5 100644 (file)
@@ -383,11 +383,10 @@ class CollectionEpubView(BaseEpubMixin, View):
     def get(self, request, *args, **kwargs):
         collection = self.get_object()
         corpus = collection.corpus.all()[0]
-        name = corpus.title + ' - ' + collection.title
-        self.write_book(corpus, collection=collection, name=name)
+        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
+        response['Content-Disposition'] = "attachment; filename=%s" % self.filename + '.epub'
         return response
 
     @method_decorator(login_required)
index ac94d9ce67682ba2b91ae0377324528f4834f5cb..b336964bb88bd18737d2d06b30cc574184b6fb9c 100644 (file)
@@ -313,7 +313,7 @@ class BaseEpubMixin(TelemetaBaseMixin):
     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, name=None):
+    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
@@ -323,18 +323,20 @@ class BaseEpubMixin(TelemetaBaseMixin):
         site = Site.objects.get_current()
         self.chapters = []
 
-        if not name:
-            self.name = self.corpus.title + '.epub'
+        if not collection:
+            self.filename = self.corpus.code
+            self.book.set_title(corpus.title)
         else:
-            self.name = name + '.epub'
+            self.filename = collection.code
+            self.book.set_title(corpus.title + ' - ' + collection.title)
 
-        self.path = self.cache_data.dir + os.sep + self.name
+        self.path = self.cache_data.dir + os.sep + self.filename + '.epub'
 
         # add metadata
-        self.book.set_identifier(self.corpus.public_id)
-        self.book.set_title(self.name)
+        self.book.set_identifier(corpus.public_id)
+        #self.book.set_title(corpus.title + ' - ' + collection.title)
         self.book.set_language('fr')
-        self.book.add_author(self.corpus.descriptions)
+        self.book.add_author(corpus.descriptions)
 
         # add cover image
         for media in self.corpus.related.all():
index bda4bc6aa0ca3ea9dfc55333e807636b431eb1a7..da546656eb0845bc0cac3463b0008bf6120e952d 100644 (file)
@@ -368,7 +368,7 @@ class CorpusEpubView(BaseEpubMixin, View):
         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.name
+        response['Content-Disposition'] = "attachment; filename=%s" % self.filename + '.epub'
         return response
 
     @method_decorator(login_required)