9e87f6607e5877ca021b844221ea109f07ef5cd2
[redakcja.git] / apps / wiki_img / views.py
1 import os
2 import functools
3 import logging
4 logger = logging.getLogger("fnp.wiki_img")
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.views.decorators.http import require_GET
12 from django.conf import settings
13 from django.utils.formats import localize
14
15 from catalogue.models import Image
16 from wiki_img.forms import DocumentTextSaveForm
17
18 #
19 # Quick hack around caching problems, TODO: use ETags
20 #
21 from django.views.decorators.cache import never_cache
22
23
24 @never_cache
25 def editor(request, slug, template_name='wiki_img/document_details.html'):
26     doc = get_object_or_404(Image, slug=slug)
27
28     return direct_to_template(request, template_name, extra_context={
29         'document': doc,
30         'forms': {
31             "text_save": DocumentTextSaveForm(user=request.user, prefix="textsave"),
32         },
33         'REDMINE_URL': settings.REDMINE_URL,
34     })
35
36
37 @require_GET
38 def editor_readonly(request, slug, template_name='wiki_img/document_details_readonly.html'):
39     doc = get_object_or_404(Image, slug=slug)
40     try:
41         revision = request.GET['revision']
42     except (KeyError):
43         raise Http404
44
45
46
47     return direct_to_template(request, template_name, extra_context={
48         'document': doc,
49         'revision': revision,
50         'readonly': True,
51         'REDMINE_URL': settings.REDMINE_URL,
52     })
53
54
55 @never_cache
56 def text(request, image_id):
57     doc = get_object_or_404(Image, pk=image_id)
58     if request.method == 'POST':
59         form = DocumentTextSaveForm(request.POST, user=request.user, prefix="textsave")
60         if form.is_valid():
61             if request.user.is_authenticated():
62                 author = request.user
63             else:
64                 author = None
65             text = form.cleaned_data['text']
66             parent_revision = form.cleaned_data['parent_revision']
67             if parent_revision is not None:
68                 parent = doc.at_revision(parent_revision)
69             else:
70                 parent = None
71             stage = form.cleaned_data['stage_completed']
72             tags = [stage] if stage else []
73             publishable = (form.cleaned_data['publishable'] and
74                     request.user.has_perm('catalogue.can_pubmark'))
75             doc.commit(author=author,
76                        text=text,
77                        parent=parent,
78                        description=form.cleaned_data['comment'],
79                        tags=tags,
80                        author_name=form.cleaned_data['author_name'],
81                        author_email=form.cleaned_data['author_email'],
82                        publishable=publishable,
83                        )
84             revision = doc.revision()
85             return JSONResponse({
86                 'text': doc.materialize() if parent_revision != revision else None,
87                 'meta': {},
88                 'revision': revision,
89             })
90         else:
91             return JSONFormInvalid(form)
92     else:
93         revision = request.GET.get("revision", None)
94         
95         try:
96             revision = int(revision)
97         except (ValueError, TypeError):
98             revision = doc.revision()
99
100         if revision is not None:
101             text = doc.at_revision(revision).materialize()
102         else:
103             text = ''
104
105         return JSONResponse({
106             'text': text,
107             'meta': {},
108             'revision': revision,
109         })
110
111
112 @never_cache
113 def history(request, chunk_id):
114     # TODO: pagination
115     doc = get_object_or_404(Image, pk=chunk_id)
116     if not doc.accessible(request):
117         return HttpResponseForbidden("Not authorized.")
118
119     changes = []
120     for change in doc.history().reverse():
121         changes.append({
122                 "version": change.revision,
123                 "description": change.description,
124                 "author": change.author_str(),
125                 "date": localize(change.created_at),
126                 "publishable": _("Publishable") + "\n" if change.publishable else "",
127                 "tag": ',\n'.join(unicode(tag) for tag in change.tags.all()),
128             })
129     return JSONResponse(changes)