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