# HG changeset patch # User Marcin Kuzminski # Date 2011-11-26 22:50:19 # Node ID 48d4fcf04a2983e75a6b6ea7ad1ee45a4f5c5b98 # Parent ac54aa4200e8e7e72a6053269dbeed06eb314a01 another major refactoring with session management diff --git a/rhodecode/controllers/admin/permissions.py b/rhodecode/controllers/admin/permissions.py --- a/rhodecode/controllers/admin/permissions.py +++ b/rhodecode/controllers/admin/permissions.py @@ -38,6 +38,7 @@ from rhodecode.lib.base import BaseContr from rhodecode.model.forms import DefaultPermissionsForm from rhodecode.model.permission import PermissionModel from rhodecode.model.db import User +from rhodecode.model.meta import Session log = logging.getLogger(__name__) @@ -101,6 +102,7 @@ class PermissionsController(BaseControll form_result = _form.to_python(dict(request.POST)) form_result.update({'perm_user_name': id}) permission_model.update(form_result) + Session().commit() h.flash(_('Default permissions updated successfully'), category='success') diff --git a/rhodecode/controllers/admin/repos_groups.py b/rhodecode/controllers/admin/repos_groups.py --- a/rhodecode/controllers/admin/repos_groups.py +++ b/rhodecode/controllers/admin/repos_groups.py @@ -17,6 +17,7 @@ from rhodecode.lib.base import BaseContr from rhodecode.model.db import RepoGroup from rhodecode.model.repos_group import ReposGroupModel from rhodecode.model.forms import ReposGroupForm +from rhodecode.model.meta import Session log = logging.getLogger(__name__) @@ -65,12 +66,12 @@ class ReposGroupsController(BaseControll """POST /repos_groups: Create a new item""" # url('repos_groups') self.__load_defaults() - repos_group_model = ReposGroupModel() repos_group_form = ReposGroupForm(available_groups= c.repo_groups_choices)() try: form_result = repos_group_form.to_python(dict(request.POST)) - repos_group_model.create(form_result) + ReposGroupModel().create(form_result) + Session().commit() h.flash(_('created repos group %s') \ % form_result['group_name'], category='success') #TODO: in futureaction_logger(, '', '', '', self.sa) @@ -110,14 +111,14 @@ class ReposGroupsController(BaseControll self.__load_defaults() c.repos_group = RepoGroup.get(id) - repos_group_model = ReposGroupModel() repos_group_form = ReposGroupForm(edit=True, old_data=c.repos_group.get_dict(), available_groups= c.repo_groups_choices)() try: form_result = repos_group_form.to_python(dict(request.POST)) - repos_group_model.update(id, form_result) + ReposGroupModel().update(id, form_result) + Session().commit() h.flash(_('updated repos group %s') \ % form_result['group_name'], category='success') #TODO: in futureaction_logger(, '', '', '', self.sa) @@ -147,7 +148,6 @@ class ReposGroupsController(BaseControll # method='delete') # url('repos_group', id=ID) - repos_group_model = ReposGroupModel() gr = RepoGroup.get(id) repos = gr.repositories.all() if repos: @@ -157,7 +157,8 @@ class ReposGroupsController(BaseControll return redirect(url('repos_groups')) try: - repos_group_model.delete(id) + ReposGroupModel().delete(id) + Session().commit() h.flash(_('removed repos group %s' % gr.group_name), category='success') #TODO: in future action_logger(, '', '', '', self.sa) except IntegrityError, e: diff --git a/rhodecode/controllers/api/api.py b/rhodecode/controllers/api/api.py --- a/rhodecode/controllers/api/api.py +++ b/rhodecode/controllers/api/api.py @@ -1,19 +1,21 @@ import traceback import logging +from sqlalchemy.orm.exc import NoResultFound + from rhodecode.controllers.api import JSONRPCController, JSONRPCError from rhodecode.lib.auth import HasPermissionAllDecorator, \ HasPermissionAnyDecorator + +from rhodecode.model.meta import Session from rhodecode.model.scm import ScmModel - from rhodecode.model.db import User, UsersGroup, RepoGroup, Repository from rhodecode.model.repo import RepoModel from rhodecode.model.user import UserModel from rhodecode.model.repo_permission import RepositoryPermissionModel from rhodecode.model.users_group import UsersGroupModel -from rhodecode.model import users_group from rhodecode.model.repos_group import ReposGroupModel -from sqlalchemy.orm.exc import NoResultFound + log = logging.getLogger(__name__) @@ -116,8 +118,9 @@ class ApiController(JSONRPCController): raise JSONRPCError("user %s already exist" % username) try: - UserModel().create_or_update(username, password, email, firstname, + UserModel().create_or_update(username, password, email, firstname, lastname, active, admin, ldap_dn) + Session().commit() return dict(msg='created new user %s' % username) except Exception: log.error(traceback.format_exc()) @@ -194,9 +197,8 @@ class ApiController(JSONRPCController): raise JSONRPCError("users group %s already exist" % name) try: - form_data = dict(users_group_name=name, - users_group_active=active) - ug = UsersGroup.create(form_data) + ug = UsersGroupModel().create(name=name, active=active) + Session().commit() return dict(id=ug.users_group_id, msg='created new users group %s' % name) except Exception: @@ -224,7 +226,7 @@ class ApiController(JSONRPCController): raise JSONRPCError('unknown user %s' % user_name) ugm = UsersGroupModel().add_user_to_group(users_group, user) - + Session().commit() return dict(id=ugm.users_group_member_id, msg='created new users group member') except Exception: @@ -291,7 +293,7 @@ class ApiController(JSONRPCController): return result @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') - def create_repo(self, apiuser, name, owner_name, description='', + def create_repo(self, apiuser, name, owner_name, description='', repo_type='hg', private=False): """ Create a repository @@ -332,6 +334,7 @@ class ApiController(JSONRPCController): repo_type=repo_type, repo_group=parent_id, clone_uri=None), owner) + Session().commit() except Exception: log.error(traceback.format_exc()) raise JSONRPCError('failed to create repository %s' % name) @@ -360,6 +363,7 @@ class ApiController(JSONRPCController): RepositoryPermissionModel()\ .update_or_delete_user_permission(repo, user, perm) + Session().commit() except Exception: log.error(traceback.format_exc()) raise JSONRPCError('failed to edit permission %(repo)s for %(user)s' diff --git a/rhodecode/lib/db_manage.py b/rhodecode/lib/db_manage.py --- a/rhodecode/lib/db_manage.py +++ b/rhodecode/lib/db_manage.py @@ -78,16 +78,11 @@ class DbManage(object): log.info('Created tables for %s', self.dbname) def set_db_version(self): - try: - ver = DbMigrateVersion() - ver.version = __dbversion__ - ver.repository_id = 'rhodecode_db_migrations' - ver.repository_path = 'versions' - self.sa.add(ver) - self.sa.commit() - except: - self.sa.rollback() - raise + ver = DbMigrateVersion() + ver.version = __dbversion__ + ver.repository_id = 'rhodecode_db_migrations' + ver.repository_path = 'versions' + self.sa.add(ver) log.info('db version set to: %s', __dbversion__) def upgrade(self): @@ -162,7 +157,7 @@ class DbManage(object): self.klass.fix_settings() print ('Adding ldap defaults') self.klass.create_ldap_options(skip_existing=True) - + upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1) #CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE @@ -299,43 +294,33 @@ class DbManage(object): dotencode_disable.ui_value = 'false' # enable largefiles - dotencode_disable = RhodeCodeUi() - dotencode_disable.ui_section = 'extensions' - dotencode_disable.ui_key = 'largefiles' - dotencode_disable.ui_value = '1' + largefiles = RhodeCodeUi() + largefiles.ui_section = 'extensions' + largefiles.ui_key = 'largefiles' + largefiles.ui_value = '1' - try: - self.sa.add(hooks1) - self.sa.add(hooks2) - self.sa.add(hooks3) - self.sa.add(hooks4) - self.sa.add(dotencode_disable) - self.sa.commit() - except: - self.sa.rollback() - raise + self.sa.add(hooks1) + self.sa.add(hooks2) + self.sa.add(hooks3) + self.sa.add(hooks4) + self.sa.add(largefiles) - def create_ldap_options(self,skip_existing=False): + def create_ldap_options(self, skip_existing=False): """Creates ldap settings""" - try: - for k, v in [('ldap_active', 'false'), ('ldap_host', ''), - ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'), - ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''), - ('ldap_dn_pass', ''), ('ldap_base_dn', ''), - ('ldap_filter', ''), ('ldap_search_scope', ''), - ('ldap_attr_login', ''), ('ldap_attr_firstname', ''), - ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]: + for k, v in [('ldap_active', 'false'), ('ldap_host', ''), + ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'), + ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''), + ('ldap_dn_pass', ''), ('ldap_base_dn', ''), + ('ldap_filter', ''), ('ldap_search_scope', ''), + ('ldap_attr_login', ''), ('ldap_attr_firstname', ''), + ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]: - if skip_existing and RhodeCodeSetting.get_by_name(k) != None: - log.debug('Skipping option %s' % k) - continue - setting = RhodeCodeSetting(k, v) - self.sa.add(setting) - self.sa.commit() - except: - self.sa.rollback() - raise + if skip_existing and RhodeCodeSetting.get_by_name(k) != None: + log.debug('Skipping option %s' % k) + continue + setting = RhodeCodeSetting(k, v) + self.sa.add(setting) def config_prompt(self, test_repo_path='', retries=3): if retries == 3: @@ -401,20 +386,14 @@ class DbManage(object): hgsettings2 = RhodeCodeSetting('title', 'RhodeCode') hgsettings3 = RhodeCodeSetting('ga_code', '') - try: - self.sa.add(web1) - self.sa.add(web2) - self.sa.add(web3) - self.sa.add(web4) - self.sa.add(paths) - self.sa.add(hgsettings1) - self.sa.add(hgsettings2) - self.sa.add(hgsettings3) - - self.sa.commit() - except: - self.sa.rollback() - raise + self.sa.add(web1) + self.sa.add(web2) + self.sa.add(web3) + self.sa.add(web4) + self.sa.add(paths) + self.sa.add(hgsettings1) + self.sa.add(hgsettings2) + self.sa.add(hgsettings3) self.create_ldap_options() @@ -422,18 +401,18 @@ class DbManage(object): def create_user(self, username, password, email='', admin=False): log.info('creating user %s', username) - UserModel().create_or_update(username, password, email, - name='RhodeCode', lastname='Admin', + UserModel().create_or_update(username, password, email, + name='RhodeCode', lastname='Admin', active=True, admin=admin) def create_default_user(self): log.info('creating default user') # create default user for handling default permissions. - UserModel().create_or_update(username='default', - password=str(uuid.uuid1())[:8], - email='anonymous@rhodecode.org', + UserModel().create_or_update(username='default', + password=str(uuid.uuid1())[:8], + email='anonymous@rhodecode.org', name='Anonymous', lastname='User') - + def create_permissions(self): #module.(access|create|change|delete)_[name] #module.(read|write|owner) @@ -458,12 +437,7 @@ class DbManage(object): new_perm = Permission() new_perm.permission_name = p[0] new_perm.permission_longname = p[1] - try: - self.sa.add(new_perm) - self.sa.commit() - except: - self.sa.rollback() - raise + self.sa.add(new_perm) def populate_default_permissions(self): log.info('creating default user permissions') @@ -489,11 +463,6 @@ class DbManage(object): .filter(Permission.permission_name == 'repository.read')\ .scalar() - try: - self.sa.add(reg_perm) - self.sa.add(create_repo_perm) - self.sa.add(default_repo_perm) - self.sa.commit() - except: - self.sa.rollback() - raise + self.sa.add(reg_perm) + self.sa.add(create_repo_perm) + self.sa.add(default_repo_perm) diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -51,6 +51,7 @@ from rhodecode.lib.caching_query import from rhodecode.model import meta from rhodecode.model.db import Repository, User, RhodeCodeUi, \ UserLog, RepoGroup, RhodeCodeSetting +from rhodecode.model.meta import Session log = logging.getLogger(__name__) @@ -509,7 +510,7 @@ def create_test_env(repos_test_path, con dbmanage.admin_prompt() dbmanage.create_permissions() dbmanage.populate_default_permissions() - + Session().commit() # PART TWO make test repo log.debug('making test vcs repositories') diff --git a/rhodecode/model/comment.py b/rhodecode/model/comment.py --- a/rhodecode/model/comment.py +++ b/rhodecode/model/comment.py @@ -109,7 +109,6 @@ class ChangesetCommentsModel(BaseModel): recipients=mention_recipients, type_=Notification.TYPE_CHANGESET_COMMENT) - self.sa.commit() return comment def delete(self, comment): diff --git a/rhodecode/model/permission.py b/rhodecode/model/permission.py --- a/rhodecode/model/permission.py +++ b/rhodecode/model/permission.py @@ -70,8 +70,8 @@ class PermissionModel(BaseModel): def update(self, form_result): perm_user = self.sa.query(User)\ - .filter(User.username == - form_result['perm_user_name']).scalar() + .filter(User.username == + form_result['perm_user_name']).scalar() u2p = self.sa.query(UserToPerm).filter(UserToPerm.user == perm_user).all() if len(u2p) != 3: @@ -110,8 +110,6 @@ class PermissionModel(BaseModel): perm_user.active = bool(form_result['anonymous']) self.sa.add(perm_user) - self.sa.commit() except (DatabaseError,): log.error(traceback.format_exc()) - self.sa.rollback() raise diff --git a/rhodecode/model/repo_permission.py b/rhodecode/model/repo_permission.py --- a/rhodecode/model/repo_permission.py +++ b/rhodecode/model/repo_permission.py @@ -50,13 +50,11 @@ class RepositoryPermissionModel(BaseMode p.repository = repository p.permission = permission self.sa.add(p) - self.sa.commit() def delete_user_permission(self, repository, user): current = self.get_user_permission(repository, user) if current: self.sa.delete(current) - self.sa.commit() def update_or_delete_user_permission(self, repository, user, permission): if permission: diff --git a/rhodecode/model/repos_group.py b/rhodecode/model/repos_group.py --- a/rhodecode/model/repos_group.py +++ b/rhodecode/model/repos_group.py @@ -111,14 +111,12 @@ class ReposGroupModel(BaseModel): new_repos_group.group_name = new_repos_group.get_new_name(form_data['group_name']) self.sa.add(new_repos_group) - + self.sa.flush() self.__create_group(new_repos_group.group_name) - self.sa.commit() return new_repos_group except: log.error(traceback.format_exc()) - self.sa.rollback() raise def update(self, repos_group_id, form_data): @@ -126,7 +124,7 @@ class ReposGroupModel(BaseModel): try: repos_group = RepoGroup.get(repos_group_id) old_path = repos_group.full_path - + # change properties repos_group.group_description = form_data['group_description'] repos_group.parent_group = RepoGroup.get(form_data['group_parent_id']) @@ -144,11 +142,9 @@ class ReposGroupModel(BaseModel): r.repo_name = r.get_new_name(r.just_name) self.sa.add(r) - self.sa.commit() return repos_group except: log.error(traceback.format_exc()) - self.sa.rollback() raise def delete(self, users_group_id): @@ -156,8 +152,6 @@ class ReposGroupModel(BaseModel): users_group = RepoGroup.get(users_group_id) self.sa.delete(users_group) self.__delete_group(users_group) - self.sa.commit() except: log.error(traceback.format_exc()) - self.sa.rollback() raise diff --git a/rhodecode/model/user.py b/rhodecode/model/user.py --- a/rhodecode/model/user.py +++ b/rhodecode/model/user.py @@ -129,13 +129,10 @@ class UserModel(BaseModel): new_user.ldap_dn = safe_unicode(ldap_dn) if ldap_dn else None new_user.name = name new_user.lastname = lastname - self.sa.add(new_user) - self.sa.commit() return new_user except (DatabaseError,): log.error(traceback.format_exc()) - self.sa.rollback() raise diff --git a/rhodecode/model/users_group.py b/rhodecode/model/users_group.py --- a/rhodecode/model/users_group.py +++ b/rhodecode/model/users_group.py @@ -43,22 +43,7 @@ class UsersGroupModel(BaseModel): def get_by_name(self, name, cache=False, case_insensitive=False): return UsersGroup.get_by_group_name(name, cache, case_insensitive) - def create(self, form_data): - try: - new_users_group = UsersGroup() - for k, v in form_data.items(): - setattr(new_users_group, k, v) - - self.sa.add(new_users_group) - self.sa.commit() - return new_users_group - except: - log.error(traceback.format_exc()) - self.sa.rollback() - raise - - - def create_(self, name, active=True): + def create(self, name, active=True): new = UsersGroup() new.users_group_name = name new.users_group_active = active @@ -84,9 +69,7 @@ class UsersGroupModel(BaseModel): user.group_member.append(users_group_member) self.sa.add(users_group_member) - self.sa.commit() return users_group_member except: log.error(traceback.format_exc()) - self.sa.rollback() raise diff --git a/rhodecode/tests/_test_concurency.py b/rhodecode/tests/_test_concurency.py --- a/rhodecode/tests/_test_concurency.py +++ b/rhodecode/tests/_test_concurency.py @@ -53,7 +53,7 @@ add_cache(conf) USER = 'test_admin' PASS = 'test12' -HOST = '127.0.0.1:5000' +HOST = 'hg.local' METHOD = 'pull' DEBUG = True log = logging.getLogger(__name__) diff --git a/rhodecode/tests/test_models.py b/rhodecode/tests/test_models.py --- a/rhodecode/tests/test_models.py +++ b/rhodecode/tests/test_models.py @@ -42,6 +42,7 @@ class TestReposGroups(unittest.TestCase) group_description=desc, group_parent_id=parent_id) gr = ReposGroupModel().create(form_data) + Session.commit() return gr def __delete_group(self, id_): @@ -65,7 +66,7 @@ class TestReposGroups(unittest.TestCase) def test_create_same_name_group(self): self.assertRaises(IntegrityError, lambda:self.__make_group('newGroup')) - + Session().rollback() def test_same_subgroup(self): sg1 = self.__make_group('sub1', parent_id=self.g1.group_id) @@ -162,16 +163,20 @@ class TestUser(unittest.TestCase): usr = UserModel().create_or_update(username=u'test_user', password=u'qweqwe', email=u'u232@rhodecode.org', name=u'u1', lastname=u'u1') + Session().commit() self.assertEqual(User.get_by_username(u'test_user'), usr) # make users group - users_group = UsersGroupModel().create_('some_example_group') + users_group = UsersGroupModel().create('some_example_group') Session().commit() + UsersGroupModel().add_user_to_group(users_group, usr) + Session().commit() self.assertEqual(UsersGroup.get(users_group.users_group_id), users_group) self.assertEqual(UsersGroupMember.query().count(), 1) UserModel().delete(usr.user_id) + Session().commit() self.assertEqual(UsersGroupMember.query().all(), []) @@ -182,15 +187,24 @@ class TestNotifications(unittest.TestCas self.u1 = UserModel().create_or_update(username=u'u1', password=u'qweqwe', email=u'u1@rhodecode.org', - name=u'u1', lastname=u'u1').user_id + name=u'u1', lastname=u'u1') + Session.commit() + self.u1 = self.u1.user_id + self.u2 = UserModel().create_or_update(username=u'u2', password=u'qweqwe', email=u'u2@rhodecode.org', - name=u'u2', lastname=u'u3').user_id + name=u'u2', lastname=u'u3') + Session.commit() + self.u2 = self.u2.user_id + self.u3 = UserModel().create_or_update(username=u'u3', password=u'qweqwe', email=u'u3@rhodecode.org', - name=u'u3', lastname=u'u3').user_id + name=u'u3', lastname=u'u3') + Session.commit() + self.u3 = self.u3.user_id + super(TestNotifications, self).__init__(methodName=methodName) def _clean_notifications(self): diff --git a/rhodecode/websetup.py b/rhodecode/websetup.py --- a/rhodecode/websetup.py +++ b/rhodecode/websetup.py @@ -28,6 +28,7 @@ import logging from rhodecode.config.environment import load_environment from rhodecode.lib.db_manage import DbManage +from rhodecode.model.meta import Session log = logging.getLogger(__name__) @@ -45,5 +46,5 @@ def setup_app(command, conf, vars): dbmanage.admin_prompt() dbmanage.create_permissions() dbmanage.populate_default_permissions() - + Session().commit() load_environment(conf.global_conf, conf.local_conf, initial=True)