+
+
+def download_zip(request, format, book=None):
+ kwargs = models.Book.split_fileid(book)
+
+ url = None
+ if format in models.Book.ebook_formats:
+ url = models.Book.zip_format(format)
+ elif format in ('mp3', 'ogg') and kwargs is not None:
+ book = get_object_or_404(models.Book, **kwargs)
+ url = book.zip_audiobooks(format)
+ else:
+ raise Http404('No format specified for zip package')
+ return HttpResponseRedirect(urlquote_plus(settings.MEDIA_URL + url, safe='/?='))
+
+
+def download_custom_pdf(request, book_fileid, method='GET'):
+ kwargs = models.Book.split_fileid(book_fileid)
+ if kwargs is None:
+ raise Http404
+ book = get_object_or_404(models.Book, **kwargs)
+
+ if request.method == method:
+ form = forms.CustomPDFForm(method == 'GET' and request.GET or request.POST)
+ if form.is_valid():
+ cust = form.customizations
+ pdf_file = models.get_customized_pdf_path(book, cust)
+
+ if not path.exists(pdf_file):
+ result = async_build_pdf.delay(book.id, cust, pdf_file)
+ result.wait()
+ return AttachmentHttpResponse(file_name=("%s.pdf" % book_fileid), file_path=pdf_file, mimetype="application/pdf")
+ else:
+ raise Http404(_('Incorrect customization options for PDF'))
+ else:
+ raise Http404(_('Bad method'))
+
+
+class CustomPDFFormView(AjaxableFormView):
+ form_class = forms.CustomPDFForm
+ title = _('Download custom PDF')
+ submit = _('Download')
+
+ def __call__(self, request):
+ if request.method == 'POST':
+ return download_custom_pdf(request, request.GET['book_id'], method='POST')
+ else:
+ return super(CustomPDFFormView, self).__call__(request)