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