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
8 from wiki.models import storage, Document, DocumentNotFound
9 from wiki.forms import DocumentForm
10 from datetime import datetime
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)
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(),
28 def document_detail(request, name, template_name = 'wiki/document_details.html'):
30 document = storage.get(name)
31 except DocumentNotFound:
34 # document = Document(storage, name = name, text = '')
36 if request.method == 'POST':
37 form = DocumentForm(request.POST, instance = document)
39 document = form.save()
40 return HttpResponse(json.dumps({'text': document.plain_text, 'meta': document.meta(), 'revision': document.revision()}))
42 return HttpResponse(json.dumps({'errors': form.errors}))
44 form = DocumentForm(instance = document)
46 return direct_to_template(request, template_name, extra_context = {
52 def document_gallery(request, directory):
54 base_dir = os.path.join(settings.MEDIA_ROOT, settings.FILEBROWSER_DIRECTORY, directory)
55 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')]
57 return HttpResponse(json.dumps(images))
58 except (IndexError, OSError), exc:
64 def document_diff(request, name, revA, revB):
65 differ = difflib.HtmlDiff(wrapcolumn=60)
67 docA = storage.get(name, int(revA))
68 docB = storage.get(name, int(revB))
70 return HttpResponse(differ.make_table(
71 docA.plain_text.splitlines(),
72 docB.plain_text.splitlines() ) )
75 def document_history(reuqest, name):
76 return HttpResponse( json.dumps(storage.history(name), cls=DateTimeEncoder), mimetype='application/json')