+ doc = Chunk.get(slug, chunk)
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
+ raise Http404
+
+ if request.method == "POST":
+ form = forms.ChunkAddForm(request.POST, instance=doc)
+ if form.is_valid():
+ if request.user.is_authenticated():
+ creator = request.user
+ else:
+ creator = None
+ doc.split(creator=creator,
+ slug=form.cleaned_data['slug'],
+ comment=form.cleaned_data['comment'],
+ )
+
+ return http.HttpResponseRedirect(doc.book.get_absolute_url())
+ else:
+ form = forms.ChunkAddForm(initial={
+ "slug": str(doc.number + 1),
+ "comment": "cz. %d" % (doc.number + 1, ),
+ })
+
+ return direct_to_template(request, "wiki/chunk_add.html", extra_context={
+ "chunk": doc,
+ "form": form,
+ })
+
+
+def chunk_edit(request, slug, chunk):
+ try:
+ doc = Chunk.get(slug, chunk)
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
+ raise Http404
+ if request.method == "POST":
+ form = forms.ChunkForm(request.POST, instance=doc)
+ if form.is_valid():
+ form.save()
+ return http.HttpResponseRedirect(doc.book.get_absolute_url())
+ else:
+ form = forms.ChunkForm(instance=doc)
+ return direct_to_template(request, "wiki/chunk_edit.html", extra_context={
+ "chunk": doc,
+ "form": form,
+ })
+
+
+def book_append(request, slug):
+ book = get_object_or_404(Book, slug=slug)
+ if request.method == "POST":
+ form = forms.BookAppendForm(request.POST)
+ if form.is_valid():
+ append_to = form.cleaned_data['append_to']
+ append_to.append(book)
+ return http.HttpResponseRedirect(append_to.get_absolute_url())
+ else:
+ form = forms.BookAppendForm()
+ return direct_to_template(request, "wiki/book_append_to.html", extra_context={
+ "book": book,
+ "form": form,
+ })
+
+
+def book_edit(request, slug):
+ book = get_object_or_404(Book, slug=slug)
+ if request.method == "POST":
+ form = forms.BookForm(request.POST, instance=book)
+ if form.is_valid():
+ form.save()
+ return http.HttpResponseRedirect(book.get_absolute_url())
+ else:
+ form = forms.BookForm(instance=book)
+ return direct_to_template(request, "wiki/book_edit.html", extra_context={
+ "book": book,
+ "form": form,
+ })
+
+
+@require_POST
+@ajax_require_permission('wiki.can_change_tags')
+def add_tag(request, slug, chunk=None):
+ form = forms.DocumentTagForm(request.POST, prefix="addtag")
+ if form.is_valid():
+ try:
+ doc = Chunk.get(slug, chunk)
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
+ raise Http404
+
+ tag = form.cleaned_data['tag']
+ revision = form.cleaned_data['revision']
+ doc.at_revision(revision).tags.add(tag)
+ return JSONResponse({"message": _("Tag added")})
+ else:
+ return JSONFormInvalid(form)
+
+
+@require_POST
+@ajax_require_permission('wiki.can_pubmark')
+def pubmark(request, slug, chunk=None):
+ form = forms.DocumentPubmarkForm(request.POST, prefix="pubmark")
+ if form.is_valid():
+ try:
+ doc = Chunk.get(slug, chunk)
+ except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
+ raise Http404
+
+ revision = form.cleaned_data['revision']
+ publishable = form.cleaned_data['publishable']
+ change = doc.at_revision(revision)
+ print publishable, change.publishable
+ if publishable != change.publishable:
+ change.publishable = publishable
+ change.save()
+ return JSONResponse({"message": _("Revision marked")})
+ else:
+ return JSONResponse({"message": _("Nothing changed")})
+ else:
+ return JSONFormInvalid(form)
+
+
+@require_POST
+@login_required
+def publish(request, slug):
+ book = get_object_or_404(Book, slug=slug)
+ try:
+ ret = api_call(request.user, "books", {"book_xml": book.materialize()})
+ except BaseException, e:
+ return http.HttpResponse(e)
+ else:
+ book.last_published = datetime.now()
+ book.save()
+ return http.HttpResponseRedirect(book.get_absolute_url())
+
+
+def themes(request):
+ prefix = request.GET.get('q', '')
+ return http.HttpResponse('\n'.join([str(t) for t in Theme.objects.filter(name__istartswith=prefix)]))