- try:
- revision = revision and int(revision)
- logger.info("Fetching %s", revision)
- document = storage.get(name, revision)
- except ValueError:
- # treat as a tag
- logger.info("Fetching tag %s", revision)
- document = storage.get_by_tag(name, revision)
- except DocumentNotFound:
- raise http.Http404
+ 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, chunk_id):
+ form = forms.DocumentTextRevertForm(request.POST, prefix="textrevert")
+ if form.is_valid():
+ doc = get_object_or_404(Chunk, pk=chunk_id)
+ if not doc.book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ 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", chunk_id, revision)
+ doc.at_revision(revision).revert(author=author, description=comment)