+ 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, chunk_id):
+ form = forms.DocumentTextRevertForm(request.POST, prefix="textrevert")
+ if form.is_valid():
+ doc = get_object_or_404(Chunk, pk=chunk_id)
+
+ 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)
+
+ 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, chunk_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
+
+ doc = get_object_or_404(Chunk, pk=chunk_id)
+ # allow diff from the beginning
+ if revA:
+ docA = doc.at_revision(revA).materialize()
+ else:
+ docA = ""
+ 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, chunk_id):
+ doc = get_object_or_404(Chunk, pk=chunk_id)
+ return http.HttpResponse(str(doc.revision()))
+
+
+@never_cache
+def history(request, chunk_id):
+ # TODO: pagination
+ doc = get_object_or_404(Chunk, pk=chunk_id)
+
+ changes = []
+ for change in doc.history().order_by('-created_at'):
+ changes.append({
+ "version": change.revision,
+ "description": change.description,
+ "author": change.author_str(),
+ "date": change.created_at,
+ "publishable": _("Publishable") + "\n" if change.publishable else "",
+ "tag": ',\n'.join(unicode(tag) for tag in change.tags.all()),
+ })
+ return JSONResponse(changes)
+
+
+@require_POST
+@ajax_require_permission('catalogue.can_pubmark')
+def pubmark(request, chunk_id):
+ form = forms.DocumentPubmarkForm(request.POST, prefix="pubmark")
+ if form.is_valid():
+ doc = get_object_or_404(Chunk, pk=chunk_id)
+
+ revision = form.cleaned_data['revision']
+ publishable = form.cleaned_data['publishable']
+ change = doc.at_revision(revision)
+ if publishable != change.publishable:
+ change.set_publishable(publishable)
+ return JSONResponse({"message": _("Revision marked")})
+ else:
+ return JSONResponse({"message": _("Nothing changed")})
+ else:
+ return JSONFormInvalid(form)
+
+
+def themes(request):
+ prefix = request.GET.get('q', '')
+ return http.HttpResponse('\n'.join([str(t) for t in Theme.objects.filter(name__istartswith=prefix)]))