7417235beca9916bbae853620c1a3e57877aab4e
[redakcja.git] / apps / wiki / views.py
1 import os
2 import functools
3 import logging
4 logger = logging.getLogger("fnp.wiki")
5
6 from django.conf import settings
7
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
14
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 _
20
21
22 #
23 # Quick hack around caching problems, TODO: use ETags
24 #
25 from django.views.decorators.cache import never_cache
26
27 import wlapi
28 import nice_diff
29 import operator
30
31 MAX_LAST_DOCS = 10
32
33
34 def normalized_name(view):
35
36     @functools.wraps(view)
37     def decorated(request, name, *args):
38         normalized = normalize_name(name)
39         logger.debug('View check %r -> %r', name, normalized)
40
41         if normalized != name:
42             return http.HttpResponseRedirect(
43                         reverse('wiki_' + view.__name__, kwargs={'name': normalized}))
44
45         return view(request, name, *args)
46
47     return decorated
48
49
50 @never_cache
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),
56     })
57
58
59 @never_cache
60 @normalized_name
61 def editor(request, name, template_name='wiki/document_details.html'):
62     storage = getstorage()
63
64     try:
65         document = storage.get(name)
66     except DocumentNotFound:
67         return http.HttpResponseRedirect(reverse("wiki_create_missing", args=[name]))
68
69     access_time = datetime.now()
70     last_documents = request.session.get("wiki_last_docs", {})
71     last_documents[name] = access_time
72
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
77
78     return direct_to_template(request, template_name, extra_context={
79         'document': document,
80         'document_name': document.name,
81         'document_info': document.info,
82         'document_meta': document.meta,
83         'forms': {
84             "text_save": DocumentTextSaveForm(prefix="textsave"),
85             "add_tag": DocumentTagForm(prefix="addtag"),
86         },
87         'REDMINE_URL': settings.REDMINE_URL,
88     })
89
90
91 @require_GET
92 @normalized_name
93 def editor_readonly(request, name, template_name='wiki/document_details_readonly.html'):
94     name = normalize_name(name)
95     storage = getstorage()
96
97     try:
98         revision = request.GET['revision']
99         document = storage.get(name, revision)
100     except (KeyError, DocumentNotFound):
101         raise http.Http404
102
103     access_time = datetime.now()
104     last_documents = request.session.get("wiki_last_docs", {})
105     last_documents[name] = access_time
106
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
111
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,
118     })
119
120
121 @normalized_name
122 def create_missing(request, name):
123     storage = getstorage()
124
125     if request.method == "POST":
126         form = DocumentCreateForm(request.POST, request.FILES)
127         if form.is_valid():
128             doc = storage.create_document(
129                 name=form.cleaned_data['id'],
130                 text=form.cleaned_data['text'],
131             )
132
133             return http.HttpResponseRedirect(reverse("wiki_details", args=[doc.name]))
134     else:
135         form = DocumentCreateForm(initial={
136                 "id": name.replace(" ", "_"),
137                 "title": name.title(),
138         })
139
140     return direct_to_template(request, "wiki/document_create_missing.html", extra_context={
141         "document_name": name,
142         "form": form,
143     })
144
145
146 def upload(request):
147     storage = getstorage()
148
149     if request.method == "POST":
150         form = DocumentsUploadForm(request.POST, request.FILES)
151         if form.is_valid():
152             zip = form.cleaned_data['zip']
153             skipped_list = []
154             ok_list = []
155             error_list = []
156             titles = {}
157             existing = storage.all()
158             for filename in zip.namelist():
159                 if filename[-1] == '/':
160                     continue
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.')))
168                 else:
169                     ok_list.append((filename, title))
170                     titles[title] = filename
171             if not error_list:
172                 for filename, title in ok_list:
173                     storage.create_document(
174                         name=title,
175                         text=zip.read(filename)
176                     )
177
178             return direct_to_template(request, "wiki/document_upload.html", extra_context={
179                 "form": form,
180                 "ok_list": ok_list,
181                 "skipped_list": skipped_list,
182                 "error_list": error_list,
183             })
184                 #doc = storage.create_document(
185                 #    name=base,
186                 #    text=form.cleaned_data['text'],
187
188             
189             return http.HttpResponse('\n'.join(yeslist) + '\n\n' + '\n'.join(nolist))
190     else:
191         form = DocumentsUploadForm()
192
193     return direct_to_template(request, "wiki/document_upload.html", extra_context={
194         "form": form,
195     })
196
197
198 @never_cache
199 @normalized_name
200 def text(request, name):
201     storage = getstorage()
202
203     if request.method == 'POST':
204         form = DocumentTextSaveForm(request.POST, prefix="textsave")
205         if form.is_valid():
206             revision = form.cleaned_data['parent_revision']
207             document = storage.get_or_404(name, revision)          
208             document.text = form.cleaned_data['text']
209             comment = form.cleaned_data['comment']
210             if form.cleaned_data['stage_completed']:        
211                 comment += '\n#stage-finished: %s\n' % form.cleaned_data['stage_completed']         
212             if request.user.is_authenticated():
213                 author_name = request.user
214                 author_email = request.user.email
215             else:
216                 author_name = form.cleaned_data['author_name']
217                 author_email = form.cleaned_data['author_email']
218             author = "%s <%s>" % (author_name, author_email)
219             storage.put(document, author=author, comment=comment, parent=revision)           
220             document = storage.get(name)          
221             return JSONResponse({
222                 'text': document.plain_text if revision != document.revision else None,
223                 'meta': document.meta(),
224                 'revision': document.revision,
225             })
226         else:
227             return JSONFormInvalid(form)
228     else:
229         revision = request.GET.get("revision", None)
230
231         try:
232             try:
233                 revision = revision and int(revision)
234                 logger.info("Fetching %s", revision)
235                 document = storage.get(name, revision)
236             except ValueError:
237                 # treat as a tag
238                 logger.info("Fetching tag %s", revision)
239                 document = storage.get_by_tag(name, revision)
240         except DocumentNotFound:
241             raise http.Http404
242
243         return JSONResponse({
244             'text': document.plain_text,
245             'meta': document.meta(),
246             'revision': document.revision,
247         })
248
249
250 @never_cache
251 @normalized_name
252 @require_POST
253 def revert(request, name):
254     storage = getstorage()
255     revision = request.POST['target_revision']
256
257     try:
258         document = storage.revert(name, revision)
259
260         return JSONResponse({
261             'text': document.plain_text if revision != document.revision else None,
262             'meta': document.meta(),
263             'revision': document.revision,
264         })
265     except DocumentNotFound:
266         raise http.Http404
267
268 @never_cache
269 def gallery(request, directory):
270     try:
271         base_url = ''.join((
272                         smart_unicode(settings.MEDIA_URL),
273                         smart_unicode(settings.FILEBROWSER_DIRECTORY),
274                         smart_unicode(directory)))
275
276         base_dir = os.path.join(
277                     smart_unicode(settings.MEDIA_ROOT),
278                     smart_unicode(settings.FILEBROWSER_DIRECTORY),
279                     smart_unicode(directory))
280
281         def map_to_url(filename):
282             return "%s/%s" % (base_url, smart_unicode(filename))
283
284         def is_image(filename):
285             return os.path.splitext(f)[1].lower() in (u'.jpg', u'.jpeg', u'.png')
286
287         images = [map_to_url(f) for f in map(smart_unicode, os.listdir(base_dir)) if is_image(f)]
288         images.sort()
289         return JSONResponse(images)
290     except (IndexError, OSError):
291         logger.exception("Unable to fetch gallery")
292         raise http.Http404
293
294
295 @never_cache
296 @normalized_name
297 def diff(request, name):
298     storage = getstorage()
299
300     revA = int(request.GET.get('from', 0))
301     revB = int(request.GET.get('to', 0))
302
303     if revA > revB:
304         revA, revB = revB, revA
305
306     if revB == 0:
307         revB = None
308
309     docA = storage.get_or_404(name, int(revA))
310     docB = storage.get_or_404(name, int(revB))
311
312     return http.HttpResponse(nice_diff.html_diff_table(docA.plain_text.splitlines(),
313                                          docB.plain_text.splitlines(), context=3))
314
315
316 @never_cache
317 @normalized_name
318 def history(request, name):
319     storage = getstorage()
320
321     # TODO: pagination
322     changesets = list(storage.history(name))
323
324     return JSONResponse(changesets)
325
326
327 @require_POST
328 @ajax_require_permission('wiki.can_change_tags')
329 def add_tag(request, name):
330     name = normalize_name(name)
331     storage = getstorage()
332
333     form = DocumentTagForm(request.POST, prefix="addtag")
334     if form.is_valid():
335         doc = storage.get_or_404(form.cleaned_data['id'])
336         doc.add_tag(tag=form.cleaned_data['tag'],
337                     revision=form.cleaned_data['revision'],
338                     author=request.user.username)
339         return JSONResponse({"message": _("Tag added")})
340     else:
341         return JSONFormInvalid(form)
342
343
344 @require_POST
345 @ajax_require_permission('wiki.can_publish')
346 def publish(request, name):
347     name = normalize_name(name)
348
349     storage = getstorage()
350     document = storage.get_by_tag(name, "ready_to_publish")
351
352     api = wlapi.WLAPI(**settings.WL_API_CONFIG)
353
354     try:
355         return JSONResponse({"result": api.publish_book(document)})
356     except wlapi.APICallException, e:
357         return JSONServerError({"message": str(e)})
358
359
360 def themes(request):
361     prefix = request.GET.get('q', '')
362     return http.HttpResponse('\n'.join([str(t) for t in Theme.objects.filter(name__istartswith=prefix)]))