info = self.book_info()
if info is not None:
- update['dc_slug'] = info.slug
+ update['dc_slug'] = info.url.slug
Book.objects.filter(pk=self.pk).update(**update)
def touch(self):
changes = self.get_current_changes(publishable)
return compile_text(change.materialize() for change in changes)
+ def wldocument(self, publishable=True, changes=None, parse_dublincore=True):
+ from catalogue.ebook_utils import RedakcjaDocProvider
+ from librarian.parser import WLDocument
+
+ return WLDocument.from_string(
+ self.materialize(publishable=publishable, changes=changes),
+ provider=RedakcjaDocProvider(publishable=publishable),
+ parse_dublincore=parse_dublincore)
+
def publish(self, user):
"""
Publishes a book on behalf of a (local) user.
book = get_object_or_404(Book, slug=slug)
if not book.accessible(request):
return HttpResponseForbidden("Not authorized.")
- xml = book.materialize()
- output = StringIO()
- # errors?
- import librarian.text
- librarian.text.transform(StringIO(xml), output)
- text = output.getvalue()
+ doc = book.wldocument()
+ text = doc.as_text().get_string()
response = http.HttpResponse(text, content_type='text/plain', mimetype='text/plain')
response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug
return response
book = get_object_or_404(Book, slug=slug)
if not book.accessible(request):
return HttpResponseForbidden("Not authorized.")
- xml = book.materialize()
- output = StringIO()
- # errors?
- import librarian.html
- librarian.html.transform(StringIO(xml), output, parse_dublincore=False,
- flags=['full-page'])
- html = output.getvalue()
+ doc = book.wldocument()
+ html = doc.as_html(parse_dublincore=False, flags=['full-page']).get_string()
response = http.HttpResponse(html, content_type='text/html', mimetype='text/html')
return response
if not book.accessible(request):
return HttpResponseForbidden("Not authorized.")
- from tempfile import NamedTemporaryFile
- from os import unlink
- from librarian import pdf
- from catalogue.ebook_utils import RedakcjaDocProvider, serve_file
-
- xml = book.materialize()
- xml_file = NamedTemporaryFile()
- xml_file.write(xml.encode('utf-8'))
- xml_file.flush()
-
- try:
- pdf_file = NamedTemporaryFile(delete=False)
- pdf.transform(RedakcjaDocProvider(publishable=True),
- file_path=xml_file.name,
- output_file=pdf_file,
- )
- return serve_file(pdf_file.name, book.slug + '.pdf', 'application/pdf')
- finally:
- unlink(pdf_file.name)
+ # TODO: move to celery
+ doc = book.wldocument()
+ # TODO: error handling
+ pdf_file = doc.as_pdf()
+ from catalogue.ebook_utils import serve_file
+ return serve_file(pdf_file.get_filename(),
+ book.slug + '.pdf', 'application/pdf')
@never_cache
if not book.accessible(request):
return HttpResponseForbidden("Not authorized.")
- from StringIO import StringIO
- from tempfile import NamedTemporaryFile
- from librarian import epub
- from catalogue.ebook_utils import RedakcjaDocProvider
-
- xml = book.materialize()
- xml_file = NamedTemporaryFile()
- xml_file.write(xml.encode('utf-8'))
- xml_file.flush()
-
- epub_file = StringIO()
- epub.transform(RedakcjaDocProvider(publishable=True),
- file_path=xml_file.name,
- output_file=epub_file)
+ # TODO: move to celery
+ doc = book.wldocument()
+ # TODO: error handling
+ epub = doc.as_epub().get_string()
response = HttpResponse(mimetype='application/epub+zip')
response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
- response.write(epub_file.getvalue())
+ response.write(epub)
return response