+ sst = SST.from_string(rev.materialize())
+ except ValueError as e:
+ return HttpResponse(content=force_str(e.message), content_type='text/plain', status='400')
+
+ ctx = Context(
+ files_path='http://%s/media/dynamic/uploads/%s/' % (request.get_host(), pk),
+ source_url='http://%s%s' % (request.get_host(), reverse('catalogue_html', args=[doc.pk])),
+ )
+ if doc.owner_organization is not None and doc.owner_organization.logo:
+ ctx.cover_logo = 'http://%s%s' % (request.get_host(), doc.owner_organization.logo.url)
+ try:
+ epub_file = EpubFormat(sst).build(ctx)
+ except BuildError as e:
+ return HttpResponse(content=force_str(e.message), content_type='text/plain', status='400')
+
+ output_file = NamedTemporaryFile(prefix='librarian', suffix='.mobi', delete=False)
+ output_file.close()
+ subprocess.check_call(
+ ['ebook-convert', epub_file.get_filename(), output_file.name, '--no-inline-toc'])
+
+ from catalogue.ebook_utils import serve_file
+ return serve_file(output_file.name, '%d.mobi' % doc.pk, 'application/epub+zip')
+
+
+# @never_cache
+# def revision(request, slug, chunk=None):
+# try:
+# doc = Chunk.get(slug, chunk)
+# except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
+# raise Http404
+# if not doc.book.accessible(request):
+# return HttpResponseForbidden("Not authorized.")
+# return http.HttpResponse(str(doc.revision()))
+
+
+@login_required
+def book_schedule(request, pk):
+ book = get_object_or_404(Document, pk=pk, deleted=False)
+ if request.method == 'POST':
+ Plan.objects.filter(document=book).delete()
+ for i, (s, name) in enumerate(STAGES):
+ user_id = request.POST.get('s%d-user' % i)
+ deadline = request.POST.get('s%d-deadline' % i) or None
+ Plan.objects.create(document=book, stage=s, user_id=user_id, deadline=deadline)
+
+ book.set_stage(request.POST.get('stage', ''))
+ return redirect('catalogue_user')
+
+ current = {}
+ for p in Plan.objects.filter(document=book):
+ current[p.stage] = (getattr(p.user, 'pk', None), (p.deadline.isoformat() if p.deadline else None))
+
+ schedule = [(i, s, current.get(s, ())) for i, (s, name) in enumerate(STAGES)]
+
+ if book.owner_organization:
+ people = [m.user for m in book.owner_organization.membership_set.exclude(status='pending')]