Merge branch 'master' into view-refactor
[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):    
353     # this is the only entry point where we create an autobranch for the user
354     # if it doesn't exists. All other views SHOULD fail.
355     #def ensure_branch_exists():
356     #    parent = repo.get_branch_tip('default')
357     #    repo._create_branch(file_branch(path, request.user), parent)
358         
359 #    try:
360     #    repo.with_wlock(ensure_branch_exists)
361         
362     return direct_to_template(request, 'explorer/editor.html', extra_context={
363         'fileid': path,
364         'panel_list': ['lewy', 'prawy'],
365         'availble_panels': models.EditorPanel.objects.all(),
366         # 'scriptlets': toolbar_models.Scriptlet.objects.all()
367     })
368 #    except KeyError:
369 #        return direct_to_template(request, 'explorer/nofile.html', \
370 #            extra_context = { 'fileid': path })
371
372 # ===============
373 # = Panel views =
374 # ===============
375 class panel_view(object):
376
377     def __new__(cls, request, name, path, **kwargs):
378     #try:        
379         panel = models.EditorPanel.objects.get(id=name)
380         method = getattr(cls, name + '_panel', None)
381         if not panel or method is None:
382             raise HttpResponseNotFound
383
384         extra_context = method(request, path, panel, **kwargs)
385
386         if not isinstance(extra_context, dict):
387             return extra_context
388
389         extra_context.update({
390             'toolbar_groups': panel.toolbar_groups.all(),
391             'toolbar_extra_group': panel.toolbar_extra,
392             'fileid': path
393         })
394
395         return direct_to_template(request, 'explorer/panels/'+name+'.html',\
396             extra_context=extra_context)
397
398     @staticmethod
399     @ajax_login_required
400     @with_repo
401     def xmleditor_panel(request, path, panel, repo):
402         rpath = file_path(path)
403         return {'text': repo.get_file(rpath, file_branch(path, request.user))}
404
405     @staticmethod
406     @ajax_login_required
407     def gallery_panel(request, path, panel):
408         return {'form': forms.ImageFoldersForm() }
409
410     @staticmethod
411     @ajax_login_required
412     @with_repo
413     def htmleditor_panel(request, path, panel, repo):
414         rpath = file_path(path)
415         user_branch = file_branch(path, request.user)
416         try:
417             result = html.transform(repo.get_file(rpath, user_branch), is_file=False)
418             print "HTML: %r" % result
419             return {'html': result}
420         except (ParseError, ValidationError), e:
421             return direct_to_template(request, 'explorer/panels/parse_error.html', extra_context={
422             'fileid': path, 'exception_type': type(e).__name__, 'exception': e,
423             'panel_name': panel.display_name})
424
425     @staticmethod
426     @ajax_login_required
427     @with_repo
428     def dceditor_panel(request, path, panel, repo):
429         user_branch = file_branch(path, request.user)
430         rpath = file_path(path)
431         try:
432             doc_text = repo.get_file(rpath, user_branch)
433             document = parser.WLDocument.from_string(doc_text)
434             form = forms.DublinCoreForm(info=document.book_info)
435             return {'form': form}
436         except (ParseError, ValidationError), e:
437             return direct_to_template(request, 'explorer/panels/parse_error.html', extra_context={
438             'fileid': path, 'exception_type': type(e).__name__, 'exception': e,
439             'panel_name': panel.display_name})
440
441 ##
442 ## Editor "commands" and "dialogs"
443 ##
444 @login_required
445 @with_repo
446 def print_html(request, path, repo):
447     user_branch = file_branch(path, request.user)
448     rpath = file_path(path)
449     return HttpResponse( 
450         html.transform(repo.get_file(rpath, user_branch), is_file=False),
451         mimetype="text/html")
452
453 @login_required
454 @with_repo
455 def print_xml(request, path, repo):
456     user_branch = file_branch(path, request.user)
457     rpath = file_path(path)
458     return HttpResponse( repo.get_file(rpath, user_branch), mimetype="text/plain; charset=utf-8")
459
460 @permission_required('explorer.can_add_files')
461 def split_text(request, path):
462     rpath = file_path(path)
463     valid = False    
464     if request.method == "POST":
465         sform = forms.SplitForm(request.POST, prefix='splitform')
466         dcform = forms.DublinCoreForm(request.POST, prefix='dcform')
467
468         print "validating sform"
469         if sform.is_valid():
470             valid = True
471 #            if sform.cleaned_data['autoxml']:
472 #                print "validating dcform"
473 #                valid = dcform.is_valid()
474
475         print "valid is ", valid
476
477         if valid:
478             uri = path + '$' + sform.cleaned_data['partname']
479             child_rpath = file_path(uri)
480             repo = hg.Repository(settings.REPOSITORY_PATH)            
481
482             # save the text into parent's branch
483             def split_action():
484                 if repo._file_exists(child_rpath):
485                     el = sform._errors.get('partname', ErrorList())
486                     el.append("Part with this name already exists")
487                     sform._errors['partname'] = el
488                     return False
489                                         
490                 fulltext = sform.cleaned_data['fulltext']               
491                 fulltext = fulltext.replace(u'<include-tag-placeholder />',
492                     librarian.xinclude_forURI(u'wlrepo://'+uri) )
493
494                 repo._write_file(rpath, fulltext.encode('utf-8'))
495
496                 newtext = sform.cleaned_data['splittext']
497                 if sform.cleaned_data['autoxml']:
498                     # this is a horrible hack - really
499                     bi = dcparser.BookInfo.from_element(librarian.DEFAULT_BOOKINFO.to_etree())
500                     bi.update(dcform.data)
501
502                     newtext = librarian.wrap_text(newtext, \
503                         unicode(date.today()), bookinfo=bi )
504
505                 repo._add_file(child_rpath, newtext.encode('utf-8'))                
506                 repo._commit(message="Split from '%s' to '%s'" % (path, uri), \
507                     user=request.user.username )
508                 return True
509
510             if repo.in_branch(split_action, file_branch(path, request.user)):
511                 # redirect to success
512                 import urllib
513                 uri = urllib.quote( unicode(uri).encode('utf-8'))
514                 return HttpResponseRedirect( reverse('split-success',\
515                     kwargs={'path': path})+'?child='+uri )
516     else:
517         try: # to read the current DC
518             repo = hg.Repository(settings.REPOSITORY_PATH)
519             fulltext = repo.get_file(rpath, file_branch(path, request.user))
520             bookinfo = dcparser.BookInfo.from_string(fulltext)
521         except (ParseError, ValidationError):
522             bookinfo = librarian.DEFAULT_BOOKINFO
523
524         sform = forms.SplitForm(prefix='splitform')
525         dcform = forms.DublinCoreForm(prefix='dcform', info=bookinfo)
526    
527     return direct_to_template(request, 'explorer/split.html', extra_context={
528         'splitform': sform, 'dcform': dcform, 'fileid': path} )
529
530 def split_success(request, path):
531     return direct_to_template(request, 'explorer/split_success.html',\
532         extra_context={'fileid': path, 'cfileid': request.GET['child']} )
533
534
535 # =================
536 # = Utility views =
537 # =================
538 @ajax_login_required
539 def folder_images(request, folder):
540     return direct_to_template(request, 'explorer/folder_images.html', extra_context={
541         'images': models.get_images_from_folder(folder),
542     })
543
544 def _add_references(message, issues):
545     return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
546
547 def _get_issues_for_file(fileid):
548     uf = None
549     try:
550         uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % fileid)
551         return json.loads(uf.read())
552     except urllib2.HTTPError:
553         return []
554     finally:
555         if uf: uf.close()
556
557 # =================
558 # = Pull requests =
559 # =================
560 #def pull_requests(request):
561 #    return direct_to_template(request, 'manager/pull_request.html', extra_context = {
562 #        'objects': models.PullRequest.objects.all()} )