2 from StringIO import StringIO
4 logger = logging.getLogger("fnp.wiki")
8 from django.conf import settings
10 from django.contrib.auth.models import User
11 from django.contrib.auth.decorators import login_required
12 from django.db.models import Count
13 from django.views.generic.simple import direct_to_template
14 from django.views.decorators.http import require_POST, require_GET
15 from django.core.urlresolvers import reverse
16 from wiki.helpers import (JSONResponse, JSONFormInvalid, JSONServerError,
17 ajax_require_permission, recursive_groupby, active_tab)
18 from wiki import helpers
19 from django import http
20 from django.shortcuts import get_object_or_404, redirect
21 from django.http import Http404
23 from wiki.models import Book, Chunk, Theme
24 from wiki import forms
25 from datetime import datetime
26 from django.utils.encoding import smart_unicode
27 from django.utils.translation import ugettext_lazy as _
28 from django.utils.decorators import decorator_from_middleware
29 from django.middleware.gzip import GZipMiddleware
33 from wiki import xml_tools
34 from apiclient import api_call
37 # Quick hack around caching problems, TODO: use ETags
39 from django.views.decorators.cache import never_cache
49 def document_list(request):
50 return direct_to_template(request, 'wiki/document_list.html', extra_context={
51 'books': [helpers.BookChunks(b) for b in Book.objects.all()],
52 'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
53 key=lambda x: x[1]['time'], reverse=True),
57 @active_tab('unassigned')
59 def unassigned(request):
60 chunks = Chunk.objects.filter(user=None).order_by('book__title', 'book', 'number')
64 if chunk.book != book:
66 books.append(helpers.ChoiceChunks(book, [chunk]))
68 books[-1].chunks.append(chunk)
70 return direct_to_template(request, 'wiki/document_list.html', extra_context={
72 'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
73 key=lambda x: x[1]['time'], reverse=True),
78 def user(request, username=None):
80 if request.user.is_authenticated():
85 user = get_object_or_404(User, username=username)
87 chunks = Chunk.objects.filter(user=user).order_by('book__title', 'number')
91 if chunk.book != book:
93 books.append(helpers.ChoiceChunks(book, [chunk]))
95 books[-1].chunks.append(chunk)
97 return direct_to_template(request, 'wiki/document_list.html', extra_context={
99 'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
100 key=lambda x: x[1]['time'], reverse=True),
102 my = login_required(active_tab('my')(user))
107 return direct_to_template(request, 'wiki/user_list.html', extra_context={
108 'users': User.objects.all().annotate(count=Count('document')).order_by(
109 '-count', 'last_name', 'first_name'),
114 def editor(request, slug, chunk=None, template_name='wiki/document_details.html'):
116 chunk = Chunk.get(slug, chunk)
117 except Chunk.MultipleObjectsReturned:
120 except Chunk.DoesNotExist:
123 book = Book.objects.get(slug=slug)
124 except Book.DoesNotExist:
125 return http.HttpResponseRedirect(reverse("wiki_create_missing", args=[slug]))
129 access_time = datetime.now()
130 last_books = request.session.get("wiki_last_books", {})
131 last_books[slug, chunk.slug] = {
133 'title': chunk.pretty_name(),
136 if len(last_books) > MAX_LAST_DOCS:
137 oldest_key = min(last_books, key=lambda x: last_books[x]['time'])
138 del last_books[oldest_key]
139 request.session['wiki_last_books'] = last_books
141 return direct_to_template(request, template_name, extra_context={
144 "text_save": forms.DocumentTextSaveForm(prefix="textsave"),
145 "text_revert": forms.DocumentTextRevertForm(prefix="textrevert"),
146 "add_tag": forms.DocumentTagForm(prefix="addtag"),
147 "pubmark": forms.DocumentPubmarkForm(prefix="pubmark"),
149 'REDMINE_URL': settings.REDMINE_URL,
154 def editor_readonly(request, slug, chunk=None, template_name='wiki/document_details_readonly.html'):
156 chunk = Chunk.get(slug, chunk)
157 revision = request.GET['revision']
158 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist, KeyError):
161 access_time = datetime.now()
162 last_books = request.session.get("wiki_last_books", {})
163 last_books[slug, chunk.slug] = {
165 'title': chunk.book.title,
168 if len(last_books) > MAX_LAST_DOCS:
169 oldest_key = min(last_books, key=lambda x: last_books[x]['time'])
170 del last_books[oldest_key]
171 request.session['wiki_last_books'] = last_books
173 return direct_to_template(request, template_name, extra_context={
175 'revision': revision,
177 'REDMINE_URL': settings.REDMINE_URL,
181 @active_tab('create')
182 def create_missing(request, slug=None):
185 slug = slug.replace(' ', '-')
187 if request.method == "POST":
188 form = forms.DocumentCreateForm(request.POST, request.FILES)
191 if request.user.is_authenticated():
192 creator = request.user
195 book = Book.create(creator=creator,
196 slug=form.cleaned_data['slug'],
197 title=form.cleaned_data['title'],
198 text=form.cleaned_data['text'],
201 return http.HttpResponseRedirect(reverse("wiki_editor", args=[book.slug]))
203 form = forms.DocumentCreateForm(initial={
205 "title": slug.replace('-', ' ').title(),
208 return direct_to_template(request, "wiki/document_create_missing.html", extra_context={
214 @active_tab('upload')
216 if request.method == "POST":
217 form = forms.DocumentsUploadForm(request.POST, request.FILES)
221 if request.user.is_authenticated():
222 creator = request.user
226 zip = form.cleaned_data['zip']
231 existing = [book.slug for book in Book.objects.all()]
232 for filename in zip.namelist():
233 if filename[-1] == '/':
235 title = os.path.basename(filename)[:-4]
236 slug = slughifi(title)
237 if not (slug and filename.endswith('.xml')):
238 skipped_list.append(filename)
240 error_list.append((filename, slug, _('Slug already used for %s' % slugs[slug])))
241 elif slug in existing:
242 error_list.append((filename, slug, _('Slug already used in repository.')))
245 zip.read(filename).decode('utf-8') # test read
246 ok_list.append((filename, slug, title))
247 except UnicodeDecodeError:
248 error_list.append((filename, title, _('File should be UTF-8 encoded.')))
249 slugs[slug] = filename
252 for filename, slug, title in ok_list:
253 Book.create(creator=creator,
256 text=zip.read(filename).decode('utf-8'),
259 return direct_to_template(request, "wiki/document_upload.html", extra_context={
262 "skipped_list": skipped_list,
263 "error_list": error_list,
266 form = forms.DocumentsUploadForm()
268 return direct_to_template(request, "wiki/document_upload.html", extra_context={
274 @decorator_from_middleware(GZipMiddleware)
275 def text(request, slug, chunk=None):
277 doc = Chunk.get(slug, chunk)
278 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
281 if request.method == 'POST':
282 form = forms.DocumentTextSaveForm(request.POST, prefix="textsave")
284 if request.user.is_authenticated():
285 author = request.user
288 text = form.cleaned_data['text']
289 parent_revision = form.cleaned_data['parent_revision']
290 parent = doc.at_revision(parent_revision)
291 stage = form.cleaned_data['stage_completed']
292 tags = [stage] if stage else []
293 doc.commit(author=author,
296 description=form.cleaned_data['comment'],
299 revision = doc.revision()
300 return JSONResponse({
301 'text': doc.materialize() if parent_revision != revision else None,
303 'revision': revision,
306 return JSONFormInvalid(form)
308 revision = request.GET.get("revision", None)
311 revision = int(revision)
312 except (ValueError, TypeError):
315 return JSONResponse({
316 'text': doc.at_revision(revision).materialize(),
318 'revision': revision if revision else doc.revision(),
323 def book_xml(request, slug):
324 xml = get_object_or_404(Book, slug=slug).materialize()
326 response = http.HttpResponse(xml, content_type='application/xml', mimetype='application/wl+xml')
327 response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
332 def book_txt(request, slug):
333 xml = get_object_or_404(Book, slug=slug).materialize()
336 librarian.text.transform(StringIO(xml), output)
337 text = output.getvalue()
338 response = http.HttpResponse(text, content_type='text/plain', mimetype='text/plain')
339 response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug
344 def book_html(request, slug):
345 xml = get_object_or_404(Book, slug=slug).materialize()
348 librarian.html.transform(StringIO(xml), output, parse_dublincore=False,
350 html = output.getvalue()
351 response = http.HttpResponse(html, content_type='text/html', mimetype='text/html')
357 def revert(request, slug, chunk=None):
358 form = forms.DocumentTextRevertForm(request.POST, prefix="textrevert")
361 doc = Chunk.get(slug, chunk)
362 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
365 revision = form.cleaned_data['revision']
367 comment = form.cleaned_data['comment']
368 comment += "\n#revert to %s" % revision
370 if request.user.is_authenticated():
371 author = request.user
375 before = doc.revision()
376 logger.info("Reverting %s to %s", slug, revision)
377 doc.at_revision(revision).revert(author=author, description=comment)
379 return JSONResponse({
380 'text': doc.materialize() if before != doc.revision() else None,
382 'revision': doc.revision(),
385 return JSONFormInvalid(form)
389 def gallery(request, directory):
392 smart_unicode(settings.MEDIA_URL),
393 smart_unicode(settings.FILEBROWSER_DIRECTORY),
394 smart_unicode(directory)))
396 base_dir = os.path.join(
397 smart_unicode(settings.MEDIA_ROOT),
398 smart_unicode(settings.FILEBROWSER_DIRECTORY),
399 smart_unicode(directory))
401 def map_to_url(filename):
402 return "%s/%s" % (base_url, smart_unicode(filename))
404 def is_image(filename):
405 return os.path.splitext(f)[1].lower() in (u'.jpg', u'.jpeg', u'.png')
407 images = [map_to_url(f) for f in map(smart_unicode, os.listdir(base_dir)) if is_image(f)]
409 return JSONResponse(images)
410 except (IndexError, OSError):
411 logger.exception("Unable to fetch gallery")
416 def diff(request, slug, chunk=None):
417 revA = int(request.GET.get('from', 0))
418 revB = int(request.GET.get('to', 0))
421 revA, revB = revB, revA
427 doc = Chunk.get(slug, chunk)
428 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
430 docA = doc.at_revision(revA).materialize()
431 docB = doc.at_revision(revB).materialize()
433 return http.HttpResponse(nice_diff.html_diff_table(docA.splitlines(),
434 docB.splitlines(), context=3))
438 def revision(request, slug, chunk=None):
440 doc = Chunk.get(slug, chunk)
441 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
443 return http.HttpResponse(str(doc.revision()))
447 def history(request, slug, chunk=None):
450 doc = Chunk.get(slug, chunk)
451 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
455 for change in doc.history().order_by('-created_at'):
457 "version": change.revision,
458 "description": change.description,
459 "author": change.author_str(),
460 "date": change.created_at,
461 "publishable": "Publishable\n" if change.publishable else "",
462 "tag": ',\n'.join(unicode(tag) for tag in change.tags.all()),
464 return JSONResponse(changes)
467 def book(request, slug):
468 book = get_object_or_404(Book, slug=slug)
470 # TODO: most of this should go somewhere else
472 # do we need some automation?
476 choose_master = False
479 for i, chunk in enumerate(book):
485 graded = xml_tools.GradedText(chunk.materialize())
487 master = graded.master()
488 if first_master is None:
489 first_master = master
490 elif master != first_master:
491 chunk_dict['fix'].append('bad-master')
493 if i > 0 and not graded.has_trim_begin():
494 chunk_dict['fix'].append('trim-begin')
495 if i < length - 1 and not graded.has_trim_end():
496 chunk_dict['fix'].append('trim-end')
498 if chunk_dict['fix']:
499 chunk_dict['grade'] = 'wl-fix'
501 chunk_dict['grade'] = 'wl'
503 elif graded.is_broken_wl():
504 chunk_dict['grade'] = 'wl-broken'
505 elif graded.is_xml():
506 chunk_dict['grade'] = 'xml'
508 chunk_dict['grade'] = 'plain'
509 chunk_dict['fix'].append('wl')
512 if chunk_dict['fix']:
514 chunks.append(chunk_dict)
516 if first_master or not need_fixing:
517 choose_master = False
519 if request.method == "POST":
520 form = forms.ChooseMasterForm(request.POST)
521 if not choose_master or form.is_valid():
523 first_master = form.cleaned_data['master']
525 # do the actual fixing
530 text = c['chunk'].materialize()
532 if fix == 'bad-master':
533 text = xml_tools.change_master(text, first_master)
534 elif fix == 'trim-begin':
535 text = xml_tools.add_trim_begin(text)
536 elif fix == 'trim-end':
537 text = xml_tools.add_trim_end(text)
539 text = xml_tools.basic_structure(text, first_master)
540 author = request.user if request.user.is_authenticated() else None
541 description = "auto-fix: " + ", ".join(c['fix'])
542 c['chunk'].commit(text=text, author=author,
543 description=description)
545 return http.HttpResponseRedirect(book.get_absolute_url())
547 form = forms.ChooseMasterForm()
551 return direct_to_template(request, "wiki/book_detail.html", extra_context={
554 "need_fixing": need_fixing,
555 "choose_master": choose_master,
556 "first_master": first_master,
561 def chunk_add(request, slug, chunk):
563 doc = Chunk.get(slug, chunk)
564 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
567 if request.method == "POST":
568 form = forms.ChunkAddForm(request.POST, instance=doc)
570 if request.user.is_authenticated():
571 creator = request.user
574 doc.split(creator=creator,
575 slug=form.cleaned_data['slug'],
576 comment=form.cleaned_data['comment'],
579 return http.HttpResponseRedirect(doc.book.get_absolute_url())
581 form = forms.ChunkAddForm(initial={
582 "slug": str(doc.number + 1),
583 "comment": "cz. %d" % (doc.number + 1, ),
586 return direct_to_template(request, "wiki/chunk_add.html", extra_context={
592 def chunk_edit(request, slug, chunk):
594 doc = Chunk.get(slug, chunk)
595 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
597 if request.method == "POST":
598 form = forms.ChunkForm(request.POST, instance=doc)
601 return http.HttpResponseRedirect(doc.book.get_absolute_url())
603 form = forms.ChunkForm(instance=doc)
604 return direct_to_template(request, "wiki/chunk_edit.html", extra_context={
610 def book_append(request, slug):
611 book = get_object_or_404(Book, slug=slug)
612 if request.method == "POST":
613 form = forms.BookAppendForm(request.POST)
615 append_to = form.cleaned_data['append_to']
616 append_to.append(book)
617 return http.HttpResponseRedirect(append_to.get_absolute_url())
619 form = forms.BookAppendForm()
620 return direct_to_template(request, "wiki/book_append_to.html", extra_context={
626 def book_edit(request, slug):
627 book = get_object_or_404(Book, slug=slug)
628 if request.method == "POST":
629 form = forms.BookForm(request.POST, instance=book)
632 return http.HttpResponseRedirect(book.get_absolute_url())
634 form = forms.BookForm(instance=book)
635 return direct_to_template(request, "wiki/book_edit.html", extra_context={
642 @ajax_require_permission('wiki.can_change_tags')
643 def add_tag(request, slug, chunk=None):
644 form = forms.DocumentTagForm(request.POST, prefix="addtag")
647 doc = Chunk.get(slug, chunk)
648 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
651 tag = form.cleaned_data['tag']
652 revision = form.cleaned_data['revision']
653 doc.at_revision(revision).tags.add(tag)
654 return JSONResponse({"message": _("Tag added")})
656 return JSONFormInvalid(form)
660 @ajax_require_permission('wiki.can_pubmark')
661 def pubmark(request, slug, chunk=None):
662 form = forms.DocumentPubmarkForm(request.POST, prefix="pubmark")
665 doc = Chunk.get(slug, chunk)
666 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
669 revision = form.cleaned_data['revision']
670 publishable = form.cleaned_data['publishable']
671 change = doc.at_revision(revision)
672 print publishable, change.publishable
673 if publishable != change.publishable:
674 change.publishable = publishable
676 return JSONResponse({"message": _("Revision marked")})
678 return JSONResponse({"message": _("Nothing changed")})
680 return JSONFormInvalid(form)
685 def publish(request, slug):
686 book = get_object_or_404(Book, slug=slug)
688 ret = api_call(request.user, "books", {"book_xml": book.materialize()})
689 except BaseException, e:
690 return http.HttpResponse(e)
692 book.last_published = datetime.now()
694 return http.HttpResponseRedirect(book.get_absolute_url())
698 prefix = request.GET.get('q', '')
699 return http.HttpResponse('\n'.join([str(t) for t in Theme.objects.filter(name__istartswith=prefix)]))