3 from django.conf import settings
5 from django.views.generic.simple import direct_to_template
6 from django.views.decorators.http import require_POST
7 from wiki.helpers import JSONResponse, JSONFormInvalid, JSONServerError, ajax_require_permission
8 from django import http
10 from wiki.models import getstorage
11 from wiki.forms import DocumentTextSaveForm, DocumentTagForm
12 from datetime import datetime
13 from django.utils.encoding import smart_unicode
14 from django.utils.translation import ugettext_lazy as _
19 # Quick hack around caching problems, TODO: use ETags
21 from django.views.decorators.cache import never_cache
24 logger = logging.getLogger("fnp.peanut.api")
33 def document_list(request, template_name='wiki/document_list.html'):
34 # TODO: find a way to cache "Storage All"
35 return direct_to_template(request, template_name, extra_context={
36 'document_list': getstorage().all(),
37 'last_docs': sorted(request.session.get("wiki_last_docs", {}).items(),
38 key=operator.itemgetter(1), reverse=True),
43 def document_detail(request, name, template_name='wiki/document_details.html'):
45 document = getstorage().get_or_404(name)
47 access_time = datetime.now()
48 last_documents = request.session.get("wiki_last_docs", {})
49 last_documents[name] = access_time
51 if len(last_documents) > MAX_LAST_DOCS:
52 oldest_key = min(last_documents, key=last_documents.__getitem__)
53 del last_documents[oldest_key]
54 request.session['wiki_last_docs'] = last_documents
56 return direct_to_template(request, template_name, extra_context={
58 'document_info': document.info,
59 'document_meta': document.meta,
60 'forms': {"text_save": DocumentTextSaveForm(), "add_tag": DocumentTagForm()},
65 def document_text(request, name):
66 storage = getstorage()
67 document = storage.get_or_404(name)
69 if request.method == 'POST':
70 form = DocumentTextSaveForm(request.POST)
73 revision = form.cleaned_data['parent_revision']
74 document.text = form.cleaned_data['text']
77 author=form.cleaned_data['author'] or request.user.username,
78 comment=form.cleaned_data['comment'],
83 'text': document.plain_text if revision != document.revision() else None,
84 'meta': document.meta(),
85 'revision': document.revision(),
88 return JSONFormInvalid(form)
91 'text': document.plain_text,
92 'meta': document.meta(),
93 'revision': document.revision(),
98 def document_gallery(request, directory):
101 smart_unicode(settings.MEDIA_URL),
102 smart_unicode(settings.FILEBROWSER_DIRECTORY),
103 smart_unicode(directory)))
105 base_dir = os.path.join(
106 smart_unicode(settings.MEDIA_ROOT),
107 smart_unicode(settings.FILEBROWSER_DIRECTORY),
108 smart_unicode(directory))
110 def map_to_url(filename):
111 return "%s/%s" % (base_url, smart_unicode(filename))
113 def is_image(filename):
114 return os.path.splitext(f)[1].lower() in (u'.jpg', u'.jpeg', u'.png')
116 images = [map_to_url(f) for f in map(smart_unicode, os.listdir(base_dir)) if is_image(f)]
118 return JSONResponse(images)
119 except (IndexError, OSError), exc:
121 traceback.print_exc()
126 def document_diff(request, name):
127 storage = getstorage()
129 revA = int(request.GET.get('from', 0))
130 revB = int(request.GET.get('to', 0))
133 revA, revB = revB, revA
138 docA = storage.get_or_404(name, int(revA))
139 docB = storage.get_or_404(name, int(revB))
141 return http.HttpResponse(nice_diff.html_diff_table(docA.plain_text.splitlines(),
142 docB.plain_text.splitlines(), context=3))
146 def document_history(request, name):
147 storage = getstorage()
150 changesets = storage.history(name)
152 return JSONResponse(changesets)
156 @ajax_require_permission('wiki.can_change_tags')
157 def document_add_tag(request, name):
158 storage = getstorage()
160 form = DocumentTagForm(request.POST)
162 doc = storage.get_or_404(form.cleaned_data['id'])
163 doc.add_tag(tag=form.cleaned_data['tag'],
164 revision=form.cleaned_data['revision'],
165 author=request.user.username)
166 return JSONResponse({"message": _("Tag added")})
168 return JSONFormInvalid(form)
172 @ajax_require_permission('wiki.can_publish')
173 def document_publish(request, name, version):
174 storage = getstorage()
177 document = storage.get_or_404(name, revision=int(version))
179 api = wlapi.WLAPI(settings.WL_API_CONFIG)
181 return JSONResponse({"result": api.publish_book(document)})
182 except wlapi.APICallException, e:
183 return JSONServerError({"message": str(e)})