+ revision = request.GET.get("revision", None)
+
+ try:
+ revision = int(revision)
+ except (ValueError, TypeError):
+ revision = doc.revision()
+
+ if revision is not None:
+ text = doc.at_revision(revision).materialize()
+ else:
+ text = ''
+
+ return JSONResponse({
+ 'text': text,
+ 'meta': {},
+ 'revision': revision,
+ })
+
+
+@never_cache
+@require_POST
+def revert(request, doc_id):
+ form = forms.DocumentTextRevertForm(request.POST, prefix="textrevert")
+ if form.is_valid():
+ doc = get_object_or_404(Document, pk=doc_id, deleted=False)
+ if not doc.can_edit(request.user):
+ return HttpResponseForbidden("Not authorized.")
+ rev = get_object_or_404(Revision, pk=form.cleaned_data['revision'])
+
+ comment = form.cleaned_data['comment']
+ comment += "\n#revert to %s" % rev.pk
+
+ if request.user.is_authenticated():
+ author = request.user
+ else:
+ author = None
+
+ # before = doc.revision
+ logger.info("Reverting %s to %s", doc_id, rev.pk)
+
+ doc.commit(
+ author=author,
+ text=rev.materialize(),
+ description=comment,
+ # author_name=form.cleaned_data['author_name'], #?
+ # author_email=form.cleaned_data['author_email'], #?
+ )
+
+ return JSONResponse({
+ 'document': doc.materialize(),
+ 'version': len(get_history(doc)),
+ 'stage': doc.stage,
+ 'stage_name': doc.stage_name(),
+ 'assignment': doc.assigned_to.username if doc.assigned_to else None,
+ })
+ else:
+ return JSONFormInvalid(form)
+
+
+@never_cache
+def gallery(request, directory):
+ if not request.user.is_authenticated():
+ return HttpResponseForbidden("Not authorized.")
+
+ try:
+ base_url = ''.join((
+ smart_unicode(settings.MEDIA_URL),
+ smart_unicode(settings.IMAGE_DIR),
+ smart_unicode(directory)))
+
+ base_dir = os.path.join(
+ smart_unicode(settings.MEDIA_ROOT),
+ smart_unicode(settings.IMAGE_DIR),
+ smart_unicode(directory))
+
+ def map_to_url(filename):
+ return urllib.quote("%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, doc_id):
+ 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
+
+ # TODO: check if revisions in line.
+
+ doc = get_object_or_404(Document, pk=doc_id, deleted=False)
+
+ # allow diff from the beginning
+ if revA:
+ docA = Revision.objects.get(pk=revA).materialize()
+ else:
+ docA = ""
+ docB = Revision.objects.get(pk=revB).materialize()
+
+ return http.HttpResponse(nice_diff.html_diff_table(docA.splitlines(), docB.splitlines(), context=3))
+
+
+@never_cache
+def history(request, doc_id):
+ # TODO: pagination
+ doc = get_object_or_404(Document, pk=doc_id, deleted=False)
+
+ return JSONResponse(get_history(doc))