+ revision = request.GET.get("revision", None)
+
+ try:
+ revision = int(revision)
+ except ValueError:
+ revision = None
+
+ return JSONResponse({
+ 'text': doc.at_revision(revision).materialize(),
+ 'meta': {},
+ 'revision': revision if revision else doc.revision(),
+ })
+
+
+@never_cache
+@require_POST
+def revert(request, slug):
+ form = DocumentTextRevertForm(request.POST, prefix="textrevert")
+ if form.is_valid():
+ doc = get_object_or_404(Book, slug=slug).doc
+ 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):
+ 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
+
+ doc = get_object_or_404(Book, slug=slug).doc
+ 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):
+ book = get_object_or_404(Book, slug=slug)
+ return http.HttpResponse(str(book.doc.revision()))
+
+
+@never_cache
+def history(request, slug):
+ # TODO: pagination
+ book = get_object_or_404(Book, slug=slug)
+ rev = book.doc.revision()
+ changes = []
+ for change in book.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": rev,
+ "description": change.description,
+ "author": author,
+ "date": change.created_at,
+ "tag": [],
+ })
+ rev -= 1
+ return JSONResponse(changes)
+
+
+
+"""
+import wlapi
+
+
+@require_POST
+@ajax_require_permission('wiki.can_change_tags')
+def add_tag(request, name):
+ name = normalize_name(name)
+ storage = getstorage()
+
+ form = DocumentTagForm(request.POST, prefix="addtag")
+ if form.is_valid():
+ doc = storage.get_or_404(form.cleaned_data['id'])
+ doc.add_tag(tag=form.cleaned_data['tag'],
+ revision=form.cleaned_data['revision'],
+ author=request.user.username)
+ return JSONResponse({"message": _("Tag added")})
+ else:
+ return JSONFormInvalid(form)
+
+
+@require_POST
+@ajax_require_permission('wiki.can_publish')
+def publish(request, name):
+ name = normalize_name(name)
+
+ storage = getstorage()
+ document = storage.get_by_tag(name, "ready_to_publish")
+
+ api = wlapi.WLAPI(**settings.WL_API_CONFIG)
+
+ try:
+ return JSONResponse({"result": api.publish_book(document)})
+ except wlapi.APICallException, e:
+ return JSONServerError({"message": str(e)})
+"""
+
+def themes(request):
+ prefix = request.GET.get('q', '')
+ return http.HttpResponse('\n'.join([str(t) for t in Theme.objects.filter(name__istartswith=prefix)]))