From dc4ad149067db46572a092e74b96bffd7467ff1b Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Thu, 7 May 2015 16:26:45 +0200 Subject: [PATCH] fix epub import and export --- examples/sandbox/settings.py | 3 +- .../telemeta-import-corpus-from-dir.py | 31 +++++++++++++------ .../telemeta/inc/collection_epub.html | 2 +- telemeta/views/collection.py | 5 ++- telemeta/views/core.py | 18 ++++++----- telemeta/views/resource.py | 2 +- 6 files changed, 37 insertions(+), 24 deletions(-) diff --git a/examples/sandbox/settings.py b/examples/sandbox/settings.py index 278a44cd..25779efd 100644 --- a/examples/sandbox/settings.py +++ b/examples/sandbox/settings.py @@ -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': { diff --git a/telemeta/management/commands/telemeta-import-corpus-from-dir.py b/telemeta/management/commands/telemeta-import-corpus-from-dir.py index ec3a0248..c5c63797 100644 --- a/telemeta/management/commands/telemeta-import-corpus-from-dir.py +++ b/telemeta/management/commands/telemeta-import-corpus-from-dir.py @@ -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): diff --git a/telemeta/templates/telemeta/inc/collection_epub.html b/telemeta/templates/telemeta/inc/collection_epub.html index cddac5da..37020245 100644 --- a/telemeta/templates/telemeta/inc/collection_epub.html +++ b/telemeta/templates/telemeta/inc/collection_epub.html @@ -5,7 +5,7 @@ {% for item in items %}

- Son {{ item.old_code }} : {{ item.title }}. {{ item.descriptions }} ({{ item.track }}) + Son {{ item.old_code }} : {{ item.title }}. {{ item.comment }} ({{ item.track }})

diff --git a/telemeta/views/collection.py b/telemeta/views/collection.py index fd8b55ab..9e47888b 100644 --- a/telemeta/views/collection.py +++ b/telemeta/views/collection.py @@ -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) diff --git a/telemeta/views/core.py b/telemeta/views/core.py index ac94d9ce..b336964b 100644 --- a/telemeta/views/core.py +++ b/telemeta/views/core.py @@ -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(): diff --git a/telemeta/views/resource.py b/telemeta/views/resource.py index bda4bc6a..da546656 100644 --- a/telemeta/views/resource.py +++ b/telemeta/views/resource.py @@ -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) -- 2.39.5