1 # -*- coding: utf-8 -*-
4 from datetime import date
6 from librarian import html, parser, dcparser, wrap_text
7 from librarian import ParseError, ValidationError
9 from django.conf import settings
10 from django.contrib.auth.decorators import login_required, permission_required
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
18 from explorer import forms, models
19 from toolbar import models as toolbar_models
22 # Some useful decorators
24 def file_branch(path, user=None):
25 return ('personal_'+user.username + '_' if user is not None else '') \
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)
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)
43 return HttpResponse( json.dumps({'result': 'access_denied', 'errors': ['Brak dostępu.']}) );
50 def file_list(request, repo):
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()
56 return direct_to_template(request, 'explorer/file_list.html', extra_context={
57 'files': files, 'bookform': bookform,
60 @permission_required('explorer.can_add_files')
62 def file_upload(request, repo):
64 if request.method == 'POST':
65 form = forms.BookUploadForm(request.POST, request.FILES)
69 f = request.FILES['file']
70 decoded = f.read().decode('utf-8')
71 path = form.cleaned_data['bookname']
73 if form.cleaned_data['autoxml']:
74 decoded = wrap_text(decoded, unicode(date.today()) )
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)
81 repo.in_branch(upload_action, 'default')
83 # if everything is ok, redirect to the editor
84 return HttpResponseRedirect( reverse('editor_view',
85 kwargs={'path': path}) )
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)
95 form = forms.BookUploadForm()
96 return direct_to_template(request, 'explorer/file_upload.html',\
97 extra_context = {'form' : form, 'other_errors': other_errors})
105 def file_xml(request, repo, path):
106 if request.method == 'POST':
109 form = forms.BookForm(request.POST)
111 print 'Saving whole text.', request.user.username
113 # encode it back to UTF-8, so we can put it into repo
114 encoded_data = form.cleaned_data['content'].encode('utf-8')
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)
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)]
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)]
135 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
137 return HttpResponse( json.dumps({'result': errors and 'error' or 'ok',
138 'errors': errors, 'warnings': warnings}) );
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}) )
147 def file_update_local(request, path, repo):
151 wlock = repo.write_lock()
153 tipA = repo.get_branch_tip('default')
154 tipB = repo.get_branch_tip( file_branch(path, request.user) )
156 nodeA = repo.getnode(tipA)
157 nodeB = repo.getnode(tipB)
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'
165 repo.merge_revisions(tipB, tipA, \
166 request.user.username, 'Personal branch update.')
168 except hg.UncleanMerge, e:
170 result = 'fatal-error'
171 except hg.RepositoryException, e:
173 result = 'fatal-error'
178 raise Exception("Ouch, this shouldn't happen!")
180 return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
184 def file_commit(request, path, repo):
187 local_modified = False
188 if request.method == 'POST':
189 form = forms.MergeForm(request.POST)
192 wlock = repo.write_lock()
194 tipA = repo.get_branch_tip('default')
195 tipB = repo.get_branch_tip( file_branch(path, request.user) )
197 nodeA = repo.getnode(tipA)
198 nodeB = repo.getnode(tipB)
200 print repr(nodeA), repr(nodeB), repo.common_ancestor(tipA, tipB), repo.common_ancestor(tipB, tipA)
202 if repo.common_ancestor(tipB, tipA) == nodeA:
206 # * <- can also be here!
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'])
218 elif any( p.branch()==nodeB.branch() for p in nodeA.parents()):
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'])
233 result = 'nothing-to-do'
234 elif repo.common_ancestor(tipA, tipB) == nodeB:
238 # * <- this case overlaps with previos one
244 # There was a recent merge to the defaul branch and
245 # no changes to local branch recently.
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
255 result = 'nothing-to-do'
257 # both branches have changes made to them, so
259 repo.merge_revisions(tipB, tipA, \
260 request.user.username, 'Personal branch update during merge.')
262 local_modified = True
265 tipB = repo.get_branch_tip( file_branch(path, request.user) )
267 # and merge back to the default
268 repo.merge_revisions(tipA, tipB, \
269 request.user.username, form.cleaned_data['message'])
271 except hg.UncleanMerge, e:
273 result = 'fatal-error'
274 except hg.RepositoryException, e:
276 result = 'fatal-error'
281 errors = [ form.errors['message'].as_text() ]
283 result = 'fatal-error'
285 return HttpResponse( json.dumps({'result': result, 'errors': errors, 'localmodified': local_modified}) );
287 return HttpResponse( json.dumps({'result': 'fatal-error', 'errors': ['No data posted']}) )
292 def file_dc(request, path, repo):
295 if request.method == 'POST':
296 form = forms.DublinCoreForm(request.POST)
301 file_contents = repo._get_file(path)
303 # wczytaj dokument z repozytorium
304 document = parser.WLDocument.from_string(file_contents)
305 document.book_info.update(form.cleaned_data)
308 repo._write_file(path, document.serialize().encode('utf-8'))
310 message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'), \
311 user=request.user.username )
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:
321 errors = ["Pole '%s': %s\n" % (field[0], field[1].as_text()) for field in form.errors.iteritems()]
323 return HttpResponse( json.dumps({'result': errors and 'error' or 'ok', 'errors': errors}) );
325 # this is unused currently, but may come in handy
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:
335 return HttpResponse( json.dumps({'result': errors and 'error' or 'ok',
336 'errors': errors, 'content': content }) )
338 # Display the main editor view
342 def display_editor(request, path, repo):
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)
351 repo.with_wlock(ensure_branch_exists)
353 return direct_to_template(request, 'explorer/editor.html', extra_context={
355 'panel_list': ['lewy', 'prawy'],
356 'availble_panels': models.EditorPanel.objects.all(),
357 'scriptlets': toolbar_models.Scriptlet.objects.all()
360 return direct_to_template(request, 'explorer/nofile.html', \
361 extra_context = { 'fileid': path })
366 class panel_view(object):
368 def __new__(cls, request, name, path, **kwargs):
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
375 extra_context = method(request, path, panel, **kwargs)
377 if not isinstance(extra_context, dict):
380 extra_context.update({
381 'toolbar_groups': panel.toolbar_groups.all(),
382 'toolbar_extra_group': panel.toolbar_extra,
386 return direct_to_template(request, 'explorer/panels/'+name+'.html',\
387 extra_context=extra_context)
392 def xmleditor_panel(request, path, panel, repo):
393 return {'text': repo.get_file(path, file_branch(path, request.user))}
397 def gallery_panel(request, path, panel):
398 return {'form': forms.ImageFoldersForm() }
403 def htmleditor_panel(request, path, panel, repo):
404 user_branch = file_branch(path, request.user)
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})
415 def dceditor_panel(request, path, panel, repo):
416 user_branch = file_branch(path, request.user)
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})
428 ## Editor "commands" and "dialogs"
432 def print_html(request, path, repo):
433 user_branch = file_branch(path, request.user)
435 html.transform(repo.get_file(path, user_branch), is_file=False),
436 mimetype="text/html")
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")
444 @login_required # WARNING: we don't wont a login form inside a window
446 def split_text(request, path, repo):
447 user_branch = file_branch(path, request.user)
450 if request.method == "POST":
451 sform = forms.SplitForm(request.POST, prefix='splitform')
452 dcform = forms.SplitForm(request.POST, prefix='dcform')
454 print "validating sform"
457 if sform.cleaned_data['autoxml']:
458 print "validating dcform"
459 valid = dcform.is_valid()
461 print "valid is ", valid
463 uri = path + '$' + sform.cleaned_data['partname']
465 return HttpResponseRedirect( reverse('split-success',\
466 kwargs={'path': path})+'?child='+uri)
468 sform = forms.SplitForm(prefix='splitform')
469 dcform = forms.DublinCoreForm(prefix='dcform')
471 return direct_to_template(request, 'explorer/split.html', extra_context={
472 'splitform': sform, 'dcform': dcform, 'fileid': path} )
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']} )
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),
489 def _add_references(message, issues):
490 return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
492 def _get_issues_for_file(path):
493 if not path.endswith('.xml'):
494 raise ValueError('Path must end with .xml')
500 uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
501 return json.loads(uf.read())
502 except urllib2.HTTPError:
511 def pull_requests(request):
512 return direct_to_template(request, 'manager/pull_request.html', extra_context = {
513 'objects': models.PullRequest.objects.all()} )