+ 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 []
+ publishable = (form.cleaned_data['publishable'] and
+ request.user.has_perm('catalogue.can_pubmark'))
+ doc.commit(author=author,
+ text=text,
+ parent=parent,
+ description=form.cleaned_data['comment'],
+ tags=tags,
+ author_name=form.cleaned_data['author_name'],
+ author_email=form.cleaned_data['author_email'],
+ publishable=publishable,
+ )
+ revision = doc.revision()
+ return JSONResponse({
+ 'text': doc.materialize() if parent_revision != revision else None,
+ 'version': revision,
+ 'stage': doc.stage.name if doc.stage else None,
+ 'assignment': doc.user.username if doc.user else None
+ })
+ 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 = ''