From: Łukasz Rekucki Date: Thu, 27 Aug 2009 16:25:31 +0000 (+0200) Subject: Dodanie obsługi zapisywania/wyświetlania z brancha. X-Git-Url: https://git.mdrn.pl/redakcja.git/commitdiff_plain/392a2d2ef7e9445ebdb238a4d43c8893875b02d0 Dodanie obsługi zapisywania/wyświetlania z brancha. --- diff --git a/apps/explorer/models.py b/apps/explorer/models.py index 1f8e93dc..2b2e8e81 100644 --- a/apps/explorer/models.py +++ b/apps/explorer/models.py @@ -12,3 +12,5 @@ def get_images_from_folder(folder): in os.listdir(os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR, folder)) if not fn.startswith('.')) +def user_branch(user): + return 'personal_'+user.username diff --git a/apps/explorer/views.py b/apps/explorer/views.py index 61d9a235..ea031216 100644 --- a/apps/explorer/views.py +++ b/apps/explorer/views.py @@ -10,27 +10,35 @@ from django.contrib.auth.decorators import login_required from explorer import forms, models -repo = hg.Repository(settings.REPOSITORY_PATH) -def file_list(request): +def with_repo(func): + def inner(request, *args, **kwargs): + kwargs['repo'] = hg.Repository(settings.REPOSITORY_PATH) + return func(request, *args, **kwargs) + return inner + +@with_repo +def file_list(request, repo): return direct_to_template(request, 'explorer/file_list.html', extra_context={ 'objects': repo.all_files(), }) - - # # Edit the file # -def file_xml(request, path): + +@with_repo +def file_xml(request, repo, path): if request.method == 'POST': form = forms.BookForm(request.POST) if form.is_valid(): print 'Saving whole text.', request.user.username def save_action(): - repo.add_file(path, form.cleaned_data['content']) - repo.commit(message='Local save at %s' % time.ctime(), user=request.user.username) + print 'In branch: ' + repo.repo[None].branch() + print repo._add_file(path, form.cleaned_data['content']) + print repo.repo.status() + print repo._commit(message='Local save at %s' % time.ctime(), user=request.user.username) - repo.in_branch('local_'+request.user.username, save_action); + print repo.in_branch(save_action, models.user_branch(request.user) ); result = "ok" else: result = "error" @@ -39,13 +47,13 @@ def file_xml(request, path): return HttpResponse( json.dumps({'result': result, 'errors': errors}) ); else: form = forms.BookForm() - form.fields['content'].initial = repo.get_file(path).data() - + form.fields['content'].initial = repo.get_file(path, models.user_branch(request.user)) return direct_to_template(request, 'explorer/edit_text.html', extra_context={ 'form': form, }) -def file_dc(request, path): +@with_repo +def file_dc(request, path, repo): if request.method == 'POST': form = forms.DublinCoreForm(request.POST) if form.is_valid(): @@ -57,7 +65,7 @@ def file_dc(request, path): errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() ) return HttpResponse( json.dumps({'result': result, 'errors': errors}) ); else: - fulltext = repo.get_file(path).data() + fulltext = repo.get_file(path, models.user_branch(request.user)) form = forms.DublinCoreForm(text=fulltext) return direct_to_template(request, 'explorer/edit_dc.html', extra_context={ @@ -75,9 +83,10 @@ def display_editor(request, path): # = Panel views = # =============== -def xmleditor_panel(request, path): +@with_repo +def xmleditor_panel(request, path, repo): form = forms.BookForm() - text = repo.get_file(path).data() + text = repo.get_file(path, models.user_branch(request.user)) return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={ 'fpath': path, @@ -91,16 +100,19 @@ def gallery_panel(request, path): 'form': forms.ImageFoldersForm(), }) - -def htmleditor_panel(request, path): +@with_repo +def htmleditor_panel(request, path, repo): + user_branch = models.user_branch(request.user) return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={ 'fpath': path, - 'html': html.transform(repo.get_file(path).data(), is_file=False), + 'html': html.transform(repo.get_file(path, user_branch), is_file=False), }) -def dceditor_panel(request, path): - text = repo.get_file(path).data() +@with_repo +def dceditor_panel(request, path, repo): + user_branch = models.user_branch(request.user) + text = repo.get_file(path, user_branch) form = forms.DublinCoreForm(text=text) return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={ diff --git a/lib/hg.py b/lib/hg.py index 2b885b3c..60e9f80e 100644 --- a/lib/hg.py +++ b/lib/hg.py @@ -1,14 +1,15 @@ # -*- coding: utf-8 -*- import os import codecs -from mercurial import localrepo, ui, error, match, node, encoding +from mercurial import localrepo, ui, match, node, encoding, util +import mercurial.merge, mercurial.error encoding.encoding = 'utf-8' + class RepositoryDoesNotExist(Exception): pass - class Repository(object): """Abstrakcja repozytorium Mercurial. Działa z Mercurial w wersji 1.3.1.""" @@ -25,7 +26,7 @@ class Repository(object): if os.path.isdir(path): try: return localrepo.localrepository(self.ui, path) - except error.RepoError: + except mercurial.error.RepoError: # dir is not an hg repo, we must init it if create: return localrepo.localrepository(self.ui, path, create=1) @@ -34,22 +35,33 @@ class Repository(object): return localrepo.localrepository(self.ui, path, create=1) raise RepositoryDoesNotExist("Repository %s does not exist." % path) - def all_files(self): - return list(self.repo['tip']) + def all_files(self, branch='default'): + return self.in_branch(lambda: self._all_files(), branch) + + def _all_files(self): + return list(self.repo[None]) - def get_file(self, path): - ctx = self.repo.changectx(None) - return ctx.filectx(path) + def get_file(self, path, branch='default'): + return self.in_branch(lambda: self._get_file(path), branch) + + def _get_file(self, path): + return self.repo.wread(path) - def add_file(self, path, value): - f = codecs.open(os.path.join(self.real_path, path), 'w', encoding='utf-8') - f.write(value) - f.close() + def add_file(self, path, value, branch='default'): + return self.in_branch(lambda: self._add_file(path, value), branch) - if path not in self._pending_files: - self._pending_files.append(path) + def _add_file(self, path, value): + return self.repo.wwrite(path, value.encode('utf-8'), []) +# f = codecs.open(os.path.join(self.real_path, path), 'w', encoding='utf-8') +# f.write(value) +# f.close() +# if path not in self._pending_files: +# self._pending_files.append(path) + + def _commit(self, message, user=None, key=None): + return self.repo.commit(text=message, user=user) - def commit(self, message=u'hgshelve auto commit', key=None, user=None): + def _commit2(self, message, key=None, user=None): """ Commit unsynchronized data to disk. Arguments:: @@ -109,16 +121,35 @@ class Repository(object): # self._keys = self.get_persisted_objects_keys() # return node.hex(rev) - def in_branch(self, branch_name, action): + def commit(self, message, key=None, user=None, branch='default'): + return self.in_branch(lambda: self._commit(message, key=key, user=user), branch) + + def in_branch(self, action, bname='default'): wlock = self.repo.wlock() try: - current_branch = self.repo[None].branch() - self.repo.dirstate.setbranch(branch_name) + old = self._switch_to_branch(bname) try: # do some stuff - action() + return action() finally: - self.repo.dirstate.setbranch(current_branch) + self._switch_to_branch(old) + finally: + wlock.release() + + def _switch_to_branch(self, bname): + wlock = self.repo.wlock() + try: + current = self.repo[None].branch() + if current == bname: + return current + + tip = self.repo.branchtags()[bname] + upstats = mercurial.merge.update(self.repo, tip, False, True, None) + return current + except KeyError, ke: + raise RepositoryException("Can't switch to branch '%s': no such branch." % bname , ke) + except util.Abort, ae: + raise repositoryException("Can't switch to branch '%s': %s" % (bname, ae.message), ae) finally: wlock.release() @@ -126,4 +157,9 @@ class Repository(object): """Returns w write lock to the repository.""" return self.repo.wlock() - + +class RepositoryException(Exception): + + def __init__(self, msg, cause=None): + Exception.__init__(self, msg) + self.cause = cause