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 # access_time = datetime.ctime();
35 # last_documents = request.session.get("wiki_last_docs", [])
37 # if name not in last_documents:
38 # last_documents.insert(0, (name, access_time))
40 if request.method == 'POST':
42 form = DocumentForm(request.POST, instance = document)
44 document = form.save()
45 return HttpResponse(json.dumps({'text': document.plain_text, 'meta': document.meta(), 'revision': document.revision()}))
47 return HttpResponse(json.dumps({'errors': form.errors}))
49 form = DocumentForm(instance = document)
51 return direct_to_template(request, template_name, extra_context = {
57 def document_gallery(request, directory):
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')]
62 return HttpResponse(json.dumps(images))
63 except (IndexError, OSError), exc:
69 def document_diff(request, name, revA, revB):
70 differ = difflib.HtmlDiff(wrapcolumn=60)
72 docA = storage.get(name, int(revA))
73 docB = storage.get(name, int(revB))
75 return HttpResponse(differ.make_table(
76 docA.plain_text.splitlines(),
77 docB.plain_text.splitlines() ) )
80 def document_history(reuqest, name):
81 return HttpResponse( json.dumps(storage.history(name), cls=DateTimeEncoder), mimetype='application/json')