+ 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, chunk=chunk, prefix="textsave")
+ try:
+ version = int(request.GET.get('version', ''))
+ except ValueError:
+ version = None
+ if version:
+ text = chunk.at_revision(version).materialize()
+ else:
+ text = chunk.materialize()
+ materials = set(os.listdir(os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR, chunk.book.gallery)))
+ materials.update([n.rsplit('.', 1)[0] for n in materials])
+ return render(request, template_name, {
+ 'serialized_document_data': json.dumps({
+ 'document': text,
+ 'document_id': chunk.id,
+ 'title': chunk.book.title,
+ 'history': get_history(chunk),
+ 'version': version or chunk.revision(),
+ 'stage': chunk.stage.name if chunk.stage else None,
+ 'assignment': chunk.user.username if chunk.user else None
+ }),
+ 'serialized_templates': json.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'),
+ 'slug': chunk.book.slug,
+ 'gallery': chunk.book.gallery,
+ 'materials': json.dumps(list(materials)),
+ })
+
+
+@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
+ 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.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 render(request, template_name, {
+ 'chunk': chunk,
+ 'revision': revision,
+ 'readonly': True,
+ 'REDMINE_URL': settings.REDMINE_URL,
+ })
+
+
+@never_cache
+@decorator_from_middleware(GZipMiddleware)
+def text(request, chunk_id):
+ doc = get_object_or_404(Chunk, pk=chunk_id)
+ if not doc.book.accessible(request):
+ return HttpResponseForbidden("Not authorized.")
+
+ if request.method == 'POST':
+ form = forms.DocumentTextSaveForm(request.POST, user=request.user, chunk=doc, prefix="textsave")