+from django.utils.encoding import smart_unicode
+from django.utils.translation import ugettext_lazy as _
+from django.utils.decorators import decorator_from_middleware
+from django.middleware.gzip import GZipMiddleware
+
+import librarian.html
+import librarian.text
+
+#
+# Quick hack around caching problems, TODO: use ETags
+#
+from django.views.decorators.cache import never_cache
+
+import nice_diff
+import operator
+
+MAX_LAST_DOCS = 10
+
+
+@never_cache
+def document_list(request):
+ return direct_to_template(request, 'wiki/document_list.html', extra_context={
+ 'books': Book.objects.all(),
+ 'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
+ key=lambda x: x[1]['time'], reverse=True),
+ })
+
+
+@never_cache
+def editor(request, slug, chunk=None, template_name='wiki/document_details.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("wiki_create_missing", args=[slug]))
+ else:
+ raise Http404
+
+ 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
+
+ return direct_to_template(request, template_name, extra_context={
+ 'chunk': chunk,
+ 'forms': {
+ "text_save": DocumentTextSaveForm(prefix="textsave"),
+ "text_revert": DocumentTextRevertForm(prefix="textrevert"),
+ "add_tag": DocumentTagForm(prefix="addtag"),
+ },
+ 'REDMINE_URL': settings.REDMINE_URL,
+ })
+
+
+@require_GET
+def editor_readonly(request, slug, chunk=None, template_name='wiki/document_details_readonly.html'):
+ try:
+ chunk = Chunk.get(slug, chunk)
+ revision = request.GET['revision']
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist, KeyError):
+ raise Http404
+
+ access_time = datetime.now()
+ last_books = request.session.get("wiki_last_books", {})
+ last_books[slug, chunk.slug] = {
+ 'time': access_time,
+ 'title': chunk.book.title,
+ }
+
+ 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
+
+ return direct_to_template(request, template_name, extra_context={
+ 'chunk': chunk,
+ 'revision': revision,
+ 'readonly': True,
+ 'REDMINE_URL': settings.REDMINE_URL,
+ })
+