e466d6a2478d8fb401af4d5f36c9649d4c46dddf
[redakcja.git] / apps / wiki / views.py
1 import os
2
3 from django.conf import settings
4
5 from django.views.generic.simple import direct_to_template
6 from django.views.decorators.http import require_POST
7 from django.core.urlresolvers import reverse
8 from wiki.helpers import JSONResponse, JSONFormInvalid, JSONServerError, ajax_require_permission
9 from django import http
10
11 from wiki.models import getstorage, DocumentNotFound
12 from wiki.forms import DocumentTextSaveForm, DocumentTagForm, DocumentCreateForm
13 from datetime import datetime
14 from django.utils.encoding import smart_unicode
15 from django.utils.translation import ugettext_lazy as _
16
17 import wlapi
18
19 #
20 # Quick hack around caching problems, TODO: use ETags
21 #
22 from django.views.decorators.cache import never_cache
23
24 import logging
25 logger = logging.getLogger("fnp.peanut.api")
26
27 import nice_diff
28 import operator
29
30 MAX_LAST_DOCS = 10
31
32
33 @never_cache
34 def document_list(request, template_name='wiki/document_list.html'):
35     # TODO: find a way to cache "Storage All"
36     return direct_to_template(request, template_name, extra_context={
37         'document_list': getstorage().all(),
38         'last_docs': sorted(request.session.get("wiki_last_docs", {}).items(),
39                         key=operator.itemgetter(1), reverse=True),
40     })
41
42
43 @never_cache
44 def document_detail(request, name, template_name='wiki/document_details.html'):
45
46     try:
47         document = getstorage().get(name)
48     except DocumentNotFound:
49         return http.HttpResponseRedirect(reverse("wiki_create_missing", args=[name]))
50
51     access_time = datetime.now()
52     last_documents = request.session.get("wiki_last_docs", {})
53     last_documents[name] = access_time
54
55     if len(last_documents) > MAX_LAST_DOCS:
56         oldest_key = min(last_documents, key=last_documents.__getitem__)
57         del last_documents[oldest_key]
58     request.session['wiki_last_docs'] = last_documents
59
60     return direct_to_template(request, template_name, extra_context={
61         'document': document,
62         'document_name': document.name,
63         'document_info': document.info,
64         'document_meta': document.meta,
65         'forms': {
66             "text_save": DocumentTextSaveForm(prefix="textsave"),
67             "add_tag": DocumentTagForm(prefix="addtag")
68         },
69     })
70
71
72 def document_create_missing(request, name):
73     storage = getstorage()
74
75     if request.method == "POST":
76         form = DocumentCreateForm(request.POST, request.FILES)
77         if form.is_valid():
78             doc = storage.create_document(
79                 id=form.cleaned_data['id'],
80                 text=form.cleaned_data['text'],
81             )
82
83             return http.HttpResponseRedirect(reverse("wiki_details", args=[doc.name]))
84     else:
85         form = DocumentCreateForm(initial={
86                 "id": name.replace(" ", "_"),
87                 "title": name.title(),
88         })
89
90     return direct_to_template(request, "wiki/document_create_missing.html", extra_context={
91         "document_name": name,
92         "form": form,
93     })
94
95
96 @never_cache
97 def document_text(request, name):
98     storage = getstorage()
99
100     if request.method == 'POST':
101         form = DocumentTextSaveForm(request.POST, prefix="textsave")
102
103         if form.is_valid():
104             revision = form.cleaned_data['parent_revision']
105
106             document = storage.get_or_404(name, revision)
107             document.text = form.cleaned_data['text']
108
109             storage.put(document,
110                 author=form.cleaned_data['author'] or request.user.username,
111                 comment=form.cleaned_data['comment'],
112                 parent=revision,
113             )
114
115             document = storage.get(name)
116
117             return JSONResponse({
118                 'text': document.plain_text if revision != document.revision else None,
119                 'meta': document.meta(),
120                 'revision': document.revision,
121             })
122         else:
123             return JSONFormInvalid(form)
124     else:
125         revision = request.GET.get("revision", None)
126
127         try:
128             try:
129                 revision = revision and int(revision)
130                 logger.info("Fetching %s", revision)
131                 document = storage.get(name, revision)
132             except ValueError:
133                 # treat as a tag
134                 logger.info("Fetching tag %s", revision)
135                 document = storage.get_by_tag(name, revision)
136         except DocumentNotFound:
137             raise http.Http404
138
139         return JSONResponse({
140             'text': document.plain_text,
141             'meta': document.meta(),
142             'revision': document.revision,
143         })
144
145
146 @never_cache
147 def document_gallery(request, directory):
148     try:
149         base_url = ''.join((
150                         smart_unicode(settings.MEDIA_URL),
151                         smart_unicode(settings.FILEBROWSER_DIRECTORY),
152                         smart_unicode(directory)))
153
154         base_dir = os.path.join(
155                     smart_unicode(settings.MEDIA_ROOT),
156                     smart_unicode(settings.FILEBROWSER_DIRECTORY),
157                     smart_unicode(directory))
158
159         def map_to_url(filename):
160             return "%s/%s" % (base_url, smart_unicode(filename))
161
162         def is_image(filename):
163             return os.path.splitext(f)[1].lower() in (u'.jpg', u'.jpeg', u'.png')
164
165         images = [map_to_url(f) for f in map(smart_unicode, os.listdir(base_dir)) if is_image(f)]
166         images.sort()
167         return JSONResponse(images)
168     except (IndexError, OSError) as e:
169         logger.exception("Unable to fetch gallery")
170         raise http.Http404
171
172
173 @never_cache
174 def document_diff(request, name):
175     storage = getstorage()
176
177     revA = int(request.GET.get('from', 0))
178     revB = int(request.GET.get('to', 0))
179
180     if revA > revB:
181         revA, revB = revB, revA
182
183     if revB == 0:
184         revB = None
185
186     docA = storage.get_or_404(name, int(revA))
187     docB = storage.get_or_404(name, int(revB))
188
189     return http.HttpResponse(nice_diff.html_diff_table(docA.plain_text.splitlines(),
190                                          docB.plain_text.splitlines(), context=3))
191
192
193 @never_cache
194 def document_history(request, name):
195     storage = getstorage()
196
197     # TODO: pagination
198     changesets = list(storage.history(name))
199
200     return JSONResponse(changesets)
201
202
203 @require_POST
204 @ajax_require_permission('wiki.can_change_tags')
205 def document_add_tag(request, name):
206     storage = getstorage()
207
208     form = DocumentTagForm(request.POST, prefix="addtag")
209     if form.is_valid():
210         doc = storage.get_or_404(form.cleaned_data['id'])
211         doc.add_tag(tag=form.cleaned_data['tag'],
212                     revision=form.cleaned_data['revision'],
213                     author=request.user.username)
214         return JSONResponse({"message": _("Tag added")})
215     else:
216         return JSONFormInvalid(form)
217
218
219 @require_POST
220 @ajax_require_permission('wiki.can_publish')
221 def document_publish(request, name):
222     storage = getstorage()
223     document = storage.get_by_tag(name, "ready_to_publish")
224
225     api = wlapi.WLAPI(**settings.WL_API_CONFIG)
226
227     try:
228         return JSONResponse({"result": api.publish_book(document)})
229     except wlapi.APICallException, e:
230         return JSONServerError({"message": str(e)})