+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'),
+ })