4 logger = logging.getLogger("fnp.wiki_img")
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
15 from catalogue.models import Image
16 from wiki_img.forms import DocumentTextSaveForm
19 # Quick hack around caching problems, TODO: use ETags
21 from django.views.decorators.cache import never_cache
25 def editor(request, slug, template_name='wiki_img/document_details.html'):
26 doc = get_object_or_404(Image, slug=slug)
28 return direct_to_template(request, template_name, extra_context={
31 "text_save": DocumentTextSaveForm(user=request.user, prefix="textsave"),
33 'REDMINE_URL': settings.REDMINE_URL,
38 def editor_readonly(request, slug, template_name='wiki_img/document_details_readonly.html'):
39 doc = get_object_or_404(Image, slug=slug)
41 revision = request.GET['revision']
47 return direct_to_template(request, template_name, extra_context={
51 'REDMINE_URL': settings.REDMINE_URL,
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")
61 if request.user.is_authenticated():
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)
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,
78 description=form.cleaned_data['comment'],
80 author_name=form.cleaned_data['author_name'],
81 author_email=form.cleaned_data['author_email'],
82 publishable=publishable,
84 revision = doc.revision()
86 'text': doc.materialize() if parent_revision != revision else None,
91 return JSONFormInvalid(form)
93 revision = request.GET.get("revision", None)
96 revision = int(revision)
97 except (ValueError, TypeError):
98 revision = doc.revision()
100 if revision is not None:
101 text = doc.at_revision(revision).materialize()
105 return JSONResponse({
108 'revision': revision,
113 def history(request, chunk_id):
115 doc = get_object_or_404(Image, pk=chunk_id)
116 if not doc.accessible(request):
117 return HttpResponseForbidden("Not authorized.")
120 for change in doc.history().reverse():
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()),
129 return JSONResponse(changes)