+@staff_required
+def import_book(request):
+ """docstring for import_book"""
+ book_import_form = forms.BookImportForm(request.POST, request.FILES)
+ if book_import_form.is_valid():
+ try:
+ book_import_form.save()
+ except:
+ import sys
+ import pprint
+ import traceback
+ info = sys.exc_info()
+ exception = pprint.pformat(info[1])
+ tb = '\n'.join(traceback.format_tb(info[2]))
+ return HttpResponse(_("An error occurred: %(exception)s\n\n%(tb)s") % {'exception':exception, 'tb':tb}, mimetype='text/plain')
+ return HttpResponse(_("Book imported successfully"))
+ else:
+ return HttpResponse(_("Error importing file: %r") % book_import_form.errors)
+
+
+# info views for API
+
+def book_info(request, id, lang='pl'):
+ book = get_object_or_404(models.Book, id=id)
+ # set language by hand
+ translation.activate(lang)
+ return render_to_response('catalogue/book_info.html', locals(),
+ context_instance=RequestContext(request))
+
+
+def tag_info(request, id):
+ tag = get_object_or_404(models.Tag, id=id)
+ return HttpResponse(tag.description)
+
+
+def download_zip(request, format, slug=None):
+ url = None
+ if format in models.Book.ebook_formats:
+ url = models.Book.zip_format(format)
+ elif format in ('mp3', 'ogg') and slug is not None:
+ book = get_object_or_404(models.Book, slug=slug)
+ 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, slug, method='GET'):
+ book = get_object_or_404(models.Book, slug=slug)
+
+ 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.slug), 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 = ugettext_lazy('Download custom PDF')
+ submit = ugettext_lazy('Download')
+
+ def __call__(self, request):
+ from copy import copy
+ if request.method == 'POST':
+ request.GET = copy(request.GET)
+ request.GET['next'] = "%s?%s" % (reverse('catalogue.views.download_custom_pdf', args=[request.GET.get('slug')]),
+ request.POST.urlencode())
+ return super(CustomPDFFormView, self).__call__(request)
+
+ def get_object(self, request):
+ return get_object_or_404(models.Book, slug=request.GET.get('slug'))
+
+ def context_description(self, request, obj):
+ return obj.pretty_title()