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