+#
+# Quick hack around caching problems, TODO: use ETags
+#
+from django.views.decorators.cache import never_cache
+
+logger = logging.getLogger("fnp.wiki")
+
+MAX_LAST_DOCS = 10
+
+
+def get_history(chunk):
+ changes = []
+ for change in chunk.history():
+ 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 changes
+
+
+@never_cache
+def editor(request, slug, chunk=None, template_name='wiki/bootstrap.html'):
+ try:
+ chunk = Chunk.get(slug, chunk)
+ except Chunk.MultipleObjectsReturned:
+ # TODO: choice page
+ raise Http404
+ except Chunk.DoesNotExist:
+ if chunk is None:
+ try:
+ book = Book.objects.get(slug=slug)
+ except Book.DoesNotExist:
+ return http.HttpResponseRedirect(reverse("catalogue_create_missing", args=[slug]))
+ else:
+ raise Http404
+ if not chunk.book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ access_time = datetime.now()
+ last_books = request.session.get("wiki_last_books", {})
+ last_books[slug, chunk.slug] = {
+ 'time': access_time,
+ 'title': chunk.pretty_name(),
+ }
+
+ if len(last_books) > MAX_LAST_DOCS:
+ oldest_key = min(last_books, key=lambda x: last_books[x]['time'])
+ del last_books[oldest_key]
+ request.session['wiki_last_books'] = last_books
+
+ save_form = forms.DocumentTextSaveForm(user=request.user, prefix="textsave")
+ return render(request, template_name, {
+ 'serialized_document_data': simplejson.dumps({
+ 'document': chunk.materialize(),
+ 'document_id': chunk.id,
+ 'title': chunk.book.title,
+ 'history': get_history(chunk),
+ 'version': chunk.revision()
+ }),
+ 'serialized_templates': simplejson.dumps([
+ {'id': t.id, 'name': t.name, 'content': t.content} for t in Template.objects.filter(is_partial=True)
+ ]),
+ 'forms': {
+ "text_save": save_form,
+ "text_revert": forms.DocumentTextRevertForm(prefix="textrevert")
+ },
+ 'tags': list(save_form.fields['stage_completed'].choices),
+ 'can_pubmark': request.user.has_perm('catalogue.can_pubmark'),