+ response = http.HttpResponse(xml, content_type='application/xml', mimetype='application/wl+xml')
+ response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
+ return response
+
+
+@never_cache
+def book_txt(request, slug):
+ xml = get_object_or_404(Book, slug=slug).materialize()
+ output = StringIO()
+ # errors?
+ librarian.text.transform(StringIO(xml), output)
+ text = output.getvalue()
+ response = http.HttpResponse(text, content_type='text/plain', mimetype='text/plain')
+ response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug
+ return response
+
+
+@never_cache
+def book_html(request, slug):
+ xml = get_object_or_404(Book, slug=slug).materialize()
+ output = StringIO()
+ # errors?
+ librarian.html.transform(StringIO(xml), output, parse_dublincore=False,
+ flags=['full-page'])
+ html = output.getvalue()
+ response = http.HttpResponse(html, content_type='text/html', mimetype='text/html')
+ return response
+
+
+@never_cache
+@require_POST
+def revert(request, slug, chunk=None):
+ form = DocumentTextRevertForm(request.POST, prefix="textrevert")
+ if form.is_valid():
+ try:
+ doc = Chunk.get(slug, chunk)
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
+ raise Http404
+
+ revision = form.cleaned_data['revision']
+
+ comment = form.cleaned_data['comment']
+ comment += "\n#revert to %s" % revision
+
+ if request.user.is_authenticated():
+ author = request.user
+ else:
+ author = None
+
+ before = doc.revision()
+ logger.info("Reverting %s to %s", slug, revision)
+ doc.at_revision(revision).revert(author=author, description=comment)
+
+ return JSONResponse({
+ 'text': doc.materialize() if before != doc.revision() else None,
+ 'meta': {},
+ 'revision': doc.revision(),
+ })
+ else:
+ return JSONFormInvalid(form)
+
+
+@never_cache
+def gallery(request, directory):
+ try:
+ base_url = ''.join((
+ smart_unicode(settings.MEDIA_URL),
+ smart_unicode(settings.FILEBROWSER_DIRECTORY),
+ smart_unicode(directory)))
+
+ base_dir = os.path.join(
+ smart_unicode(settings.MEDIA_ROOT),
+ smart_unicode(settings.FILEBROWSER_DIRECTORY),
+ smart_unicode(directory))
+
+ def map_to_url(filename):
+ return "%s/%s" % (base_url, smart_unicode(filename))
+
+ def is_image(filename):
+ return os.path.splitext(f)[1].lower() in (u'.jpg', u'.jpeg', u'.png')
+
+ images = [map_to_url(f) for f in map(smart_unicode, os.listdir(base_dir)) if is_image(f)]
+ images.sort()
+ return JSONResponse(images)
+ except (IndexError, OSError):
+ logger.exception("Unable to fetch gallery")
+ raise http.Http404
+
+
+@never_cache
+def diff(request, slug, chunk=None):
+ revA = int(request.GET.get('from', 0))
+ revB = int(request.GET.get('to', 0))
+
+ if revA > revB:
+ revA, revB = revB, revA
+
+ if revB == 0:
+ revB = None
+
+ try:
+ doc = Chunk.get(slug, chunk)
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
+ raise Http404
+ docA = doc.at_revision(revA).materialize()
+ docB = doc.at_revision(revB).materialize()
+
+ return http.HttpResponse(nice_diff.html_diff_table(docA.splitlines(),
+ docB.splitlines(), context=3))
+
+
+@never_cache
+def revision(request, slug, chunk=None):
+ try:
+ doc = Chunk.get(slug, chunk)
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
+ raise Http404
+ return http.HttpResponse(str(doc.revision()))
+
+
+@never_cache
+def history(request, slug, chunk=None):
+ # TODO: pagination
+ try:
+ doc = Chunk.get(slug, chunk)
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
+ raise Http404
+
+ changes = []
+ for change in doc.history().order_by('-created_at'):
+ if change.author:
+ author = "%s %s <%s>" % (
+ change.author.first_name,
+ change.author.last_name,
+ change.author.email)
+ else:
+ author = None
+ changes.append({
+ "version": change.revision,
+ "description": change.description,
+ "author": author,
+ "date": change.created_at,
+ "tag": [],
+ })
+ return JSONResponse(changes)
+
+
+def book(request, slug):
+ book = get_object_or_404(Book, slug=slug)
+
+ return direct_to_template(request, "wiki/book_detail.html", extra_context={
+ "book": book,