7bff1a055f3ff4939a8f5b9befb6d076612a2787
[redakcja.git] / apps / wiki / views.py
1 import os
2 from StringIO import StringIO
3 import logging
4 logger = logging.getLogger("fnp.wiki")
5
6 from lxml import etree
7
8 from django.conf import settings
9
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
22
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
30
31 import librarian.html
32 import librarian.text
33 from wiki import xml_tools
34 from apiclient import api_call
35
36 #
37 # Quick hack around caching problems, TODO: use ETags
38 #
39 from django.views.decorators.cache import never_cache
40
41 import nice_diff
42 import operator
43
44 MAX_LAST_DOCS = 10
45
46
47 @active_tab('all')
48 @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),
54     })
55
56
57 @active_tab('unassigned')
58 @never_cache
59 def unassigned(request):
60     chunks = Chunk.objects.filter(user=None).order_by('book__title', 'book', 'number')
61     books = []
62     book = None
63     for chunk in chunks:
64         if chunk.book != book:
65             book = chunk.book
66             books.append(helpers.ChoiceChunks(book, [chunk]))
67         else:
68             books[-1].chunks.append(chunk)
69
70     return direct_to_template(request, 'wiki/document_list.html', extra_context={
71         'books': books,
72         'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
73                         key=lambda x: x[1]['time'], reverse=True),
74     })
75
76
77 @never_cache
78 def user(request, username=None):
79     if username is None:
80         if request.user.is_authenticated():
81             user = request.user
82         else:
83             raise Http404
84     else:
85         user = get_object_or_404(User, username=username)
86
87     chunks = Chunk.objects.filter(user=user).order_by('book__title', 'number')
88     books = []
89     book = None
90     for chunk in chunks:
91         if chunk.book != book:
92             book = chunk.book
93             books.append(helpers.ChoiceChunks(book, [chunk]))
94         else:
95             books[-1].chunks.append(chunk)
96
97     return direct_to_template(request, 'wiki/document_list.html', extra_context={
98         'books': books,
99         'last_books': sorted(request.session.get("wiki_last_books", {}).items(),
100                         key=lambda x: x[1]['time'], reverse=True),
101     })
102 my = login_required(active_tab('my')(user))
103
104
105 @active_tab('users')
106 def users(request):
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'),
110     })
111
112
113 @never_cache
114 def editor(request, slug, chunk=None, template_name='wiki/document_details.html'):
115     try:
116         chunk = Chunk.get(slug, chunk)
117     except Chunk.MultipleObjectsReturned:
118         # TODO: choice page
119         raise Http404
120     except Chunk.DoesNotExist:
121         if chunk is None:
122             try:
123                 book = Book.objects.get(slug=slug)
124             except Book.DoesNotExist:
125                 return http.HttpResponseRedirect(reverse("wiki_create_missing", args=[slug]))
126         else:
127             raise Http404
128
129     access_time = datetime.now()
130     last_books = request.session.get("wiki_last_books", {})
131     last_books[slug, chunk.slug] = {
132         'time': access_time,
133         'title': chunk.pretty_name(),
134         }
135
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
140
141     return direct_to_template(request, template_name, extra_context={
142         'chunk': chunk,
143         'forms': {
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"),
148         },
149         'REDMINE_URL': settings.REDMINE_URL,
150     })
151
152
153 @require_GET
154 def editor_readonly(request, slug, chunk=None, template_name='wiki/document_details_readonly.html'):
155     try:
156         chunk = Chunk.get(slug, chunk)
157         revision = request.GET['revision']
158     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist, KeyError):
159         raise Http404
160
161     access_time = datetime.now()
162     last_books = request.session.get("wiki_last_books", {})
163     last_books[slug, chunk.slug] = {
164         'time': access_time,
165         'title': chunk.book.title,
166         }
167
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
172
173     return direct_to_template(request, template_name, extra_context={
174         'chunk': chunk,
175         'revision': revision,
176         'readonly': True,
177         'REDMINE_URL': settings.REDMINE_URL,
178     })
179
180
181 @active_tab('create')
182 def create_missing(request, slug=None):
183     if slug is None:
184         slug = ''
185     slug = slug.replace(' ', '-')
186
187     if request.method == "POST":
188         form = forms.DocumentCreateForm(request.POST, request.FILES)
189         if form.is_valid():
190             
191             if request.user.is_authenticated():
192                 creator = request.user
193             else:
194                 creator = None
195             book = Book.create(creator=creator,
196                 slug=form.cleaned_data['slug'],
197                 title=form.cleaned_data['title'],
198                 text=form.cleaned_data['text'],
199             )
200
201             return http.HttpResponseRedirect(reverse("wiki_editor", args=[book.slug]))
202     else:
203         form = forms.DocumentCreateForm(initial={
204                 "slug": slug,
205                 "title": slug.replace('-', ' ').title(),
206         })
207
208     return direct_to_template(request, "wiki/document_create_missing.html", extra_context={
209         "slug": slug,
210         "form": form,
211     })
212
213
214 @active_tab('upload')
215 def upload(request):
216     if request.method == "POST":
217         form = forms.DocumentsUploadForm(request.POST, request.FILES)
218         if form.is_valid():
219             import slughifi
220
221             if request.user.is_authenticated():
222                 creator = request.user
223             else:
224                 creator = None
225
226             zip = form.cleaned_data['zip']
227             skipped_list = []
228             ok_list = []
229             error_list = []
230             slugs = {}
231             existing = [book.slug for book in Book.objects.all()]
232             for filename in zip.namelist():
233                 if filename[-1] == '/':
234                     continue
235                 title = os.path.basename(filename)[:-4]
236                 slug = slughifi(title)
237                 if not (slug and filename.endswith('.xml')):
238                     skipped_list.append(filename)
239                 elif slug in slugs:
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.')))
243                 else:
244                     try:
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
250
251             if not error_list:
252                 for filename, slug, title in ok_list:
253                     Book.create(creator=creator,
254                         slug=slug,
255                         title=title,
256                         text=zip.read(filename).decode('utf-8'),
257                     )
258
259             return direct_to_template(request, "wiki/document_upload.html", extra_context={
260                 "form": form,
261                 "ok_list": ok_list,
262                 "skipped_list": skipped_list,
263                 "error_list": error_list,
264             })
265     else:
266         form = forms.DocumentsUploadForm()
267
268     return direct_to_template(request, "wiki/document_upload.html", extra_context={
269         "form": form,
270     })
271
272
273 @never_cache
274 @decorator_from_middleware(GZipMiddleware)
275 def text(request, slug, chunk=None):
276     try:
277         doc = Chunk.get(slug, chunk)
278     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
279         raise Http404
280
281     if request.method == 'POST':
282         form = forms.DocumentTextSaveForm(request.POST, prefix="textsave")
283         if form.is_valid():
284             if request.user.is_authenticated():
285                 author = request.user
286             else:
287                 author = None
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,
294                        text=text,
295                        parent=parent,
296                        description=form.cleaned_data['comment'],
297                        tags=tags,
298                        )
299             revision = doc.revision()
300             return JSONResponse({
301                 'text': doc.materialize() if parent_revision != revision else None,
302                 'meta': {},
303                 'revision': revision,
304             })
305         else:
306             return JSONFormInvalid(form)
307     else:
308         revision = request.GET.get("revision", None)
309         
310         try:
311             revision = int(revision)
312         except (ValueError, TypeError):
313             revision = None
314
315         return JSONResponse({
316             'text': doc.at_revision(revision).materialize(),
317             'meta': {},
318             'revision': revision if revision else doc.revision(),
319         })
320
321
322 @never_cache
323 def book_xml(request, slug):
324     xml = get_object_or_404(Book, slug=slug).materialize()
325
326     response = http.HttpResponse(xml, content_type='application/xml', mimetype='application/wl+xml')
327     response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
328     return response
329
330
331 @never_cache
332 def book_txt(request, slug):
333     xml = get_object_or_404(Book, slug=slug).materialize()
334     output = StringIO()
335     # errors?
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
340     return response
341
342
343 @never_cache
344 def book_html(request, slug):
345     xml = get_object_or_404(Book, slug=slug).materialize()
346     output = StringIO()
347     # errors?
348     librarian.html.transform(StringIO(xml), output, parse_dublincore=False,
349                              flags=['full-page'])
350     html = output.getvalue()
351     response = http.HttpResponse(html, content_type='text/html', mimetype='text/html')
352     return response
353
354
355 @never_cache
356 @require_POST
357 def revert(request, slug, chunk=None):
358     form = forms.DocumentTextRevertForm(request.POST, prefix="textrevert")
359     if form.is_valid():
360         try:
361             doc = Chunk.get(slug, chunk)
362         except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
363             raise Http404
364
365         revision = form.cleaned_data['revision']
366
367         comment = form.cleaned_data['comment']
368         comment += "\n#revert to %s" % revision
369
370         if request.user.is_authenticated():
371             author = request.user
372         else:
373             author = None
374
375         before = doc.revision()
376         logger.info("Reverting %s to %s", slug, revision)
377         doc.at_revision(revision).revert(author=author, description=comment)
378
379         return JSONResponse({
380             'text': doc.materialize() if before != doc.revision() else None,
381             'meta': {},
382             'revision': doc.revision(),
383         })
384     else:
385         return JSONFormInvalid(form)
386
387
388 @never_cache
389 def gallery(request, directory):
390     try:
391         base_url = ''.join((
392                         smart_unicode(settings.MEDIA_URL),
393                         smart_unicode(settings.FILEBROWSER_DIRECTORY),
394                         smart_unicode(directory)))
395
396         base_dir = os.path.join(
397                     smart_unicode(settings.MEDIA_ROOT),
398                     smart_unicode(settings.FILEBROWSER_DIRECTORY),
399                     smart_unicode(directory))
400
401         def map_to_url(filename):
402             return "%s/%s" % (base_url, smart_unicode(filename))
403
404         def is_image(filename):
405             return os.path.splitext(f)[1].lower() in (u'.jpg', u'.jpeg', u'.png')
406
407         images = [map_to_url(f) for f in map(smart_unicode, os.listdir(base_dir)) if is_image(f)]
408         images.sort()
409         return JSONResponse(images)
410     except (IndexError, OSError):
411         logger.exception("Unable to fetch gallery")
412         raise http.Http404
413
414
415 @never_cache
416 def diff(request, slug, chunk=None):
417     revA = int(request.GET.get('from', 0))
418     revB = int(request.GET.get('to', 0))
419
420     if revA > revB:
421         revA, revB = revB, revA
422
423     if revB == 0:
424         revB = None
425
426     try:
427         doc = Chunk.get(slug, chunk)
428     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
429         raise Http404
430     docA = doc.at_revision(revA).materialize()
431     docB = doc.at_revision(revB).materialize()
432
433     return http.HttpResponse(nice_diff.html_diff_table(docA.splitlines(),
434                                          docB.splitlines(), context=3))
435
436
437 @never_cache
438 def revision(request, slug, chunk=None):
439     try:
440         doc = Chunk.get(slug, chunk)
441     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
442         raise Http404
443     return http.HttpResponse(str(doc.revision()))
444
445
446 @never_cache
447 def history(request, slug, chunk=None):
448     # TODO: pagination
449     try:
450         doc = Chunk.get(slug, chunk)
451     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
452         raise Http404
453
454     changes = []
455     for change in doc.history().order_by('-created_at'):
456         changes.append({
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()),
463             })
464     return JSONResponse(changes)
465
466
467 def book(request, slug):
468     book = get_object_or_404(Book, slug=slug)
469
470     # TODO: most of this should go somewhere else
471
472     # do we need some automation?
473     first_master = None
474     chunks = []
475     need_fixing = False
476     choose_master = False
477
478     length = len(book)
479     for i, chunk in enumerate(book):
480         chunk_dict = {
481             "chunk": chunk,
482             "fix": [],
483             "grade": ""
484             }
485         graded = xml_tools.GradedText(chunk.materialize())
486         if graded.is_wl():
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')
492
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')
497
498             if chunk_dict['fix']:
499                 chunk_dict['grade'] = 'wl-fix'
500             else:
501                 chunk_dict['grade'] = 'wl'
502
503         elif graded.is_broken_wl():
504             chunk_dict['grade'] = 'wl-broken'
505         elif graded.is_xml():
506             chunk_dict['grade'] = 'xml'
507         else:
508             chunk_dict['grade'] = 'plain'
509             chunk_dict['fix'].append('wl')
510             choose_master = True
511
512         if chunk_dict['fix']:
513             need_fixing = True
514         chunks.append(chunk_dict)
515
516     if first_master or not need_fixing:
517         choose_master = False
518
519     if request.method == "POST":
520         form = forms.ChooseMasterForm(request.POST)
521         if not choose_master or form.is_valid():
522             if choose_master:
523                 first_master = form.cleaned_data['master']
524
525             # do the actual fixing
526             for c in chunks:
527                 if not c['fix']:
528                     continue
529
530                 text = c['chunk'].materialize()
531                 for fix in c['fix']:
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)
538                     elif fix == 'wl':
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)
544
545             return http.HttpResponseRedirect(book.get_absolute_url())
546     elif choose_master:
547         form = forms.ChooseMasterForm()
548     else:
549         form = None
550
551     return direct_to_template(request, "wiki/book_detail.html", extra_context={
552         "book": book,
553         "chunks": chunks,
554         "need_fixing": need_fixing,
555         "choose_master": choose_master,
556         "first_master": first_master,
557         "form": form,
558     })
559
560
561 def chunk_add(request, slug, chunk):
562     try:
563         doc = Chunk.get(slug, chunk)
564     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
565         raise Http404
566
567     if request.method == "POST":
568         form = forms.ChunkAddForm(request.POST, instance=doc)
569         if form.is_valid():
570             if request.user.is_authenticated():
571                 creator = request.user
572             else:
573                 creator = None
574             doc.split(creator=creator,
575                 slug=form.cleaned_data['slug'],
576                 comment=form.cleaned_data['comment'],
577             )
578
579             return http.HttpResponseRedirect(doc.book.get_absolute_url())
580     else:
581         form = forms.ChunkAddForm(initial={
582                 "slug": str(doc.number + 1),
583                 "comment": "cz. %d" % (doc.number + 1, ),
584         })
585
586     return direct_to_template(request, "wiki/chunk_add.html", extra_context={
587         "chunk": doc,
588         "form": form,
589     })
590
591
592 def chunk_edit(request, slug, chunk):
593     try:
594         doc = Chunk.get(slug, chunk)
595     except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
596         raise Http404
597     if request.method == "POST":
598         form = forms.ChunkForm(request.POST, instance=doc)
599         if form.is_valid():
600             form.save()
601             return http.HttpResponseRedirect(doc.book.get_absolute_url())
602     else:
603         form = forms.ChunkForm(instance=doc)
604     return direct_to_template(request, "wiki/chunk_edit.html", extra_context={
605         "chunk": doc,
606         "form": form,
607     })
608
609
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)
614         if form.is_valid():
615             append_to = form.cleaned_data['append_to']
616             append_to.append(book)
617             return http.HttpResponseRedirect(append_to.get_absolute_url())
618     else:
619         form = forms.BookAppendForm()
620     return direct_to_template(request, "wiki/book_append_to.html", extra_context={
621         "book": book,
622         "form": form,
623     })
624
625
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)
630         if form.is_valid():
631             form.save()
632             return http.HttpResponseRedirect(book.get_absolute_url())
633     else:
634         form = forms.BookForm(instance=book)
635     return direct_to_template(request, "wiki/book_edit.html", extra_context={
636         "book": book,
637         "form": form,
638     })
639
640
641 @require_POST
642 @ajax_require_permission('wiki.can_change_tags')
643 def add_tag(request, slug, chunk=None):
644     form = forms.DocumentTagForm(request.POST, prefix="addtag")
645     if form.is_valid():
646         try:
647             doc = Chunk.get(slug, chunk)
648         except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
649             raise Http404
650
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")})
655     else:
656         return JSONFormInvalid(form)
657
658
659 @require_POST
660 @ajax_require_permission('wiki.can_pubmark')
661 def pubmark(request, slug, chunk=None):
662     form = forms.DocumentPubmarkForm(request.POST, prefix="pubmark")
663     if form.is_valid():
664         try:
665             doc = Chunk.get(slug, chunk)
666         except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
667             raise Http404
668
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
675             change.save()
676             return JSONResponse({"message": _("Revision marked")})
677         else:
678             return JSONResponse({"message": _("Nothing changed")})
679     else:
680         return JSONFormInvalid(form)
681
682
683 @require_POST
684 @login_required
685 def publish(request, slug):
686     book = get_object_or_404(Book, slug=slug)
687     try:
688         ret = api_call(request.user, "books", {"book_xml": book.materialize()})
689     except BaseException, e:
690         return http.HttpResponse(e)
691     else:
692         book.last_published = datetime.now()
693         book.save()
694         return http.HttpResponseRedirect(book.get_absolute_url())
695
696
697 def themes(request):
698     prefix = request.GET.get('q', '')
699     return http.HttpResponse('\n'.join([str(t) for t in Theme.objects.filter(name__istartswith=prefix)]))