From: Guillaume Pellerin Date: Mon, 23 Feb 2015 17:34:44 +0000 (+0100) Subject: get first corpus epub generator (needs ebooklib) X-Git-Tag: 1.6a^2~19^2~8 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=ebe5292b3411762b5dc5cbb66d6aac0ae93b7681;p=telemeta.git get first corpus epub generator (needs ebooklib) --- diff --git a/telemeta/models/corpus.py b/telemeta/models/corpus.py index 067b82a5..a1f28f96 100644 --- a/telemeta/models/corpus.py +++ b/telemeta/models/corpus.py @@ -90,3 +90,4 @@ class MediaCorpusRelated(MediaRelated): verbose_name = _('corpus related media') verbose_name_plural = _('corpus related media') + diff --git a/telemeta/static/telemeta/css/telemeta_epub.css b/telemeta/static/telemeta/css/telemeta_epub.css new file mode 100644 index 00000000..f4d3b694 --- /dev/null +++ b/telemeta/static/telemeta/css/telemeta_epub.css @@ -0,0 +1,29 @@ +@namespace epub "http://www.idpf.org/2007/ops"; + +body { + font-family: Cambria, Liberation Serif, Bitstream Vera Serif, Georgia, Times, Times New Roman, serif; +} + +h2 { + text-align: left; + text-transform: uppercase; + font-weight: 200; +} + +ol { + list-style-type: none; +} + +ol > li:first-child { + margin-top: 0.3em; +} + + +nav[epub|type~='toc'] > ol > li > ol { + list-style-type:square; +} + + +nav[epub|type~='toc'] > ol > li > ol > li { + margin-top: 0.3em; +} diff --git a/telemeta/templates/telemeta/collection_epub.html b/telemeta/templates/telemeta/collection_epub.html new file mode 100644 index 00000000..0fa92b5f --- /dev/null +++ b/telemeta/templates/telemeta/collection_epub.html @@ -0,0 +1,8 @@ + +{% for item in collection.items.all %} +

+ {{ item.title }} +
+

+{% endfor %} diff --git a/telemeta/templates/telemeta/resource_detail.html b/telemeta/templates/telemeta/resource_detail.html index 69568be0..e80d37ac 100644 --- a/telemeta/templates/telemeta/resource_detail.html +++ b/telemeta/templates/telemeta/resource_detail.html @@ -57,7 +57,13 @@ jQuery(document).ready(function(){ {% endif %} - + {% if type == 'corpus' %} + + + + {% endif %} {% endblock %} diff --git a/telemeta/urls.py b/telemeta/urls.py index 8550588b..bd86ba0d 100644 --- a/telemeta/urls.py +++ b/telemeta/urls.py @@ -135,6 +135,7 @@ urlpatterns = patterns('', url(r'^archives/(?P[A-Za-z0-9._-]+)/(?P[A-Za-z0-9._-]+)/delete/$', ResourceDeleteView.as_view(), name="telemeta-resource-delete"), url(r'^archives/(?P[A-Za-z0-9._-]+)/(?P[A-Za-z0-9._-]+)/related/(?P[A-Za-z0-9._-]+)/view/$', resource_view.related_stream, name="telemeta-resource-related"), url(r'^archives/(?P[A-Za-z0-9._-]+)/(?P[A-Za-z0-9._-]+)/related/(?P[A-Za-z0-9._-]+)/download/$', resource_view.related_download, name="telemeta-resource-related-download"), + url(r'^archives/corpus/(?P[A-Za-z0-9._-]+)/epub/$', CorpusEpubView.as_view(), name="telemeta-corpus-epub"), # search # url(r'^archives/$', home_view.search, name="telemeta-archives"), diff --git a/telemeta/views/collection.py b/telemeta/views/collection.py index a6dca61e..00dca635 100644 --- a/telemeta/views/collection.py +++ b/telemeta/views/collection.py @@ -363,3 +363,4 @@ class CollectionCopyView(CollectionAddView): def dispatch(self, *args, **kwargs): return super(CollectionCopyView, self).dispatch(*args, **kwargs) + diff --git a/telemeta/views/resource.py b/telemeta/views/resource.py index dc84ccb0..50feeb26 100644 --- a/telemeta/views/resource.py +++ b/telemeta/views/resource.py @@ -348,3 +348,83 @@ class ResourceEditView(ResourceSingleMixin, UpdateWithInlinesView): def dispatch(self, *args, **kwargs): return super(ResourceEditView, self).dispatch(*args, **kwargs) + + +class CorpusEpubView(View): + + model = MediaCorpus + + def get_object(self): + return MediaCorpus.objects.get(public_id=self.kwargs['public_id']) + + def get(self, request, *args, **kwargs): + """ + Stream an Epub file of collection data + """ + from ebooklib import epub + from django.template.loader import render_to_string + + book = epub.EpubBook() + corpus = self.get_object() + local_path = os.path.dirname(__file__) + css = os.sep.join([local_path, '..', 'static', 'telemeta', 'css', 'telemeta_epub.css']) + collection_template = os.sep.join([local_path, '..', 'templates', 'telemeta', 'collection_epub.html']) + site = Site.objects.get_current() + + # add metadata + book.set_identifier(corpus.public_id) + book.set_title(corpus.title) + book.set_language('fr') + + book.add_author(corpus.descriptions) + + # add cover image + for media in corpus.related.all(): + if 'cover' in media.title or 'Cover' in media.title: + book.set_cover("cover.jpg", open(media.file.path, 'r').read()) + break + + chapters = [] + for collection in corpus.children.all(): + context = {'collection': collection, 'site': site} + c = epub.EpubHtml(title=collection.title, file_name=collection.code + '.xhtml', lang='fr') + c.content = render_to_string(collection_template, context) + print c.content + chapters.append(c) + # add chapters to the book + book.add_item(c) + + # create table of contents + # - add manual link + # - add section + # - add auto created links to chaptersfesse + + book.toc = (( chapters )) + + # add navigation files + book.add_item(epub.EpubNcx()) + book.add_item(epub.EpubNav()) + + # define css style + style = open(css, 'r') + nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style.read()) + book.add_item(nav_css) + + # create spin, add cover page as first page + chapters.insert(0,'nav') + chapters.insert(0,'cover') + book.spine = chapters + + # create epub file + filename = '/tmp/test.epub' + epub.write_epub(filename, book, {}) + epub_file = open(filename, 'r') + + response = StreamingHttpResponse(epub_file.read(), content_type='application/epub+zip') + response['Content-Disposition'] = "attachment; filename=%s.%s" % \ + (collection.code, 'epub') + return response + + @method_decorator(login_required) + def dispatch(self, *args, **kwargs): + return super(CorpusEpubView, self).dispatch(*args, **kwargs)