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': {
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
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:
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):
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:
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):
{% 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">
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)
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
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():
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)