+ if request.user.is_authenticated():
+ author = request.user
+ else:
+ author = None
+ text = form.cleaned_data['text']
+ parent_revision = form.cleaned_data['parent_revision']
+ if parent_revision is not None:
+ parent = doc.at_revision(parent_revision)
+ else:
+ parent = None
+ stage = form.cleaned_data['stage_completed']
+ tags = [stage] if stage else []
+ doc.commit(author=author,
+ text=text,
+ parent=parent,
+ description=form.cleaned_data['comment'],
+ tags=tags,
+ )
+ revision = doc.revision()
+ return JSONResponse({
+ 'text': doc.materialize() if parent_revision != revision else None,
+ 'meta': {},
+ 'revision': revision,
+ })
+ else:
+ return JSONFormInvalid(form)
+ else:
+ 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, slug, chunk=None):
+ form = forms.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