Fixed tests to reflect changes in API.
[redakcja.git] / apps / wiki / views.py
index e466d6a..1539baf 100644 (file)
@@ -3,7 +3,7 @@ import os
 from django.conf import settings
 
 from django.views.generic.simple import direct_to_template
-from django.views.decorators.http import require_POST
+from django.views.decorators.http import require_POST, require_GET
 from django.core.urlresolvers import reverse
 from wiki.helpers import JSONResponse, JSONFormInvalid, JSONServerError, ajax_require_permission
 from django import http
@@ -42,9 +42,10 @@ def document_list(request, template_name='wiki/document_list.html'):
 
 @never_cache
 def document_detail(request, name, template_name='wiki/document_details.html'):
+    storage = getstorage()
 
     try:
-        document = getstorage().get(name)
+        document = storage.get(name)
     except DocumentNotFound:
         return http.HttpResponseRedirect(reverse("wiki_create_missing", args=[name]))
 
@@ -64,11 +65,38 @@ def document_detail(request, name, template_name='wiki/document_details.html'):
         'document_meta': document.meta,
         'forms': {
             "text_save": DocumentTextSaveForm(prefix="textsave"),
-            "add_tag": DocumentTagForm(prefix="addtag")
+            "add_tag": DocumentTagForm(prefix="addtag"),
         },
     })
 
 
+@require_GET
+def document_detail_readonly(request, name, template_name='wiki/document_details_readonly.html'):
+    storage = getstorage()
+
+    try:
+        revision = request.GET['revision']
+        document = storage.get(name, revision)
+    except (KeyError, DocumentNotFound) as e:
+        raise http.Http404
+
+    access_time = datetime.now()
+    last_documents = request.session.get("wiki_last_docs", {})
+    last_documents[name] = access_time
+
+    if len(last_documents) > MAX_LAST_DOCS:
+        oldest_key = min(last_documents, key=last_documents.__getitem__)
+        del last_documents[oldest_key]
+    request.session['wiki_last_docs'] = last_documents
+
+    return direct_to_template(request, template_name, extra_context={
+        'document': document,
+        'document_name': document.name,
+        'document_info': dict(document.info(), readonly=True),
+        'document_meta': document.meta,
+    })
+
+
 def document_create_missing(request, name):
     storage = getstorage()
 
@@ -106,12 +134,14 @@ def document_text(request, name):
             document = storage.get_or_404(name, revision)
             document.text = form.cleaned_data['text']
 
-            storage.put(document,
-                author=form.cleaned_data['author'] or request.user.username,
-                comment=form.cleaned_data['comment'],
-                parent=revision,
-            )
+            comment = form.cleaned_data['comment']
+
+            if form.cleaned_data['stage_completed']:
+                comment += '\n#stage-finished: %s\n' % form.cleaned_data['stage_completed']
+
+            author = "%s <%s>" % (form.cleaned_data['author_name'], form.cleaned_data['author_email'])
 
+            storage.put(document, author=author, comment=comment, parent=revision)
             document = storage.get(name)
 
             return JSONResponse({