Removed faulty debug
[redakcja.git] / apps / wiki / views.py
1 import os
2
3 from django.conf import settings
4 from django.views.generic.simple import direct_to_template
5 from django.http import HttpResponse, Http404
6 from django.utils import simplejson as json
7
8 from wiki.models import storage, Document, DocumentNotFound
9 from wiki.forms import DocumentForm
10 from datetime import datetime
11
12 # import google_diff
13 import difflib
14
15
16 class DateTimeEncoder(json.JSONEncoder):
17      def default(self, obj):
18          if isinstance(obj, datetime):
19              return datetime.ctime(obj) + " " + (datetime.tzname(obj) or 'GMT')
20          return json.JSONEncoder.default(self, obj)
21
22 def document_list(request, template_name = 'wiki/document_list.html'):
23     return direct_to_template(request, template_name, extra_context = {
24         'document_list': storage.all(),
25     })
26
27
28 def document_detail(request, name, template_name = 'wiki/document_details.html'):
29     try:
30         document = storage.get(name)
31     except DocumentNotFound:        
32         raise Http404
33     
34 #    access_time = datetime.ctime();
35 #    last_documents = request.session.get("wiki_last_docs", [])
36 #        
37 #    if name not in last_documents:
38 #        last_documents.insert(0, (name, access_time))        
39
40     if request.method == 'POST':
41         
42         form = DocumentForm(request.POST, instance = document)
43         if form.is_valid():
44             document = form.save()
45             return HttpResponse(json.dumps({'text': document.plain_text, 'meta': document.meta(), 'revision': document.revision()}))
46         else:
47             return HttpResponse(json.dumps({'errors': form.errors}))
48     else:
49         form = DocumentForm(instance = document)
50
51     return direct_to_template(request, template_name, extra_context = {
52         'document': document,
53         'form': form,
54     })
55
56
57 def document_gallery(request, directory):
58     try:
59         base_dir = os.path.join(settings.MEDIA_ROOT, settings.FILEBROWSER_DIRECTORY, directory)
60         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')]
61         images.sort()
62         return HttpResponse(json.dumps(images))
63     except (IndexError, OSError), exc:
64         import traceback
65         traceback.print_exc()
66
67         raise Http404
68     
69 def document_diff(request, name, revA, revB):
70     differ = difflib.HtmlDiff(wrapcolumn=60)
71      
72     docA = storage.get(name, int(revA))
73     docB = storage.get(name, int(revB))
74      
75     return HttpResponse(differ.make_table(
76                                 docA.plain_text.splitlines(),
77                                 docB.plain_text.splitlines() ) )                                           
78     
79     
80 def document_history(reuqest, name):
81     return HttpResponse( json.dumps(storage.history(name), cls=DateTimeEncoder), mimetype='application/json')