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