Complete serialization with pretty printing. Works both in FF and Webkit. This fixes...
[redakcja.git] / apps / wiki / views.py
index d64a529..931cf78 100644 (file)
@@ -7,32 +7,43 @@ from django.utils import simplejson as json
 
 from wiki.models import storage, Document, DocumentNotFound
 from wiki.forms import DocumentForm
 
 from wiki.models import storage, Document, DocumentNotFound
 from wiki.forms import DocumentForm
+from datetime import datetime
 
 
+# import google_diff
+import difflib
 
 
-def document_list(request, template_name='wiki/document_list.html'):
-    return direct_to_template(request, template_name, extra_context={
+
+class DateTimeEncoder(json.JSONEncoder):
+     def default(self, obj):
+         if isinstance(obj, datetime):
+             return datetime.ctime(obj) + " " + (datetime.tzname(obj) or 'GMT')
+         return json.JSONEncoder.default(self, obj)
+
+def document_list(request, template_name = 'wiki/document_list.html'):
+    return direct_to_template(request, template_name, extra_context = {
         'document_list': storage.all(),
     })
 
 
         'document_list': storage.all(),
     })
 
 
-def document_detail(request, name, template_name='wiki/document_details.html'):
+def document_detail(request, name, template_name = 'wiki/document_details.html'):
     try:
         document = storage.get(name)
     except DocumentNotFound:
     try:
         document = storage.get(name)
     except DocumentNotFound:
-        document = Document(storage, name=name, text='')
-    
+        # WTF ?!
+        raise Http404 
+        # document = Document(storage, name = name, text = '')
 
     if request.method == 'POST':
 
     if request.method == 'POST':
-        form = DocumentForm(request.POST, instance=document)
+        form = DocumentForm(request.POST, instance = document)
         if form.is_valid():
             document = form.save()
         if form.is_valid():
             document = form.save()
-            return HttpResponse(json.dumps({'text': document.plain_text(), 'meta': document.meta(), 'revision': document.revision()}))
+            return HttpResponse(json.dumps({'text': document.plain_text, 'meta': document.meta(), 'revision': document.revision()}))
         else:
             return HttpResponse(json.dumps({'errors': form.errors}))
     else:
         else:
             return HttpResponse(json.dumps({'errors': form.errors}))
     else:
-        form = DocumentForm(instance=document)
-    
-    return direct_to_template(request, template_name, extra_context={
+        form = DocumentForm(instance = document)
+
+    return direct_to_template(request, template_name, extra_context = {
         'document': document,
         'form': form,
     })
         'document': document,
         'form': form,
     })
@@ -41,7 +52,25 @@ def document_detail(request, name, template_name='wiki/document_details.html'):
 def document_gallery(request, directory):
     try:
         base_dir = os.path.join(settings.MEDIA_ROOT, settings.FILEBROWSER_DIRECTORY, directory)
 def document_gallery(request, directory):
     try:
         base_dir = os.path.join(settings.MEDIA_ROOT, settings.FILEBROWSER_DIRECTORY, directory)
-        images = ['%s%s%s/%s' % (settings.MEDIA_URL, settings.FILEBROWSER_DIRECTORY, directory, f) for f in os.listdir(base_dir) if os.path.splitext(f)[1].lower() in ('.jpg', '.jpeg', '.png')]
+        images = [u'%s%s%s/%s' % (settings.MEDIA_URL, settings.FILEBROWSER_DIRECTORY, directory, f) for f in os.listdir(base_dir) if os.path.splitext(f)[1].lower() in (u'.jpg', u'.jpeg', u'.png')]
+        images.sort()
         return HttpResponse(json.dumps(images))
         return HttpResponse(json.dumps(images))
-    except (IndexError, OSError), e:
+    except (IndexError, OSError), exc:
+        import traceback
+        traceback.print_exc()
+
         raise Http404
         raise Http404
+    
+def document_diff(request, name, revA, revB):
+    differ = difflib.HtmlDiff(wrapcolumn=60)
+     
+    docA = storage.get(name, int(revA))
+    docB = storage.get(name, int(revB))
+     
+    return HttpResponse(differ.make_table(
+                                docA.plain_text.splitlines(),
+                                docB.plain_text.splitlines() ) )                                           
+    
+    
+def document_history(reuqest, name):
+    return HttpResponse( json.dumps(storage.history(name), cls=DateTimeEncoder), mimetype='application/json')