3 from django.conf import settings
4 from django.views.generic.simple import direct_to_template
5 from django.views.decorators.http import require_POST
6 from .helpers import JSONResponse, JSONFormInvalid, JSONServerError
7 from django import http
9 from wiki.models import getstorage
10 from wiki.forms import DocumentForm, DocumentTextSaveForm, DocumentTagForm
11 from datetime import datetime
12 from django.utils.encoding import smart_unicode
16 # Quick hack around caching problems, TODO: use ETags
18 from django.views.decorators.cache import never_cache
21 logger = logging.getLogger("fnp.peanut.api")
30 def document_list(request, template_name='wiki/document_list.html'):
31 # TODO: find a way to cache "Storage All"
32 return direct_to_template(request, template_name, extra_context={
33 'document_list': getstorage().all(),
34 'last_docs': sorted(request.session.get("wiki_last_docs", {}).items(),
35 key=operator.itemgetter(1), reverse=True),
40 def document_detail(request, name, template_name='wiki/document_details.html'):
42 document = getstorage().get_or_404(name)
44 access_time = datetime.now()
45 last_documents = request.session.get("wiki_last_docs", {})
46 last_documents[name] = access_time
48 if len(last_documents) > MAX_LAST_DOCS:
49 oldest_key = min(last_documents, key=last_documents.__getitem__)
50 del last_documents[oldest_key]
51 request.session['wiki_last_docs'] = last_documents
53 return direct_to_template(request, template_name, extra_context={
55 'document_info': document.info,
56 'document_meta': document.meta,
57 'forms': {"text_save": DocumentTextSaveForm(), "add_tag": DocumentTagForm()},
62 def document_text(request, name):
63 storage = getstorage()
64 document = storage.get_or_404(name)
66 if request.method == 'POST':
67 form = DocumentTextSaveForm(request.POST)
70 revision = form.cleaned_data['parent_revision']
71 document.text = form.cleaned_data['text']
74 author=form.cleaned_data['author'] or request.user.username,
75 comment=form.cleaned_data['comment'],
80 'text': document.plain_text if revision != document.revision() else None,
81 'meta': document.meta(),
82 'revision': document.revision(),
85 return JSONFormInvalid(form)
88 'text': document.plain_text,
89 'meta': document.meta(),
90 'revision': document.revision(),
95 def document_gallery(request, directory):
98 smart_unicode(settings.MEDIA_URL),
99 smart_unicode(settings.FILEBROWSER_DIRECTORY),
100 smart_unicode(directory)))
102 base_dir = os.path.join(
103 smart_unicode(settings.MEDIA_ROOT),
104 smart_unicode(settings.FILEBROWSER_DIRECTORY),
105 smart_unicode(directory))
107 def map_to_url(filename):
108 return "%s/%s" % (base_url, smart_unicode(filename))
110 def is_image(filename):
111 return os.path.splitext(f)[1].lower() in (u'.jpg', u'.jpeg', u'.png')
113 images = [map_to_url(f) for f in map(smart_unicode, os.listdir(base_dir)) if is_image(f)]
115 return JSONResponse(images)
116 except (IndexError, OSError), exc:
118 traceback.print_exc()
123 def document_diff(request, name):
124 storage = getstorage()
126 revA = int(request.GET.get('from', 0))
127 revB = int(request.GET.get('to', 0))
130 revA, revB = revB, revA
135 docA = storage.get_or_404(name, int(revA))
136 docB = storage.get_or_404(name, int(revB))
138 return http.HttpResponse(nice_diff.html_diff_table(docA.plain_text.splitlines(),
139 docB.plain_text.splitlines()))
143 def document_history(request, name):
144 storage = getstorage()
145 return JSONResponse(storage.history(name))
149 def document_add_tag(request, name):
150 storage = getstorage()
152 form = DocumentTagForm(request.POST)
154 doc = storage.get_or_404(name, form.cleaned_data['version'])
155 doc.add_tag(form.cleaned_data['tag'])
156 return JSONResponse({"message": _("Tag added")})
158 return JSONFormInvalid(form)
162 def document_publish(request, name, version):
163 storage = getstorage()
166 document = storage.get_or_404(name, revision=int(version))
168 api = wlapi.WLAPI(settings.WL_API_CONFIG)
170 return JSONResponse({"result": api.publish_book(document)})
171 except wlapi.APICallException, e:
172 return JSONServerError({"message": str(e)})