Hmm..
[redakcja.git] / apps / explorer / views.py
1 # -*- coding: utf-8 -*-
2 import urllib2
3 import hg, re
4 from datetime import date
5
6 import librarian
7
8 from librarian import html, parser, dcparser
9 from librarian import ParseError, ValidationError
10
11 from django.conf import settings
12 from django.contrib.auth.decorators import login_required, permission_required
13
14 from django.core.urlresolvers import reverse
15 from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotFound
16 from django.utils import simplejson as json
17 from django.views.generic.simple import direct_to_template
18 from django.contrib.auth.decorators import login_required
19
20 from explorer import forms, models
21 from toolbar import models as toolbar_models
22
23 from django.forms.util import ErrorList
24
25 import wlrepo
26
27 #
28 # Some useful decorators
29
30 def file_branch(fileid, user=None):
31     parts = fileid.split('$')
32     return ('personal_'+ user.username + '_' if user is not None else '') \
33         + 'file_' + parts[0]
34
35 def file_path(fileid):
36     return 'pub_'+fileid+'.xml'
37
38 def with_repo(view):
39     """Open a repository for this view"""
40     def view_with_repo(request, *args, **kwargs):          
41         kwargs['repo'] = wlrepo.open_library(settings.REPOSITORY_PATH, 'hg')
42         return view(request, *args, **kwargs)
43     return view_with_repo
44
45 #
46 def ajax_login_required(view):
47     """Similar ro @login_required, but instead of redirect, 
48     just return some JSON stuff with error."""
49     def view_with_auth(request, *args, **kwargs):
50         if request.user.is_authenticated():
51             return view(request, *args, **kwargs)
52         # not authenticated
53         return HttpResponse( json.dumps({'result': 'access_denied', 'errors': ['Brak dostępu.']}) );
54     return view_with_auth
55
56 #
57 # View all files
58 #
59 @with_repo
60 def file_list(request, repo):   
61     import api.forms
62     from api.resources import library_resource
63
64     bookform = api.forms.DocumentUploadForm()
65
66     # short-circut the api document list
67     doctree = library_resource.handler.read(request)
68     print "DOCTREE:", doctree['documents']
69         
70     return direct_to_template(request, 'explorer/file_list.html', extra_context={
71         'filetree': doctree['documents'], 'bookform': bookform,
72     })
73
74 @permission_required('explorer.can_add_files')
75 @with_repo
76 def file_upload(request, repo):
77     other_errors = []
78     if request.method == 'POST':
79         form = forms.BookUploadForm(request.POST, request.FILES)
80         if form.is_valid():
81             try:
82                 # prepare the data
83                 f = request.FILES['file']
84                 decoded = f.read().decode('utf-8')
85                 fileid = form.cleaned_data['bookname'].lower()
86                 rpath = file_path(fileid)
87
88                 if form.cleaned_data['autoxml']:
89                     decoded = librarian.wrap_text(decoded, unicode(date.today()) )
90                 
91                 def upload_action():
92                     repo._add_file(rpath, decoded.encode('utf-8') )
93                     repo._commit(message="File %s uploaded by user %s" % \
94                         (rpath, request.user.username), user=request.user.username)
95
96                 repo.in_branch(upload_action, 'default')
97
98                 # if everything is ok, redirect to the editor
99                 return HttpResponseRedirect( reverse('editor_view',
100                         kwargs={'path': fileid}) )
101
102             except hg.RepositoryException, e:
103                 other_errors.append(u'Błąd repozytorium: ' + unicode(e) )
104             #except UnicodeDecodeError, e:
105             #    other_errors.append(u'Niepoprawne kodowanie pliku: ' + e.reason \
106             #     + u'. Żądane kodowanie: ' + e.encoding)
107         # invalid form
108
109     # get
110     form = forms.BookUploadForm()
111     return direct_to_template(request, 'explorer/file_upload.html',\
112         extra_context = {'form' : form, 'other_errors': other_errors})
113    
114 #
115 # Edit the file
116 #
117
118 @ajax_login_required
119 @with_repo
120 def file_xml(request, repo, path):
121     rpath = file_path(path)
122     if request.method == 'POST':
123         errors = None
124         warnings = None
125         form = forms.BookForm(request.POST)
126         if form.is_valid():
127             print 'Saving whole text.', request.user.username
128             try:
129                 # encode it back to UTF-8, so we can put it into repo
130                 encoded_data = form.cleaned_data['content'].encode('utf-8')
131
132                 def save_action():                    
133                     repo._add_file(rpath, encoded_data)
134                     repo._commit(message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'),\
135                          user=request.user.username)
136
137                 try:
138                     # wczytaj dokument z ciągu znaków -> weryfikacja
139                     document = parser.WLDocument.from_string(form.cleaned_data['content'])
140                 except (ParseError, ValidationError), e:
141                     warnings = [u'Niepoprawny dokument XML: ' + unicode(e.message)]
142
143                 #  save to user's branch
144                 repo.in_branch(save_action, file_branch(path, request.user) );
145             except UnicodeDecodeError, e:
146                 errors = [u'Błąd kodowania danych przed zapisem: ' + unicode(e.message)]
147             except hg.RepositoryException, e:
148                 errors = [u'Błąd repozytorium: ' + unicode(e.message)]            
149
150         if not errors:
151             errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
152
153         return HttpResponse( json.dumps({'result': errors and 'error' or 'ok',
154             'errors': errors, 'warnings': warnings}) );
155
156     form = forms.BookForm()
157     data = repo.get_file(rpath, file_branch(path, request.user))
158     form.fields['content'].initial = data
159     return HttpResponse( json.dumps({'result': 'ok', 'content': data}) )
160
161 @ajax_login_required
162 @with_repo
163 def file_update_local(request, path, repo):
164     result = None
165     errors = None
166     
167     wlock = repo.write_lock()
168     try:
169         tipA = repo.get_branch_tip('default')
170         tipB = repo.get_branch_tip( file_branch(path, request.user) )
171
172         nodeA = repo.getnode(tipA)
173         nodeB = repo.getnode(tipB)
174         
175         # do some wild checks - see file_commit() for more info
176         if (repo.common_ancestor(tipA, tipB) == nodeA) \
177         or (nodeB in nodeA.parents()):
178             result = 'nothing-to-do'
179         else:
180             # Case 2+
181             repo.merge_revisions(tipB, tipA, \
182                 request.user.username, 'Personal branch update.')
183             result = 'done'
184     except hg.UncleanMerge, e:
185         errors = [e.message]
186         result = 'fatal-error'
187     except hg.RepositoryException, e:
188         errors = [e.message]
189         result = 'fatal-error'
190     finally:
191         wlock.release()
192
193     if result is None:
194         raise Exception("Ouch, this shouldn't happen!")
195     
196     return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
197
198 @ajax_login_required
199 @with_repo
200 def file_commit(request, path, repo):
201     result = None
202     errors = None
203     local_modified = False
204     if request.method == 'POST':
205         form = forms.MergeForm(request.POST)
206
207         if form.is_valid():           
208             wlock = repo.write_lock()
209             try:
210                 tipA = repo.get_branch_tip('default')
211                 tipB = repo.get_branch_tip( file_branch(path, request.user) )
212
213                 nodeA = repo.getnode(tipA)
214                 nodeB = repo.getnode(tipB)
215
216                 print repr(nodeA), repr(nodeB), repo.common_ancestor(tipA, tipB), repo.common_ancestor(tipB, tipA)
217
218                 if repo.common_ancestor(tipB, tipA) == nodeA:
219                     # Case 1:
220                     #         * tipB
221                     #         |
222                     #         * <- can also be here!
223                     #        /|
224                     #       / |
225                     # tipA *  *
226                     #      |  |
227                     # The local branch has been recently updated,
228                     # so we don't need to update yet again, but we need to
229                     # merge down to default branch, even if there was
230                     # no commit's since last update
231                     repo.merge_revisions(tipA, tipB, \
232                         request.user.username, form.cleaned_data['message'])
233                     result = 'done'
234                 elif any( p.branch()==nodeB.branch() for p in nodeA.parents()):
235                     # Case 2:
236                     #
237                     # tipA *  * tipB
238                     #      |\ |
239                     #      | \|
240                     #      |  * 
241                     #      |  |
242                     # Default has no changes, to update from this branch
243                     # since the last merge of local to default.
244                     if nodeB not in nodeA.parents():
245                         repo.merge_revisions(tipA, tipB, \
246                             request.user.username, form.cleaned_data['message'])
247                         result = 'done'
248                     else:
249                         result = 'nothing-to-do'
250                 elif repo.common_ancestor(tipA, tipB) == nodeB:
251                     # Case 3:
252                     # tipA * 
253                     #      |
254                     #      * <- this case overlaps with previos one
255                     #      |\
256                     #      | \
257                     #      |  * tipB
258                     #      |  |
259                     #
260                     # There was a recent merge to the defaul branch and
261                     # no changes to local branch recently.
262                     # 
263                     # Use the fact, that user is prepared to see changes, to
264                     # update his branch if there are any
265                     if nodeB not in nodeA.parents():
266                         repo.merge_revisions(tipB, tipA, \
267                             request.user.username, 'Personal branch update during merge.')
268                         local_modified = True
269                         result = 'done'
270                     else:
271                         result = 'nothing-to-do'
272                 else:
273                     # both branches have changes made to them, so
274                     # first do an update
275                     repo.merge_revisions(tipB, tipA, \
276                         request.user.username, 'Personal branch update during merge.')
277
278                     local_modified = True
279
280                     # fetch the new tip
281                     tipB = repo.get_branch_tip( file_branch(path, request.user) )
282
283                     # and merge back to the default
284                     repo.merge_revisions(tipA, tipB, \
285                         request.user.username, form.cleaned_data['message'])
286                     result = 'done'
287             except hg.UncleanMerge, e:
288                 errors = [e.message]
289                 result = 'fatal-error'
290             except hg.RepositoryException, e:
291                 errors = [e.message]
292                 result = 'fatal-error'
293             finally:
294                 wlock.release()
295                 
296         if result is None:
297             errors = [ form.errors['message'].as_text() ]
298             if len(errors) > 0:
299                 result = 'fatal-error'
300
301         return HttpResponse( json.dumps({'result': result, 'errors': errors, 'localmodified': local_modified}) );
302
303     return HttpResponse( json.dumps({'result': 'fatal-error', 'errors': ['No data posted']}) )
304     
305
306 @ajax_login_required
307 @with_repo
308 def file_dc(request, path, repo):
309     errors = None
310     rpath = file_path(path)
311
312     if request.method == 'POST':
313         form = forms.DublinCoreForm(request.POST)
314         
315         if form.is_valid():
316             
317             def save_action():
318                 file_contents = repo._get_file(rpath)
319
320                 # wczytaj dokument z repozytorium
321                 document = parser.WLDocument.from_string(file_contents)                    
322                 document.book_info.update(form.cleaned_data)             
323
324                 # zapisz
325                 repo._write_file(rpath, document.serialize().encode('utf-8'))
326                 repo._commit( \
327                     message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'), \
328                     user=request.user.username )
329                 
330             try:
331                 repo.in_branch(save_action, file_branch(path, request.user) )
332             except UnicodeEncodeError, e:
333                 errors = ['Bład wewnętrzny: nie można zakodować pliku do utf-8']
334             except (ParseError, ValidationError), e:
335                 errors = [e.message]
336
337         if errors is None:
338             errors = ["Pole '%s': %s\n" % (field[0], field[1].as_text()) for field in form.errors.iteritems()]
339
340         return HttpResponse( json.dumps({'result': errors and 'error' or 'ok', 'errors': errors}) );
341     
342     # this is unused currently, but may come in handy 
343     content = []
344     
345     try:
346         fulltext = repo.get_file(rpath, file_branch(path, request.user))
347         bookinfo = dcparser.BookInfo.from_string(fulltext)
348         content = bookinfo.to_dict()
349     except (ParseError, ValidationError), e:
350         errors = [e.message]
351
352     return HttpResponse( json.dumps({'result': errors and 'error' or 'ok', 
353         'errors': errors, 'content': content }) ) 
354
355 # Display the main editor view
356
357 @login_required
358 # @with_repo
359 def display_editor(request, path):    
360     # this is the only entry point where we create an autobranch for the user
361     # if it doesn't exists. All other views SHOULD fail.
362     #def ensure_branch_exists():
363     #    parent = repo.get_branch_tip('default')
364     #    repo._create_branch(file_branch(path, request.user), parent)
365         
366 #    try:
367     #    repo.with_wlock(ensure_branch_exists)
368         
369     return direct_to_template(request, 'explorer/editor.html', extra_context={
370         'fileid': path,
371         'panel_list': ['lewy', 'prawy'],
372         'availble_panels': models.EditorPanel.objects.all(),
373         # 'scriptlets': toolbar_models.Scriptlet.objects.all()
374     })
375 #    except KeyError:
376 #        return direct_to_template(request, 'explorer/nofile.html', \
377 #            extra_context = { 'fileid': path })
378
379 # ===============
380 # = Panel views =
381 # ===============
382 class panel_view(object):
383
384     def __new__(cls, request, name, path, **kwargs):
385     #try:        
386         panel = models.EditorPanel.objects.get(id=name)
387         method = getattr(cls, name + '_panel', None)
388         if not panel or method is None:
389             raise HttpResponseNotFound
390
391         extra_context = method(request, path, panel, **kwargs)
392
393         if not isinstance(extra_context, dict):
394             return extra_context
395
396         extra_context.update({
397             'toolbar_groups': panel.toolbar_groups.all(),
398             'toolbar_extra_group': panel.toolbar_extra,
399             'fileid': path
400         })
401
402         return direct_to_template(request, 'explorer/panels/'+name+'.html',\
403             extra_context=extra_context)
404
405     @staticmethod
406     @ajax_login_required
407     @with_repo
408     def xmleditor_panel(request, path, panel, repo):
409         rpath = file_path(path)
410         return {'text': repo.get_file(rpath, file_branch(path, request.user))}
411
412     @staticmethod
413     @ajax_login_required
414     def gallery_panel(request, path, panel):
415         return {'form': forms.ImageFoldersForm() }
416
417     @staticmethod
418     @ajax_login_required
419     @with_repo
420     def htmleditor_panel(request, path, panel, repo):
421         rpath = file_path(path)
422         user_branch = file_branch(path, request.user)
423         try:
424             result = html.transform(repo.get_file(rpath, user_branch), is_file=False)
425             print "HTML: %r" % result
426             return {'html': result}
427         except (ParseError, ValidationError), e:
428             return direct_to_template(request, 'explorer/panels/parse_error.html', extra_context={
429             'fileid': path, 'exception_type': type(e).__name__, 'exception': e,
430             'panel_name': panel.display_name})
431
432     @staticmethod
433     @ajax_login_required
434     @with_repo
435     def dceditor_panel(request, path, panel, repo):
436         user_branch = file_branch(path, request.user)
437         rpath = file_path(path)
438         try:
439             doc_text = repo.get_file(rpath, user_branch)
440             document = parser.WLDocument.from_string(doc_text)
441             form = forms.DublinCoreForm(info=document.book_info)
442             return {'form': form}
443         except (ParseError, ValidationError), e:
444             return direct_to_template(request, 'explorer/panels/parse_error.html', extra_context={
445             'fileid': path, 'exception_type': type(e).__name__, 'exception': e,
446             'panel_name': panel.display_name})
447
448 ##
449 ## Editor "commands" and "dialogs"
450 ##
451 @login_required
452 @with_repo
453 def print_html(request, path, repo):
454     user_branch = file_branch(path, request.user)
455     rpath = file_path(path)
456     return HttpResponse( 
457         html.transform(repo.get_file(rpath, user_branch), is_file=False),
458         mimetype="text/html")
459
460 @login_required
461 @with_repo
462 def print_xml(request, path, repo):
463     user_branch = file_branch(path, request.user)
464     rpath = file_path(path)
465     return HttpResponse( repo.get_file(rpath, user_branch), mimetype="text/plain; charset=utf-8")
466
467 @permission_required('explorer.can_add_files')
468 def split_text(request, path):
469     rpath = file_path(path)
470     valid = False    
471     if request.method == "POST":
472         sform = forms.SplitForm(request.POST, prefix='splitform')
473         dcform = forms.DublinCoreForm(request.POST, prefix='dcform')
474
475         print "validating sform"
476         if sform.is_valid():
477             valid = True
478 #            if sform.cleaned_data['autoxml']:
479 #                print "validating dcform"
480 #                valid = dcform.is_valid()
481
482         print "valid is ", valid
483
484         if valid:
485             uri = path + '$' + sform.cleaned_data['partname']
486             child_rpath = file_path(uri)
487             repo = hg.Repository(settings.REPOSITORY_PATH)            
488
489             # save the text into parent's branch
490             def split_action():
491                 if repo._file_exists(child_rpath):
492                     el = sform._errors.get('partname', ErrorList())
493                     el.append("Part with this name already exists")
494                     sform._errors['partname'] = el
495                     return False
496                                         
497                 fulltext = sform.cleaned_data['fulltext']               
498                 fulltext = fulltext.replace(u'<include-tag-placeholder />',
499                     librarian.xinclude_forURI(u'wlrepo://'+uri) )
500
501                 repo._write_file(rpath, fulltext.encode('utf-8'))
502
503                 newtext = sform.cleaned_data['splittext']
504                 if sform.cleaned_data['autoxml']:
505                     # this is a horrible hack - really
506                     bi = dcparser.BookInfo.from_element(librarian.DEFAULT_BOOKINFO.to_etree())
507                     bi.update(dcform.data)
508
509                     newtext = librarian.wrap_text(newtext, \
510                         unicode(date.today()), bookinfo=bi )
511
512                 repo._add_file(child_rpath, newtext.encode('utf-8'))                
513                 repo._commit(message="Split from '%s' to '%s'" % (path, uri), \
514                     user=request.user.username )
515                 return True
516
517             if repo.in_branch(split_action, file_branch(path, request.user)):
518                 # redirect to success
519                 import urllib
520                 uri = urllib.quote( unicode(uri).encode('utf-8'))
521                 return HttpResponseRedirect( reverse('split-success',\
522                     kwargs={'path': path})+'?child='+uri )
523     else:
524         try: # to read the current DC
525             repo = hg.Repository(settings.REPOSITORY_PATH)
526             fulltext = repo.get_file(rpath, file_branch(path, request.user))
527             bookinfo = dcparser.BookInfo.from_string(fulltext)
528         except (ParseError, ValidationError):
529             bookinfo = librarian.DEFAULT_BOOKINFO
530
531         sform = forms.SplitForm(prefix='splitform')
532         dcform = forms.DublinCoreForm(prefix='dcform', info=bookinfo)
533    
534     return direct_to_template(request, 'explorer/split.html', extra_context={
535         'splitform': sform, 'dcform': dcform, 'fileid': path} )
536
537 def split_success(request, path):
538     return direct_to_template(request, 'explorer/split_success.html',\
539         extra_context={'fileid': path, 'cfileid': request.GET['child']} )
540
541
542 # =================
543 # = Utility views =
544 # =================
545 @ajax_login_required
546 def folder_images(request, folder):
547     return direct_to_template(request, 'explorer/folder_images.html', extra_context={
548         'images': models.get_images_from_folder(folder),
549     })
550
551 def _add_references(message, issues):
552     return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
553
554 def _get_issues_for_file(fileid):
555     uf = None
556     try:
557         uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % fileid)
558         return json.loads(uf.read())
559     except urllib2.HTTPError:
560         return []
561     finally:
562         if uf: uf.close()
563
564 # =================
565 # = Pull requests =
566 # =================
567 #def pull_requests(request):
568 #    return direct_to_template(request, 'manager/pull_request.html', extra_context = {
569 #        'objects': models.PullRequest.objects.all()} )