c11549ab63d3401319874d80ba85705eb1b60234
[redakcja.git] / apps / wiki_img / views.py
1 import os
2 import functools
3 import logging
4 logger = logging.getLogger("fnp.wiki")
5
6 from django.views.generic.simple import direct_to_template
7 from django.core.urlresolvers import reverse
8 from wiki.helpers import JSONResponse
9 from django import http
10 from django.shortcuts import get_object_or_404
11 from django.conf import settings
12
13 from wiki_img.models import ImageDocument
14 from wiki_img.forms import DocumentTextSaveForm
15
16 #
17 # Quick hack around caching problems, TODO: use ETags
18 #
19 from django.views.decorators.cache import never_cache
20
21
22 @never_cache
23 def editor(request, slug, template_name='wiki_img/document_details.html'):
24     doc = get_object_or_404(ImageDocument, slug=slug)
25
26     return direct_to_template(request, template_name, extra_context={
27         'document': doc,
28         'forms': {
29             "text_save": DocumentTextSaveForm(prefix="textsave"),
30         },
31         'REDMINE_URL': settings.REDMINE_URL,
32     })
33
34
35 @never_cache
36 def text(request, slug):
37     if request.method == 'POST':
38         form = DocumentTextSaveForm(request.POST, prefix="textsave")
39         if form.is_valid():
40             document = get_object_or_404(ImageDocument, slug=slug)
41             revision = form.cleaned_data['parent_revision']
42
43             comment = form.cleaned_data['comment']
44
45             if request.user.is_authenticated():
46                 user = request.user
47             else:
48                 user = None
49
50             document.doc.commit(
51                 parent=revision,
52                 text=form.cleaned_data['text'],
53                 author=user,
54                 description=comment
55             )
56
57             return JSONResponse({
58                 'text': document.doc.materialize(),
59                 'revision': document.doc.change_set.count(),
60             })
61         else:
62             return JSONFormInvalid(form)
63     else:
64         doc = get_object_or_404(ImageDocument, slug=slug)
65         return JSONResponse({
66             'text': doc.doc.materialize(),
67             'revision': doc.doc.change_set.count()
68         })
69