+ print 'Saving whole text.', request.user.username
+ try:
+ # encode it back to UTF-8, so we can put it into repo
+ encoded_data = form.cleaned_data['content'].encode('utf-8')
+
+ def save_action():
+ repo._add_file(path, encoded_data)
+ repo._commit(message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'),\
+ user=request.user.username)
+
+ try:
+ # wczytaj dokument z ciągu znaków -> weryfikacja
+ document = parser.WLDocument.from_string(form.cleaned_data['content'])
+ except (ParseError, ValidationError), e:
+ warnings = [u'Niepoprawny dokument XML: ' + unicode(e.message)]
+
+ # save to user's branch
+ repo.in_branch(save_action, file_branch(path, request.user) );
+ except UnicodeDecodeError, e:
+ errors = [u'Błąd kodowania danych przed zapisem: ' + unicode(e.message)]
+ except hg.RepositoryException, e:
+ errors = [u'Błąd repozytorium: ' + unicode(e.message)]
+
+ if not errors:
+ errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
+
+ return HttpResponse( json.dumps({'result': errors and 'error' or 'ok',
+ 'errors': errors, 'warnings': warnings}) );
+
+ form = forms.BookForm()
+ data = repo.get_file(path, file_branch(path, request.user))
+ form.fields['content'].initial = data
+ return HttpResponse( json.dumps({'result': 'ok', 'content': data}) )
+
+@ajax_login_required
+@with_repo
+def file_update_local(request, path, repo):
+ result = None
+ errors = None
+
+ wlock = repo.write_lock()
+ try:
+ tipA = repo.get_branch_tip('default')
+ tipB = repo.get_branch_tip( file_branch(path, request.user) )
+
+ nodeA = repo.getnode(tipA)
+ nodeB = repo.getnode(tipB)
+
+ # do some wild checks - see file_commit() for more info
+ if (repo.common_ancestor(tipA, tipB) == nodeA) \
+ or (nodeB in nodeA.parents()):
+ result = 'nothing-to-do'
+ else:
+ # Case 2+
+ repo.merge_revisions(tipB, tipA, \
+ request.user.username, 'Personal branch update.')
+ result = 'done'
+ except hg.UncleanMerge, e:
+ errors = [e.message]
+ result = 'fatal-error'
+ except hg.RepositoryException, e:
+ errors = [e.message]
+ result = 'fatal-error'
+ finally:
+ wlock.release()
+
+ if result is None:
+ raise Exception("Ouch, this shouldn't happen!")
+
+ return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
+
+@ajax_login_required
+@with_repo
+def file_commit(request, path, repo):
+ result = None
+ errors = None
+ local_modified = False
+ if request.method == 'POST':
+ form = forms.MergeForm(request.POST)
+
+ if form.is_valid():
+ wlock = repo.write_lock()
+ try:
+ tipA = repo.get_branch_tip('default')
+ tipB = repo.get_branch_tip( file_branch(path, request.user) )
+
+ nodeA = repo.getnode(tipA)
+ nodeB = repo.getnode(tipB)
+
+ print repr(nodeA), repr(nodeB), repo.common_ancestor(tipA, tipB), repo.common_ancestor(tipB, tipA)
+
+ if repo.common_ancestor(tipB, tipA) == nodeA:
+ # Case 1:
+ # * tipB
+ # |
+ # * <- can also be here!
+ # /|
+ # / |
+ # tipA * *
+ # | |
+ # The local branch has been recently updated,
+ # so we don't need to update yet again, but we need to
+ # merge down to default branch, even if there was
+ # no commit's since last update
+ repo.merge_revisions(tipA, tipB, \
+ request.user.username, form.cleaned_data['message'])
+ result = 'done'
+ elif any( p.branch()==nodeB.branch() for p in nodeA.parents()):
+ # Case 2:
+ #
+ # tipA * * tipB
+ # |\ |
+ # | \|
+ # | *
+ # | |
+ # Default has no changes, to update from this branch
+ # since the last merge of local to default.
+ if nodeB not in nodeA.parents():
+ repo.merge_revisions(tipA, tipB, \
+ request.user.username, form.cleaned_data['message'])
+ result = 'done'
+ else:
+ result = 'nothing-to-do'
+ elif repo.common_ancestor(tipA, tipB) == nodeB:
+ # Case 3:
+ # tipA *
+ # |
+ # * <- this case overlaps with previos one
+ # |\
+ # | \
+ # | * tipB
+ # | |
+ #
+ # There was a recent merge to the defaul branch and
+ # no changes to local branch recently.
+ #
+ # Use the fact, that user is prepared to see changes, to
+ # update his branch if there are any
+ if nodeB not in nodeA.parents():
+ repo.merge_revisions(tipB, tipA, \
+ request.user.username, 'Personal branch update during merge.')
+ local_modified = True
+ result = 'done'
+ else:
+ result = 'nothing-to-do'
+ else:
+ # both branches have changes made to them, so
+ # first do an update
+ repo.merge_revisions(tipB, tipA, \
+ request.user.username, 'Personal branch update during merge.')
+
+ local_modified = True
+
+ # fetch the new tip
+ tipB = repo.get_branch_tip( file_branch(path, request.user) )
+
+ # and merge back to the default
+ repo.merge_revisions(tipA, tipB, \
+ request.user.username, form.cleaned_data['message'])
+ result = 'done'
+ except hg.UncleanMerge, e:
+ errors = [e.message]
+ result = 'fatal-error'
+ except hg.RepositoryException, e:
+ errors = [e.message]
+ result = 'fatal-error'
+ finally:
+ wlock.release()
+
+ if result is None:
+ errors = [ form.errors['message'].as_text() ]
+ if len(errors) > 0:
+ result = 'fatal-error'
+
+ return HttpResponse( json.dumps({'result': result, 'errors': errors, 'localmodified': local_modified}) );
+
+ return HttpResponse( json.dumps({'result': 'fatal-error', 'errors': ['No data posted']}) )
+
+
+@ajax_login_required
+@with_repo
+def file_dc(request, path, repo):
+ errors = None
+
+ if request.method == 'POST':
+ form = forms.DublinCoreForm(request.POST)
+
+ if form.is_valid():
+
+ def save_action():
+ file_contents = repo._get_file(path)
+
+ # wczytaj dokument z repozytorium
+ document = parser.WLDocument.from_string(file_contents)
+ document.book_info.update(form.cleaned_data)
+
+ # zapisz
+ repo._write_file(path, document.serialize().encode('utf-8'))
+ repo._commit( \
+ message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'), \
+ user=request.user.username )
+
+ try:
+ repo.in_branch(save_action, file_branch(path, request.user) )
+ except UnicodeEncodeError, e:
+ errors = ['Bład wewnętrzny: nie można zakodować pliku do utf-8']
+ except (ParseError, ValidationError), e:
+ errors = [e.message]
+
+ if errors is None:
+ errors = ["Pole '%s': %s\n" % (field[0], field[1].as_text()) for field in form.errors.iteritems()]
+
+ return HttpResponse( json.dumps({'result': errors and 'error' or 'ok', 'errors': errors}) );
+
+ # this is unused currently, but may come in handy
+ content = []