- raise Http404
-
-def document_diff(request, name, revA, revB):
- docA = storage.get(name, int(revA))
- docB = storage.get(name, int(revB))
-
-
- return HttpResponse(nice_diff.html_diff_table(docA.plain_text.splitlines(),
- docB.plain_text.splitlines()) )
-
-
-def document_history(request, name):
- return HttpResponse(
- json.dumps(storage.history(name), cls=DateTimeEncoder),
- mimetype='application/json')
-
-
-def document_publish(request, name, version):
- # get the document
- try:
- document = storage.get(name, revision = int(version))
- except DocumentNotFound:
- raise Http404
-
- poster.streaminghttp.register_openers()
-
- # send request to WL
- http = httplib2.Http()
- http.add_credentials("test", "test")
- http.follow_all_redirects = True
- datagen, headers = poster.encode.multipart_encode({name: document.plain_text})
-
- for key, value in headers.items():
- headers[key] = unicode(value)
-
- try:
- resp, data = http.request("http://localhost:8000/api/books.json",
- method = "POST", body = ''.join(datagen), headers = headers)
-
- print resp, data
-
- if resp.status == 201: # success
- result = {"success": True}
+ book = Book.objects.get(gallery=directory)
+
+ if not book.public and not request.user.is_authenticated():
+ return HttpResponseForbidden("Not authorized.")
+
+ 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)
+ if not doc.book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ # 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)
+ if not doc.book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+ return http.HttpResponse(str(doc.revision()))
+
+
+@never_cache
+def history(request, chunk_id):
+ # TODO: pagination
+ doc = get_object_or_404(Chunk, pk=chunk_id)
+ if not doc.book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ changes = []
+ for change in doc.history().reverse():
+ changes.append({
+ "version": change.revision,
+ "description": change.description,
+ "author": change.author_str(),
+ "date": localize(change.created_at),
+ "publishable": _("Publishable") + "\n" if change.publishable else "",
+ "tag": ',\n'.join(unicode(tag) for tag in change.tags.all()),
+ "published": _("Published") + ": " + \
+ localize(change.publish_log.order_by('-book_record__timestamp')[0].book_record.timestamp) \
+ if change.publish_log.exists() else "",
+ })
+ 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)
+ if not doc.book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ 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")})