4 logger = logging.getLogger("fnp.wiki")
6 from django.conf import settings
8 from django.views.generic.simple import direct_to_template
9 from django.views.decorators.http import require_POST, require_GET
10 from django.core.urlresolvers import reverse
11 from wiki.helpers import (JSONResponse, JSONFormInvalid, JSONServerError,
12 ajax_require_permission, recursive_groupby)
13 from django import http
15 from wiki.models import getstorage, DocumentNotFound, normalize_name, split_name, join_name, Theme
16 from wiki.forms import DocumentTextSaveForm, DocumentTagForm, DocumentCreateForm, DocumentsUploadForm
17 from datetime import datetime
18 from django.utils.encoding import smart_unicode
19 from django.utils.translation import ugettext_lazy as _
23 # Quick hack around caching problems, TODO: use ETags
25 from django.views.decorators.cache import never_cache
34 def normalized_name(view):
36 @functools.wraps(view)
37 def decorated(request, name, *args):
38 normalized = normalize_name(name)
39 logger.debug('View check %r -> %r', name, normalized)
41 if normalized != name:
42 return http.HttpResponseRedirect(
43 reverse('wiki_' + view.__name__, kwargs={'name': normalized}))
45 return view(request, name, *args)
51 def document_list(request):
52 return direct_to_template(request, 'wiki/document_list.html', extra_context={
53 'docs': getstorage().all(),
54 'last_docs': sorted(request.session.get("wiki_last_docs", {}).items(),
55 key=operator.itemgetter(1), reverse=True),
61 def editor(request, name, template_name='wiki/document_details.html'):
62 storage = getstorage()
65 document = storage.get(name)
66 except DocumentNotFound:
67 return http.HttpResponseRedirect(reverse("wiki_create_missing", args=[name]))
69 access_time = datetime.now()
70 last_documents = request.session.get("wiki_last_docs", {})
71 last_documents[name] = access_time
73 if len(last_documents) > MAX_LAST_DOCS:
74 oldest_key = min(last_documents, key=last_documents.__getitem__)
75 del last_documents[oldest_key]
76 request.session['wiki_last_docs'] = last_documents
78 return direct_to_template(request, template_name, extra_context={
80 'document_name': document.name,
81 'document_info': document.info,
82 'document_meta': document.meta,
84 "text_save": DocumentTextSaveForm(prefix="textsave"),
85 "add_tag": DocumentTagForm(prefix="addtag"),
87 'REDMINE_URL': settings.REDMINE_URL,
93 def editor_readonly(request, name, template_name='wiki/document_details_readonly.html'):
94 name = normalize_name(name)
95 storage = getstorage()
98 revision = request.GET['revision']
99 document = storage.get(name, revision)
100 except (KeyError, DocumentNotFound):
103 access_time = datetime.now()
104 last_documents = request.session.get("wiki_last_docs", {})
105 last_documents[name] = access_time
107 if len(last_documents) > MAX_LAST_DOCS:
108 oldest_key = min(last_documents, key=last_documents.__getitem__)
109 del last_documents[oldest_key]
110 request.session['wiki_last_docs'] = last_documents
112 return direct_to_template(request, template_name, extra_context={
113 'document': document,
114 'document_name': document.name,
115 'document_info': dict(document.info(), readonly=True),
116 'document_meta': document.meta,
117 'REDMINE_URL': settings.REDMINE_URL,
122 def create_missing(request, name):
123 storage = getstorage()
125 if request.method == "POST":
126 form = DocumentCreateForm(request.POST, request.FILES)
128 doc = storage.create_document(
129 name=form.cleaned_data['id'],
130 text=form.cleaned_data['text'],
133 return http.HttpResponseRedirect(reverse("wiki_editor", args=[doc.name]))
135 form = DocumentCreateForm(initial={
136 "id": name.replace(" ", "_"),
137 "title": name.title(),
140 return direct_to_template(request, "wiki/document_create_missing.html", extra_context={
141 "document_name": name,
147 storage = getstorage()
149 if request.method == "POST":
150 form = DocumentsUploadForm(request.POST, request.FILES)
152 zip = form.cleaned_data['zip']
157 existing = storage.all()
158 for filename in zip.namelist():
159 if filename[-1] == '/':
161 title = normalize_name(os.path.basename(filename)[:-4])
162 if not (title and filename.endswith('.xml')):
163 skipped_list.append(filename)
164 elif title in titles:
165 error_list.append((filename, title, _('Title already used for %s' % titles[title])))
166 elif title in existing:
167 error_list.append((filename, title, _('Title already used in repository.')))
170 zip.read(filename).decode('utf-8') # test read
171 ok_list.append((filename, title))
172 except UnicodeDecodeError:
173 error_list.append((filename, title, _('File should be UTF-8 encoded.')))
174 titles[title] = filename
177 for filename, title in ok_list:
178 storage.create_document(
180 text=zip.read(filename).decode('utf-8')
183 return direct_to_template(request, "wiki/document_upload.html", extra_context={
186 "skipped_list": skipped_list,
187 "error_list": error_list,
189 #doc = storage.create_document(
191 # text=form.cleaned_data['text'],
194 return http.HttpResponse('\n'.join(yeslist) + '\n\n' + '\n'.join(nolist))
196 form = DocumentsUploadForm()
198 return direct_to_template(request, "wiki/document_upload.html", extra_context={
205 def text(request, name):
206 storage = getstorage()
208 if request.method == 'POST':
209 form = DocumentTextSaveForm(request.POST, prefix="textsave")
211 revision = form.cleaned_data['parent_revision']
212 document = storage.get_or_404(name, revision)
213 document.text = form.cleaned_data['text']
214 comment = form.cleaned_data['comment']
215 if form.cleaned_data['stage_completed']:
216 comment += '\n#stage-finished: %s\n' % form.cleaned_data['stage_completed']
217 if request.user.is_authenticated():
218 author_name = request.user
219 author_email = request.user.email
221 author_name = form.cleaned_data['author_name']
222 author_email = form.cleaned_data['author_email']
223 author = "%s <%s>" % (author_name, author_email)
224 storage.put(document, author=author, comment=comment, parent=revision)
225 document = storage.get(name)
226 return JSONResponse({
227 'text': document.plain_text if revision != document.revision else None,
228 'meta': document.meta(),
229 'revision': document.revision,
232 return JSONFormInvalid(form)
234 revision = request.GET.get("revision", None)
238 revision = revision and int(revision)
239 logger.info("Fetching %s", revision)
240 document = storage.get(name, revision)
243 logger.info("Fetching tag %s", revision)
244 document = storage.get_by_tag(name, revision)
245 except DocumentNotFound:
248 return JSONResponse({
249 'text': document.plain_text,
250 'meta': document.meta(),
251 'revision': document.revision,
258 def revert(request, name):
259 storage = getstorage()
260 revision = request.POST['target_revision']
263 document = storage.revert(name, revision)
265 return JSONResponse({
266 'text': document.plain_text if revision != document.revision else None,
267 'meta': document.meta(),
268 'revision': document.revision,
270 except DocumentNotFound:
274 def gallery(request, directory):
277 smart_unicode(settings.MEDIA_URL),
278 smart_unicode(settings.FILEBROWSER_DIRECTORY),
279 smart_unicode(directory)))
281 base_dir = os.path.join(
282 smart_unicode(settings.MEDIA_ROOT),
283 smart_unicode(settings.FILEBROWSER_DIRECTORY),
284 smart_unicode(directory))
286 def map_to_url(filename):
287 return "%s/%s" % (base_url, smart_unicode(filename))
289 def is_image(filename):
290 return os.path.splitext(f)[1].lower() in (u'.jpg', u'.jpeg', u'.png')
292 images = [map_to_url(f) for f in map(smart_unicode, os.listdir(base_dir)) if is_image(f)]
294 return JSONResponse(images)
295 except (IndexError, OSError):
296 logger.exception("Unable to fetch gallery")
302 def diff(request, name):
303 storage = getstorage()
305 revA = int(request.GET.get('from', 0))
306 revB = int(request.GET.get('to', 0))
309 revA, revB = revB, revA
314 docA = storage.get_or_404(name, int(revA))
315 docB = storage.get_or_404(name, int(revB))
317 return http.HttpResponse(nice_diff.html_diff_table(docA.plain_text.splitlines(),
318 docB.plain_text.splitlines(), context=3))
323 def history(request, name):
324 storage = getstorage()
327 changesets = list(storage.history(name))
329 return JSONResponse(changesets)
333 @ajax_require_permission('wiki.can_change_tags')
334 def add_tag(request, name):
335 name = normalize_name(name)
336 storage = getstorage()
338 form = DocumentTagForm(request.POST, prefix="addtag")
340 doc = storage.get_or_404(form.cleaned_data['id'])
341 doc.add_tag(tag=form.cleaned_data['tag'],
342 revision=form.cleaned_data['revision'],
343 author=request.user.username)
344 return JSONResponse({"message": _("Tag added")})
346 return JSONFormInvalid(form)
350 @ajax_require_permission('wiki.can_publish')
351 def publish(request, name):
352 name = normalize_name(name)
354 storage = getstorage()
355 document = storage.get_by_tag(name, "ready_to_publish")
357 api = wlapi.WLAPI(**settings.WL_API_CONFIG)
360 return JSONResponse({"result": api.publish_book(document)})
361 except wlapi.APICallException, e:
362 return JSONServerError({"message": str(e)})
366 prefix = request.GET.get('q', '')
367 return http.HttpResponse('\n'.join([str(t) for t in Theme.objects.filter(name__istartswith=prefix)]))