diff --git a/pylons_app/controllers/settings.py b/pylons_app/controllers/settings.py --- a/pylons_app/controllers/settings.py +++ b/pylons_app/controllers/settings.py @@ -17,6 +17,11 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. +""" +Created on June 30, 2010 +settings controller for pylons +@author: marcink +""" from formencode import htmlfill from pylons import tmpl_context as c, request, url from pylons.controllers.util import redirect @@ -30,11 +35,7 @@ import formencode import logging import pylons_app.lib.helpers as h import traceback -""" -Created on June 30, 2010 -settings controller for pylons -@author: marcink -""" + log = logging.getLogger(__name__) class SettingsController(BaseController): @@ -72,6 +73,7 @@ class SettingsController(BaseController) def update(self, repo_name): repo_model = RepoModel() + changed_name = repo_name _form = RepoSettingsForm(edit=True, old_data={'repo_name':repo_name})() try: form_result = _form.to_python(dict(request.POST)) @@ -79,7 +81,7 @@ class SettingsController(BaseController) invalidate_cache('cached_repo_list') h.flash(_('Repository %s updated succesfully' % repo_name), category='success') - + changed_name = form_result['repo_name'] except formencode.Invalid as errors: c.repo_info = repo_model.get(repo_name) c.users_array = repo_model.get_users_js() @@ -93,6 +95,6 @@ class SettingsController(BaseController) except Exception: log.error(traceback.format_exc()) h.flash(_('error occured during update of repository %s') \ - % form_result['repo_name'], category='error') + % repo_name, category='error') - return redirect(url('repo_settings_home', repo_name=form_result['repo_name'])) + return redirect(url('repo_settings_home', repo_name=changed_name)) diff --git a/pylons_app/lib/auth.py b/pylons_app/lib/auth.py --- a/pylons_app/lib/auth.py +++ b/pylons_app/lib/auth.py @@ -106,12 +106,17 @@ def set_base_path(config): config['base_path'] = config['pylons.app_globals'].base_path def fill_perms(user): + """ + Fills user permission attribute with permissions taken from database + @param user: + """ + sa = meta.Session user.permissions['repositories'] = {} #first fetch default permissions default_perms = sa.query(Repo2Perm, Repository, Permission)\ - .join((Repository, Repo2Perm.repository == Repository.repo_name))\ + .join((Repository, Repo2Perm.repository_id == Repository.repo_id))\ .join((Permission, Repo2Perm.permission_id == Permission.permission_id))\ .filter(Repo2Perm.user_id == sa.query(User).filter(User.username == 'default').one().user_id).all() @@ -121,7 +126,7 @@ def fill_perms(user): #admin have all rights full for perm in default_perms: p = 'repository.admin' - user.permissions['repositories'][perm.Repo2Perm.repository] = p + user.permissions['repositories'][perm.Repo2Perm.repository.repo_name] = p else: user.permissions['global'] = set() @@ -135,11 +140,11 @@ def fill_perms(user): else: p = perm.Permission.permission_name - user.permissions['repositories'][perm.Repo2Perm.repository] = p + user.permissions['repositories'][perm.Repo2Perm.repository.repo_name] = p user_perms = sa.query(Repo2Perm, Permission, Repository)\ - .join((Repository, Repo2Perm.repository == Repository.repo_name))\ + .join((Repository, Repo2Perm.repository_id == Repository.repo_id))\ .join((Permission, Repo2Perm.permission_id == Permission.permission_id))\ .filter(Repo2Perm.user_id == user.user_id).all() #overwrite userpermissions with defaults @@ -149,7 +154,7 @@ def fill_perms(user): p = 'repository.write' else: p = perm.Permission.permission_name - user.permissions['repositories'][perm.Repo2Perm.repository] = p + user.permissions['repositories'][perm.Repo2Perm.repository.repo_name] = p meta.Session.remove() return user @@ -159,10 +164,9 @@ def get_user(session): @param session: """ user = session.get('hg_app_user', AuthUser()) - + if user.is_authenticated: user = fill_perms(user) - session['hg_app_user'] = user session.save() return user diff --git a/pylons_app/lib/db_manage.py b/pylons_app/lib/db_manage.py --- a/pylons_app/lib/db_manage.py +++ b/pylons_app/lib/db_manage.py @@ -126,7 +126,7 @@ class DbManage(object): hgsettings.app_title = 'hg-app' try: - self.sa.add(hooks) + #self.sa.add(hooks) self.sa.add(web1) self.sa.add(web2) self.sa.add(web3) diff --git a/pylons_app/lib/utils.py b/pylons_app/lib/utils.py --- a/pylons_app/lib/utils.py +++ b/pylons_app/lib/utils.py @@ -205,7 +205,7 @@ def repo2db_mapper(initial_repo_list, re rm = RepoModel() for name, repo in initial_repo_list.items(): - if not sa.query(Repository).get(name): + if not sa.query(Repository).filter(Repository.repo_name == name).scalar(): log.info('repository %s not found creating default', name) form_data = { diff --git a/pylons_app/model/db.py b/pylons_app/model/db.py --- a/pylons_app/model/db.py +++ b/pylons_app/model/db.py @@ -54,8 +54,9 @@ class UserLog(Base): class Repository(Base): __tablename__ = 'repositories' - __table_args__ = {'useexisting':True} - repo_name = Column("repo_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None, primary_key=True) + __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) + repo_id = Column("repo_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) + repo_name = Column("repo_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None) private = Column("private", BOOLEAN(), nullable=True, unique=None, default=None) description = Column("description", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) @@ -75,12 +76,12 @@ class Permission(Base): class Repo2Perm(Base): __tablename__ = 'repo_to_perm' - __table_args__ = (UniqueConstraint('user_id', 'repository'), {'useexisting':True}) + __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True}) repo2perm_id = Column("repo2perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None) permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None) - repository = Column("repository", TEXT(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_name'), nullable=False, unique=None, default=None) + repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None) user = relation('User') permission = relation('Permission') - + repository = relation('Repository') diff --git a/pylons_app/model/forms.py b/pylons_app/model/forms.py --- a/pylons_app/model/forms.py +++ b/pylons_app/model/forms.py @@ -164,10 +164,9 @@ def ValidRepoName(edit, old_data): if slug in ['_admin']: raise formencode.Invalid(_('This repository name is disallowed'), value, state) - if old_data.get('repo_name') != value or not edit: sa = meta.Session - if sa.query(Repository).get(slug): + if sa.query(Repository).filter(Repository.repo_name == slug).scalar(): raise formencode.Invalid(_('This repository already exists') , value, state) meta.Session.remove() diff --git a/pylons_app/model/repo_model.py b/pylons_app/model/repo_model.py --- a/pylons_app/model/repo_model.py +++ b/pylons_app/model/repo_model.py @@ -16,21 +16,20 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. - """ Created on Jun 5, 2010 model for handling repositories actions @author: marcink """ -from pylons_app.model.meta import Session +from datetime import datetime +from pylons import app_globals as g +from pylons_app.lib.utils import check_repo from pylons_app.model.db import Repository, Repo2Perm, User, Permission -import shutil +from pylons_app.model.meta import Session +import logging import os -from datetime import datetime -from pylons_app.lib.utils import check_repo -from pylons import app_globals as g +import shutil import traceback -import logging log = logging.getLogger(__name__) class RepoModel(object): @@ -39,7 +38,7 @@ class RepoModel(object): self.sa = Session() def get(self, id): - return self.sa.query(Repository).get(id) + return self.sa.query(Repository).filter(Repository.repo_name == id).scalar() def get_users_js(self): @@ -51,42 +50,49 @@ class RepoModel(object): return users_array - def update(self, repo_id, form_data): + def update(self, repo_name, form_data): try: - if repo_id != form_data['repo_name']: - #rename our data - self.__rename_repo(repo_id, form_data['repo_name']) - cur_repo = self.sa.query(Repository).get(repo_id) - for k, v in form_data.items(): - if k == 'user': - cur_repo.user_id = v - else: - setattr(cur_repo, k, v) - + #update permissions for username, perm in form_data['perms_updates']: r2p = self.sa.query(Repo2Perm)\ .filter(Repo2Perm.user == self.sa.query(User)\ .filter(User.username == username).one())\ - .filter(Repo2Perm.repository == repo_id).one() + .filter(Repo2Perm.repository == self.get(repo_name))\ + .one() r2p.permission_id = self.sa.query(Permission).filter( Permission.permission_name == perm).one().permission_id self.sa.add(r2p) + #set new permissions for username, perm in form_data['perms_new']: r2p = Repo2Perm() - r2p.repository = repo_id + r2p.repository = self.get(repo_name) r2p.user = self.sa.query(User)\ .filter(User.username == username).one() r2p.permission_id = self.sa.query(Permission).filter( - Permission.permission_name == - perm).one().permission_id + Permission.permission_name == perm)\ + .one().permission_id self.sa.add(r2p) - + + #update current repo + cur_repo = self.get(repo_name) + + for k, v in form_data.items(): + if k == 'user': + cur_repo.user_id = v + else: + setattr(cur_repo, k, v) + self.sa.add(cur_repo) + + if repo_name != form_data['repo_name']: + #rename our data + self.__rename_repo(repo_name, form_data['repo_name']) + self.sa.commit() except: log.error(traceback.format_exc()) @@ -102,7 +108,7 @@ class RepoModel(object): new_repo.user_id = cur_user.user_id self.sa.add(new_repo) - + #create default permission repo2perm = Repo2Perm() default_perm = 'repository.none' if form_data['private'] \ @@ -111,7 +117,7 @@ class RepoModel(object): .filter(Permission.permission_name == default_perm)\ .one().permission_id - repo2perm.repository = repo_name + repo2perm.repository_id = new_repo.repo_id repo2perm.user_id = self.sa.query(User)\ .filter(User.username == 'default').one().user_id @@ -133,10 +139,12 @@ class RepoModel(object): log.error(traceback.format_exc()) self.sa.rollback() raise + def delete_perm_user(self, form_data, repo_name): try: - r2p = self.sa.query(Repo2Perm).filter(Repo2Perm.repository == repo_name)\ - .filter(Repo2Perm.user_id == form_data['user_id']).delete() + self.sa.query(Repo2Perm)\ + .filter(Repo2Perm.repository == self.get(repo_name))\ + .filter(Repo2Perm.user_id == form_data['user_id']).delete() self.sa.commit() except: log.error(traceback.format_exc())