# HG changeset patch # User Marcin Kuzminski # Date 2011-12-02 20:31:13 # Node ID 8ecc6b8229a56c62beedfc95dd68915e0621df53 # Parent a3ee2611e6e8b872a797fdb35927239787bbd147 commit less models - models don't do any commits(with few exceptions) - all db transactions should be handled by higher level modules like controllers, celery tasks 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 @@ -102,7 +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() + Session.commit() h.flash(_('Default permissions updated successfully'), category='success') diff --git a/rhodecode/controllers/admin/repos.py b/rhodecode/controllers/admin/repos.py --- a/rhodecode/controllers/admin/repos.py +++ b/rhodecode/controllers/admin/repos.py @@ -150,7 +150,7 @@ class ReposController(BaseController): else: action_logger(self.rhodecode_user, 'admin_created_repo', form_result['repo_name_full'], '', self.sa) - Session().commit() + Session.commit() except formencode.Invalid, errors: c.new_repo = errors.value['repo_name'] @@ -208,7 +208,7 @@ class ReposController(BaseController): changed_name = repo.repo_name action_logger(self.rhodecode_user, 'admin_updated_repo', changed_name, '', self.sa) - Session().commit() + Session.commit() except formencode.Invalid, errors: defaults = self.__load_data(repo_name) defaults.update(errors.value) @@ -252,7 +252,7 @@ class ReposController(BaseController): repo_model.delete(repo) invalidate_cache('get_repo_cached_%s' % repo_name) h.flash(_('deleted repository %s') % repo_name, category='success') - Session().commit() + Session.commit() except IntegrityError, e: if e.message.find('repositories_fork_id_fkey'): log.error(traceback.format_exc()) 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 @@ -71,7 +71,7 @@ class ReposGroupsController(BaseControll try: form_result = repos_group_form.to_python(dict(request.POST)) ReposGroupModel().create(form_result) - Session().commit() + Session.commit() h.flash(_('created repos group %s') \ % form_result['group_name'], category='success') #TODO: in futureaction_logger(, '', '', '', self.sa) @@ -118,7 +118,7 @@ class ReposGroupsController(BaseControll try: form_result = repos_group_form.to_python(dict(request.POST)) ReposGroupModel().update(id, form_result) - Session().commit() + Session.commit() h.flash(_('updated repos group %s') \ % form_result['group_name'], category='success') #TODO: in futureaction_logger(, '', '', '', self.sa) @@ -158,7 +158,7 @@ class ReposGroupsController(BaseControll try: ReposGroupModel().delete(id) - Session().commit() + 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/admin/settings.py b/rhodecode/controllers/admin/settings.py --- a/rhodecode/controllers/admin/settings.py +++ b/rhodecode/controllers/admin/settings.py @@ -48,6 +48,7 @@ from rhodecode.model.scm import ScmModel from rhodecode.model.user import UserModel from rhodecode.model.db import User from rhodecode.model.notification import EmailNotificationModel +from rhodecode.model.meta import Session log = logging.getLogger(__name__) @@ -247,7 +248,7 @@ class SettingsController(BaseController) if update: h.flash(_('Updated hooks'), category='success') - + Session.commit() except: log.error(traceback.format_exc()) h.flash(_('error occurred during hook creation'), @@ -353,7 +354,7 @@ class SettingsController(BaseController) user_model.update_my_account(uid, form_result) h.flash(_('Your account was updated successfully'), category='success') - + Session.commit() except formencode.Invalid, errors: c.user = User.get(self.rhodecode_user.user_id) all_repos = self.sa.query(Repository)\ diff --git a/rhodecode/controllers/admin/users.py b/rhodecode/controllers/admin/users.py --- a/rhodecode/controllers/admin/users.py +++ b/rhodecode/controllers/admin/users.py @@ -41,6 +41,7 @@ from rhodecode.lib.base import BaseContr from rhodecode.model.db import User, UserRepoToPerm, UserToPerm, Permission from rhodecode.model.forms import UserForm from rhodecode.model.user import UserModel +from rhodecode.model.meta import Session log = logging.getLogger(__name__) @@ -77,6 +78,7 @@ class UsersController(BaseController): user_model.create(form_result) h.flash(_('created user %s') % form_result['username'], category='success') + Session.commit() #action_logger(self.rhodecode_user, 'new_user', '', '', self.sa) except formencode.Invalid, errors: return htmlfill.render( @@ -114,11 +116,11 @@ class UsersController(BaseController): form_result = _form.to_python(dict(request.POST)) user_model.update(id, form_result) h.flash(_('User updated successfully'), category='success') - + Session.commit() except formencode.Invalid, errors: e = errors.error_dict or {} perm = Permission.get_by_key('hg.create.repository') - e.update({'create_repo_perm': UserToPerm.has_perm(id, perm)}) + e.update({'create_repo_perm': user_model.has_perm(id, perm)}) return htmlfill.render( render('admin/users/user_edit.html'), defaults=errors.value, @@ -144,6 +146,7 @@ class UsersController(BaseController): try: user_model.delete(id) h.flash(_('successfully deleted user'), category='success') + Session.commit() except (UserOwnsReposException, DefaultUserException), e: h.flash(str(e), category='warning') except Exception: @@ -158,20 +161,19 @@ class UsersController(BaseController): def edit(self, id, format='html'): """GET /users/id/edit: Form to edit an existing item""" # url('edit_user', id=ID) - user_model = UserModel() - c.user = user_model.get(id) + c.user = User.get(id) if not c.user: return redirect(url('users')) if c.user.username == 'default': h.flash(_("You can't edit this user"), category='warning') return redirect(url('users')) c.user.permissions = {} - c.granted_permissions = user_model.fill_perms(c.user)\ + c.granted_permissions = UserModel().fill_perms(c.user)\ .permissions['global'] defaults = c.user.get_dict() perm = Permission.get_by_key('hg.create.repository') - defaults.update({'create_repo_perm': UserToPerm.has_perm(id, perm)}) + defaults.update({'create_repo_perm': UserModel().has_perm(id, perm)}) return htmlfill.render( render('admin/users/user_edit.html'), @@ -185,22 +187,23 @@ class UsersController(BaseController): # url('user_perm', id=ID, method='put') grant_perm = request.POST.get('create_repo_perm', False) - + user_model = UserModel() + if grant_perm: perm = Permission.get_by_key('hg.create.none') - UserToPerm.revoke_perm(id, perm) + user_model.revoke_perm(id, perm) perm = Permission.get_by_key('hg.create.repository') - UserToPerm.grant_perm(id, perm) + user_model.grant_perm(id, perm) h.flash(_("Granted 'repository create' permission to user"), category='success') else: perm = Permission.get_by_key('hg.create.repository') - UserToPerm.revoke_perm(id, perm) + user_model.revoke_perm(id, perm) perm = Permission.get_by_key('hg.create.none') - UserToPerm.grant_perm(id, perm) + user_model.grant_perm(id, perm) h.flash(_("Revoked 'repository create' permission to user"), category='success') diff --git a/rhodecode/controllers/admin/users_groups.py b/rhodecode/controllers/admin/users_groups.py --- a/rhodecode/controllers/admin/users_groups.py +++ b/rhodecode/controllers/admin/users_groups.py @@ -33,12 +33,15 @@ from pylons.controllers.util import abor from pylons.i18n.translation import _ from rhodecode.lib.exceptions import UsersGroupsAssignedException -from rhodecode.lib import helpers as h +from rhodecode.lib import helpers as h, safe_unicode from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator from rhodecode.lib.base import BaseController, render +from rhodecode.model.users_group import UsersGroupModel + from rhodecode.model.db import User, UsersGroup, Permission, UsersGroupToPerm -from rhodecode.model.forms import UserForm, UsersGroupForm +from rhodecode.model.forms import UsersGroupForm +from rhodecode.model.meta import Session log = logging.getLogger(__name__) @@ -70,10 +73,12 @@ class UsersGroupsController(BaseControll users_group_form = UsersGroupForm()() try: form_result = users_group_form.to_python(dict(request.POST)) - UsersGroup.create(form_result) + UsersGroupModel().create(name=form_result['users_group_name'], + active=form_result['users_group_active']) h.flash(_('created users group %s') \ % form_result['users_group_name'], category='success') #action_logger(self.rhodecode_user, 'new_user', '', '', self.sa) + Session.commit() except formencode.Invalid, errors: return htmlfill.render( render('admin/users_groups/users_group_add.html'), @@ -108,24 +113,27 @@ class UsersGroupsController(BaseControll c.available_members = [(x.user_id, x.username) for x in self.sa.query(User).all()] + + available_members = [safe_unicode(x[0]) for x in c.available_members] + users_group_form = UsersGroupForm(edit=True, old_data=c.users_group.get_dict(), - available_members=[str(x[0]) for x - in c.available_members])() + available_members=available_members)() try: form_result = users_group_form.to_python(request.POST) - UsersGroup.update(id, form_result) + UsersGroupModel().update(c.users_group, form_result) h.flash(_('updated users group %s') \ % form_result['users_group_name'], category='success') #action_logger(self.rhodecode_user, 'new_user', '', '', self.sa) + Session.commit() except formencode.Invalid, errors: e = errors.error_dict or {} perm = Permission.get_by_key('hg.create.repository') e.update({'create_repo_perm': - UsersGroupToPerm.has_perm(id, perm)}) + UsersGroupModel().has_perm(id, perm)}) return htmlfill.render( render('admin/users_groups/users_group_edit.html'), @@ -150,8 +158,9 @@ class UsersGroupsController(BaseControll # url('users_group', id=ID) try: - UsersGroup.delete(id) + UsersGroupModel().delete(id) h.flash(_('successfully deleted users group'), category='success') + Session.commit() except UsersGroupsAssignedException, e: h.flash(e, category='error') except Exception: @@ -179,7 +188,7 @@ class UsersGroupsController(BaseControll defaults = c.users_group.get_dict() perm = Permission.get_by_key('hg.create.repository') defaults.update({'create_repo_perm': - UsersGroupToPerm.has_perm(id, perm)}) + UsersGroupModel().has_perm(c.users_group, perm)}) return htmlfill.render( render('admin/users_groups/users_group_edit.html'), defaults=defaults, @@ -195,20 +204,21 @@ class UsersGroupsController(BaseControll if grant_perm: perm = Permission.get_by_key('hg.create.none') - UsersGroupToPerm.revoke_perm(id, perm) + UsersGroupModel().revoke_perm(id, perm) perm = Permission.get_by_key('hg.create.repository') - UsersGroupToPerm.grant_perm(id, perm) + UsersGroupModel().grant_perm(id, perm) h.flash(_("Granted 'repository create' permission to user"), category='success') - + + Session.commit() else: perm = Permission.get_by_key('hg.create.repository') - UsersGroupToPerm.revoke_perm(id, perm) + UsersGroupModel().revoke_perm(id, perm) perm = Permission.get_by_key('hg.create.none') - UsersGroupToPerm.grant_perm(id, perm) + UsersGroupModel().grant_perm(id, perm) h.flash(_("Revoked 'repository create' permission to user"), category='success') - + Session.commit() return redirect(url('edit_users_group', id=id)) 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 @@ -120,7 +120,7 @@ class ApiController(JSONRPCController): try: UserModel().create_or_update(username, password, email, firstname, lastname, active, admin, ldap_dn) - Session().commit() + Session.commit() return dict(msg='created new user %s' % username) except Exception: log.error(traceback.format_exc()) @@ -198,7 +198,7 @@ class ApiController(JSONRPCController): try: ug = UsersGroupModel().create(name=name, active=active) - Session().commit() + Session.commit() return dict(id=ug.users_group_id, msg='created new users group %s' % name) except Exception: @@ -226,7 +226,7 @@ class ApiController(JSONRPCController): raise JSONRPCError('unknown user %s' % user_name) ugm = UsersGroupModel().add_user_to_group(users_group, user) - Session().commit() + Session.commit() return dict(id=ugm.users_group_member_id, msg='created new users group member') except Exception: @@ -242,10 +242,9 @@ class ApiController(JSONRPCController): :param repo_name """ - try: - repo = Repository.get_by_repo_name(repo_name) - except NoResultFound: - return None + repo = Repository.get_by_repo_name(repo_name) + if repo is None: + raise JSONRPCError('unknown repository %s' % repo) members = [] for user in repo.repo_to_perm: @@ -334,7 +333,7 @@ class ApiController(JSONRPCController): repo_type=repo_type, repo_group=parent_id, clone_uri=None), owner) - Session().commit() + Session.commit() except Exception: log.error(traceback.format_exc()) raise JSONRPCError('failed to create repository %s' % name) @@ -351,10 +350,9 @@ class ApiController(JSONRPCController): """ try: - try: - repo = Repository.get_by_repo_name(repo_name) - except NoResultFound: - raise JSONRPCError('unknown repository %s' % repo) + repo = Repository.get_by_repo_name(repo_name) + if repo is None: + raise JSONRPCError('unknown repository %s' % repo) try: user = User.get_by_username(user_name) @@ -363,7 +361,7 @@ class ApiController(JSONRPCController): RepositoryPermissionModel()\ .update_or_delete_user_permission(repo, user, perm) - Session().commit() + Session.commit() except Exception: log.error(traceback.format_exc()) raise JSONRPCError('failed to edit permission %(repo)s for %(user)s' diff --git a/rhodecode/controllers/changeset.py b/rhodecode/controllers/changeset.py --- a/rhodecode/controllers/changeset.py +++ b/rhodecode/controllers/changeset.py @@ -280,7 +280,7 @@ class ChangesetController(BaseRepoContro revision=revision, f_path=request.POST.get('f_path'), line_no=request.POST.get('line')) - Session().commit() + Session.commit() return redirect(h.url('changeset_home', repo_name=repo_name, revision=revision)) @@ -290,7 +290,7 @@ class ChangesetController(BaseRepoContro owner = lambda : co.author.user_id == c.rhodecode_user.user_id if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner: ChangesetCommentsModel().delete(comment=co) - Session().commit() + Session.commit() return True else: raise HTTPForbidden() diff --git a/rhodecode/controllers/journal.py b/rhodecode/controllers/journal.py --- a/rhodecode/controllers/journal.py +++ b/rhodecode/controllers/journal.py @@ -134,7 +134,7 @@ class JournalController(BaseController): try: self.scm_model.toggle_following_user(user_id, self.rhodecode_user.user_id) - Session().commit() + Session.commit() return 'ok' except: raise HTTPBadRequest() @@ -144,7 +144,7 @@ class JournalController(BaseController): try: self.scm_model.toggle_following_repo(repo_id, self.rhodecode_user.user_id) - Session().commit() + Session.commit() return 'ok' except: raise HTTPBadRequest() diff --git a/rhodecode/controllers/login.py b/rhodecode/controllers/login.py --- a/rhodecode/controllers/login.py +++ b/rhodecode/controllers/login.py @@ -75,7 +75,8 @@ class LoginController(BaseController): log.info('user %s is now authenticated and stored in ' 'session, session attrs %s' % (username, cs)) user.update_lastlogin() - + Session.commit() + if c.came_from: return redirect(c.came_from) else: @@ -94,7 +95,6 @@ class LoginController(BaseController): @HasPermissionAnyDecorator('hg.admin', 'hg.register.auto_activate', 'hg.register.manual_activate') def register(self): - user_model = UserModel() c.auto_active = False for perm in User.get_by_username('default').user_perms: if perm.permission.permission_name == 'hg.register.auto_activate': @@ -107,10 +107,10 @@ class LoginController(BaseController): try: form_result = register_form.to_python(dict(request.POST)) form_result['active'] = c.auto_active - user_model.create_registration(form_result) + UserModel().create_registration(form_result) h.flash(_('You have successfully registered into rhodecode'), category='success') - Session().commit() + Session.commit() return redirect(url('login_home')) except formencode.Invalid, errors: @@ -124,13 +124,11 @@ class LoginController(BaseController): return render('/register.html') def password_reset(self): - user_model = UserModel() if request.POST: - password_reset_form = PasswordResetForm()() try: form_result = password_reset_form.to_python(dict(request.POST)) - user_model.reset_password_link(form_result) + UserModel().reset_password_link(form_result) h.flash(_('Your password reset link was sent'), category='success') return redirect(url('login_home')) @@ -146,13 +144,11 @@ class LoginController(BaseController): return render('/password_reset.html') def password_reset_confirmation(self): - if request.GET and request.GET.get('key'): try: - user_model = UserModel() user = User.get_by_api_key(request.GET.get('key')) data = dict(email=user.email) - user_model.reset_password(data) + UserModel().reset_password(data) h.flash(_('Your password reset was successful, ' 'new password has been sent to your email'), category='success') diff --git a/rhodecode/controllers/settings.py b/rhodecode/controllers/settings.py --- a/rhodecode/controllers/settings.py +++ b/rhodecode/controllers/settings.py @@ -105,7 +105,7 @@ class SettingsController(BaseRepoControl changed_name = form_result['repo_name_full'] action_logger(self.rhodecode_user, 'user_updated_repo', changed_name, '', self.sa) - Session().commit() + Session.commit() except formencode.Invalid, errors: c.repo_info = repo_model.get_by_repo_name(repo_name) c.users_array = repo_model.get_users_js() @@ -149,7 +149,7 @@ class SettingsController(BaseRepoControl repo_model.delete(repo) invalidate_cache('get_repo_cached_%s' % repo_name) h.flash(_('deleted repository %s') % repo_name, category='success') - Session().commit() + Session.commit() except Exception: log.error(traceback.format_exc()) h.flash(_('An error occurred during deletion of %s') % repo_name, diff --git a/rhodecode/lib/auth.py b/rhodecode/lib/auth.py --- a/rhodecode/lib/auth.py +++ b/rhodecode/lib/auth.py @@ -35,6 +35,7 @@ from pylons.controllers.util import abor from pylons.i18n.translation import _ from rhodecode import __platform__, PLATFORM_WIN, PLATFORM_OTHERS +from rhodecode.model.meta import Session if __platform__ in PLATFORM_WIN: from hashlib import sha256 @@ -225,7 +226,8 @@ def authenticate(username, password): if user_model.create_ldap(username, password, user_dn, user_attrs): log.info('created new ldap user %s', username) - + + Session.commit() return True except (LdapUsernameError, LdapPasswordError,): pass @@ -237,13 +239,12 @@ def authenticate(username, password): def login_container_auth(username): user = User.get_by_username(username) if user is None: - user_model = UserModel() user_attrs = { 'name': username, 'lastname': None, 'email': None, } - user = user_model.create_for_container_auth(username, user_attrs) + user = UserModel().create_for_container_auth(username, user_attrs) if not user: return None log.info('User %s was created by container authentication', username) @@ -252,6 +253,8 @@ def login_container_auth(username): return None user.update_lastlogin() + Session.commit() + log.debug('User %s is now logged in by container authentication', user.username) return user @@ -380,7 +383,7 @@ def set_available_permissions(config): """ log.info('getting information about all available permissions') try: - sa = meta.Session() + sa = meta.Session all_perms = sa.query(Permission).all() except: pass diff --git a/rhodecode/lib/base.py b/rhodecode/lib/base.py --- a/rhodecode/lib/base.py +++ b/rhodecode/lib/base.py @@ -35,7 +35,7 @@ class BaseController(WSGIController): .get_unread_cnt_for_user(c.rhodecode_user.user_id) self.cut_off_limit = int(config.get('cut_off_limit')) - self.sa = meta.Session() + self.sa = meta.Session self.scm_model = ScmModel(self.sa) def __call__(self, environ, start_response): diff --git a/rhodecode/lib/celerylib/tasks.py b/rhodecode/lib/celerylib/tasks.py --- a/rhodecode/lib/celerylib/tasks.py +++ b/rhodecode/lib/celerylib/tasks.py @@ -64,7 +64,7 @@ def get_session(): if CELERY_ON: engine = engine_from_config(config, 'sqlalchemy.db1.') init_model(engine) - sa = meta.Session() + sa = meta.Session return sa def get_logger(cls): @@ -109,7 +109,11 @@ def get_commits_stats(repo_name, ts_min_ co_day_auth_aggr = {} commits_by_day_aggregate = {} - repo = Repository.get_by_repo_name(repo_name).scm_instance + repo = Repository.get_by_repo_name(repo_name) + if repo is None: + return True + + repo = repo.scm_instance repo_size = len(repo.revisions) #return if repo have no revisions if repo_size < 1: 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 @@ -57,7 +57,7 @@ class DbManage(object): def init_db(self): engine = create_engine(self.dburi, echo=self.log_sql) init_model(engine) - self.sa = meta.Session() + self.sa = meta.Session def create_tables(self, override=False): """Create a auth database diff --git a/rhodecode/lib/dbmigrate/schema/db_1_2_0.py b/rhodecode/lib/dbmigrate/schema/db_1_2_0.py --- a/rhodecode/lib/dbmigrate/schema/db_1_2_0.py +++ b/rhodecode/lib/dbmigrate/schema/db_1_2_0.py @@ -1061,7 +1061,7 @@ class CacheInvalidation(Base, BaseModel) """ log.debug('marking %s for invalidation' % key) - inv_obj = Session().query(cls)\ + inv_obj = Session.query(cls)\ .filter(cls.cache_key == key).scalar() if inv_obj: inv_obj.cache_active = False @@ -1083,7 +1083,7 @@ class CacheInvalidation(Base, BaseModel) :param key: """ - inv_obj = Session().query(CacheInvalidation)\ + inv_obj = Session.query(CacheInvalidation)\ .filter(CacheInvalidation.cache_key == key).scalar() inv_obj.cache_active = True Session.add(inv_obj) diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -110,7 +110,7 @@ def action_logger(user, action, repo, ip """ if not sa: - sa = meta.Session() + sa = meta.Session try: if hasattr(user, 'user_id'): @@ -271,7 +271,7 @@ def make_ui(read_from='file', path=None, baseui.setconfig(section, k, v) elif read_from == 'db': - sa = meta.Session() + sa = meta.Session ret = sa.query(RhodeCodeUi)\ .options(FromCache("sql_cache_short", "get_hg_ui_settings")).all() @@ -362,7 +362,7 @@ def map_groups(groups): :param groups: list of groups structure """ - sa = meta.Session() + sa = meta.Session parent = None group = None @@ -392,7 +392,7 @@ def repo2db_mapper(initial_repo_list, re :param remove_obsolete: check for obsolete entries in database """ from rhodecode.model.repo import RepoModel - sa = meta.Session() + sa = meta.Session rm = RepoModel() user = sa.query(User).filter(User.admin == True).first() if user is None: @@ -510,7 +510,7 @@ def create_test_env(repos_test_path, con dbmanage.admin_prompt() dbmanage.create_permissions() dbmanage.populate_default_permissions() - Session().commit() + Session.commit() # PART TWO make test repo log.debug('making test vcs repositories') diff --git a/rhodecode/model/__init__.py b/rhodecode/model/__init__.py --- a/rhodecode/model/__init__.py +++ b/rhodecode/model/__init__.py @@ -71,7 +71,7 @@ class BaseModel(object): if sa is not None: self.sa = sa else: - self.sa = meta.Session() + self.sa = meta.Session def _get_instance(self, cls, instance): """ diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -118,7 +118,7 @@ class BaseModel(object): @classmethod def query(cls): - return Session().query(cls) + return Session.query(cls) @classmethod def get(cls, id_): @@ -132,7 +132,7 @@ class BaseModel(object): @classmethod def delete(cls, id_): obj = cls.query().get(id_) - Session().delete(obj) + Session.delete(obj) class RhodeCodeSetting(Base, BaseModel): @@ -252,8 +252,7 @@ class RhodeCodeUi(Base, BaseModel): new_ui.ui_key = key new_ui.ui_value = val - Session().add(new_ui) - Session().commit() + Session.add(new_ui) class User(Base, BaseModel): @@ -335,10 +334,8 @@ class User(Base, BaseModel): def update_lastlogin(self): """Update user lastlogin""" - self.last_login = datetime.datetime.now() - Session().add(self) - Session().commit() + Session.add(self) log.debug('updated user %s lastlogin', self.username) @@ -386,7 +383,6 @@ class UsersGroup(Base, BaseModel): "get_user_%s" % group_name)) return q.scalar() - @classmethod def get(cls, users_group_id, cache=False): users_group = cls.query() @@ -395,68 +391,6 @@ class UsersGroup(Base, BaseModel): "get_users_group_%s" % users_group_id)) return users_group.get(users_group_id) - @classmethod - def create(cls, form_data): - try: - new_users_group = cls() - for k, v in form_data.items(): - setattr(new_users_group, k, v) - - Session().add(new_users_group) - Session().commit() - return new_users_group - except: - log.error(traceback.format_exc()) - Session().rollback() - raise - - @classmethod - def update(cls, users_group_id, form_data): - - try: - users_group = cls.get(users_group_id, cache=False) - - for k, v in form_data.items(): - if k == 'users_group_members': - users_group.members = [] - Session().flush() - members_list = [] - if v: - v = [v] if isinstance(v, basestring) else v - for u_id in set(v): - member = UsersGroupMember(users_group_id, u_id) - members_list.append(member) - setattr(users_group, 'members', members_list) - setattr(users_group, k, v) - - Session().add(users_group) - Session().commit() - except: - log.error(traceback.format_exc()) - Session().rollback() - raise - - @classmethod - def delete(cls, users_group_id): - try: - - # check if this group is not assigned to repo - assigned_groups = UsersGroupRepoToPerm.query()\ - .filter(UsersGroupRepoToPerm.users_group_id == - users_group_id).all() - - if assigned_groups: - raise UsersGroupsAssignedException('RepoGroup assigned to %s' % - assigned_groups) - - users_group = cls.get(users_group_id, cache=False) - Session().delete(users_group) - Session().commit() - except: - log.error(traceback.format_exc()) - Session().rollback() - raise - class UsersGroupMember(Base, BaseModel): __tablename__ = 'users_groups_members' __table_args__ = {'extend_existing':True} @@ -477,8 +411,8 @@ class UsersGroupMember(Base, BaseModel): ugm = UsersGroupMember() ugm.users_group = group ugm.user = user - Session().add(ugm) - Session().commit() + Session.add(ugm) + Session.commit() return ugm class Repository(Base, BaseModel): @@ -521,11 +455,11 @@ class Repository(Base, BaseModel): @classmethod def get_by_repo_name(cls, repo_name): - q = Session().query(cls).filter(cls.repo_name == repo_name) + q = Session.query(cls).filter(cls.repo_name == repo_name) q = q.options(joinedload(Repository.fork))\ .options(joinedload(Repository.user))\ .options(joinedload(Repository.group)) - return q.one() + return q.scalar() @classmethod def get_repo_forks(cls, repo_id): @@ -538,7 +472,7 @@ class Repository(Base, BaseModel): :param cls: """ - q = Session().query(RhodeCodeUi)\ + q = Session.query(RhodeCodeUi)\ .filter(RhodeCodeUi.ui_key == cls.url_sep()) q = q.options(FromCache("sql_cache_short", "repository_repo_path")) return q.one().ui_value @@ -574,7 +508,7 @@ class Repository(Base, BaseModel): Returns base full path for that repository means where it actually exists on a filesystem """ - q = Session().query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == + q = Session.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == Repository.url_sep()) q = q.options(FromCache("sql_cache_short", "repository_repo_path")) return q.one().ui_value @@ -851,7 +785,7 @@ class Permission(Base, BaseModel): @classmethod def get_default_perms(cls, default_user_id): - q = Session().query(UserRepoToPerm, Repository, cls)\ + q = Session.query(UserRepoToPerm, Repository, cls)\ .join((Repository, UserRepoToPerm.repository_id == Repository.repo_id))\ .join((cls, UserRepoToPerm.permission_id == cls.permission_id))\ .filter(UserRepoToPerm.user_id == default_user_id) @@ -877,7 +811,7 @@ class UserRepoToPerm(Base, BaseModel): n.user = user n.repository = repository n.permission = permission - Session().add(n) + Session.add(n) return n def __repr__(self): @@ -893,41 +827,6 @@ class UserToPerm(Base, BaseModel): user = relationship('User') permission = relationship('Permission', lazy='joined') - @classmethod - def has_perm(cls, user_id, perm): - if not isinstance(perm, Permission): - raise Exception('perm needs to be an instance of Permission class') - - return cls.query().filter(cls.user_id == user_id)\ - .filter(cls.permission == perm).scalar() is not None - - @classmethod - def grant_perm(cls, user_id, perm): - if not isinstance(perm, Permission): - raise Exception('perm needs to be an instance of Permission class') - - new = cls() - new.user_id = user_id - new.permission = perm - try: - Session().add(new) - Session().commit() - except: - Session().rollback() - - - @classmethod - def revoke_perm(cls, user_id, perm): - if not isinstance(perm, Permission): - raise Exception('perm needs to be an instance of Permission class') - - try: - obj = cls.query().filter(cls.user_id == user_id)\ - .filter(cls.permission == perm).one() - Session().delete(obj) - Session().commit() - except: - Session().rollback() class UsersGroupRepoToPerm(Base, BaseModel): __tablename__ = 'users_group_repo_to_perm' @@ -947,7 +846,7 @@ class UsersGroupRepoToPerm(Base, BaseMod n.users_group = users_group n.repository = repository n.permission = permission - Session().add(n) + Session.add(n) return n def __repr__(self): @@ -963,45 +862,6 @@ class UsersGroupToPerm(Base, BaseModel): permission = relationship('Permission') - @classmethod - def has_perm(cls, users_group_id, perm): - if not isinstance(perm, Permission): - raise Exception('perm needs to be an instance of Permission class') - - return cls.query().filter(cls.users_group_id == - users_group_id)\ - .filter(cls.permission == perm)\ - .scalar() is not None - - @classmethod - def grant_perm(cls, users_group_id, perm): - if not isinstance(perm, Permission): - raise Exception('perm needs to be an instance of Permission class') - - new = cls() - new.users_group_id = users_group_id - new.permission = perm - try: - Session().add(new) - Session().commit() - except: - Session().rollback() - - - @classmethod - def revoke_perm(cls, users_group_id, perm): - if not isinstance(perm, Permission): - raise Exception('perm needs to be an instance of Permission class') - - try: - obj = cls.query().filter(cls.users_group_id == users_group_id)\ - .filter(cls.permission == perm).one() - Session().delete(obj) - Session().commit() - except: - Session().rollback() - - class UserRepoGroupToPerm(Base, BaseModel): __tablename__ = 'group_to_perm' __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'extend_existing':True}) @@ -1103,7 +963,7 @@ class CacheInvalidation(Base, BaseModel) """ log.debug('marking %s for invalidation' % key) - inv_obj = Session().query(cls)\ + inv_obj = Session.query(cls)\ .filter(cls.cache_key == key).scalar() if inv_obj: inv_obj.cache_active = False @@ -1112,11 +972,11 @@ class CacheInvalidation(Base, BaseModel) inv_obj = CacheInvalidation(key) try: - Session().add(inv_obj) - Session().commit() + Session.add(inv_obj) + Session.commit() except Exception: log.error(traceback.format_exc()) - Session().rollback() + Session.rollback() @classmethod def set_valid(cls, key): @@ -1128,8 +988,8 @@ class CacheInvalidation(Base, BaseModel) inv_obj = CacheInvalidation.query()\ .filter(CacheInvalidation.cache_key == key).scalar() inv_obj.cache_active = True - Session().add(inv_obj) - Session().commit() + Session.add(inv_obj) + Session.commit() class ChangesetComment(Base, BaseModel): @@ -1157,7 +1017,7 @@ class ChangesetComment(Base, BaseModel): :param cls: :param revision: """ - return Session().query(User)\ + return Session.query(User)\ .filter(cls.revision == revision)\ .join(ChangesetComment.author).all() @@ -1203,7 +1063,7 @@ class Notification(Base, BaseModel): assoc = UserNotification() assoc.notification = notification u.notifications.append(assoc) - Session().add(notification) + Session.add(notification) return notification @property @@ -1226,7 +1086,7 @@ class UserNotification(Base, BaseModel): def mark_as_read(self): self.read = True - Session().add(self) + Session.add(self) class DbMigrateVersion(Base, BaseModel): __tablename__ = 'db_migrate_version' diff --git a/rhodecode/model/forms.py b/rhodecode/model/forms.py --- a/rhodecode/model/forms.py +++ b/rhodecode/model/forms.py @@ -36,9 +36,7 @@ from rhodecode.config.routing import ADM from rhodecode.lib.utils import repo_name_slug from rhodecode.lib.auth import authenticate, get_crypt_password from rhodecode.lib.exceptions import LdapImportError -from rhodecode.model.user import UserModel -from rhodecode.model.repo import RepoModel -from rhodecode.model.db import User, UsersGroup, RepoGroup +from rhodecode.model.db import User, UsersGroup, RepoGroup, Repository from rhodecode import BACKENDS log = logging.getLogger(__name__) @@ -68,7 +66,7 @@ def ValidUsername(edit, old_data): #check if user is unique old_un = None if edit: - old_un = UserModel().get(old_data.get('user_id')).username + old_un = User.get(old_data.get('user_id')).username if old_un != value or not edit: if User.get_by_username(value, case_insensitive=True): @@ -268,7 +266,7 @@ def ValidRepoName(edit, old_data): if rename or create: if group_path != '': - if RepoModel().get_by_repo_name(repo_name_full,): + if Repository.get_by_repo_name(repo_name_full): e_dict = {'repo_name':_('This repository already ' 'exists in a group "%s"') % gr.group_name} @@ -281,7 +279,7 @@ def ValidRepoName(edit, old_data): raise formencode.Invalid('', value, state, error_dict=e_dict) - elif RepoModel().get_by_repo_name(repo_name_full): + elif Repository.get_by_repo_name(repo_name_full): e_dict = {'repo_name':_('This repository ' 'already exists')} raise formencode.Invalid('', value, state, diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -204,11 +204,9 @@ class RepoModel(BaseModel): # rename repository self.__rename_repo(old=repo_name, new=new_name) - self.sa.commit() return cur_repo except: log.error(traceback.format_exc()) - self.sa.rollback() raise def create(self, form_data, cur_user, just_db=False, fork=False): @@ -312,10 +310,8 @@ class RepoModel(BaseModel): try: self.sa.delete(repo) self.__delete_repo(repo) - self.sa.commit() except: log.error(traceback.format_exc()) - self.sa.rollback() raise def delete_perm_user(self, form_data, repo_name): @@ -325,10 +321,8 @@ class RepoModel(BaseModel): == self.get_by_repo_name(repo_name))\ .filter(UserRepoToPerm.user_id == form_data['user_id']).one() self.sa.delete(obj) - self.sa.commit() except: log.error(traceback.format_exc()) - self.sa.rollback() raise def delete_perm_users_group(self, form_data, repo_name): @@ -339,10 +333,8 @@ class RepoModel(BaseModel): .filter(UsersGroupRepoToPerm.users_group_id == form_data['users_group_id']).one() self.sa.delete(obj) - self.sa.commit() except: log.error(traceback.format_exc()) - self.sa.rollback() raise def delete_stats(self, repo_name): @@ -356,10 +348,8 @@ class RepoModel(BaseModel): .filter(Statistics.repository == \ self.get_by_repo_name(repo_name)).one() self.sa.delete(obj) - self.sa.commit() except: log.error(traceback.format_exc()) - self.sa.rollback() raise def __create_repo(self, repo_name, alias, new_parent_id, clone_uri=False): @@ -428,11 +418,11 @@ class RepoModel(BaseModel): """ rm_path = os.path.join(self.repos_path, repo.repo_name) log.info("Removing %s", rm_path) - #disable hg/git + # disable hg/git alias = repo.repo_type shutil.move(os.path.join(rm_path, '.%s' % alias), os.path.join(rm_path, 'rm__.%s' % alias)) - #disable repo + # disable repo shutil.move(rm_path, os.path.join(self.repos_path, 'rm__%s__%s' \ % (datetime.today()\ .strftime('%Y%m%d_%H%M%S_%f'), diff --git a/rhodecode/model/user.py b/rhodecode/model/user.py --- a/rhodecode/model/user.py +++ b/rhodecode/model/user.py @@ -54,6 +54,9 @@ PERM_WEIGHTS = {'repository.none': 0, class UserModel(BaseModel): + def __get_user(self, user): + return self._get_instance(User, user) + def get(self, user_id, cache=False): user = self.sa.query(User) if cache: @@ -84,11 +87,9 @@ class UserModel(BaseModel): new_user.api_key = generate_api_key(form_data['username']) self.sa.add(new_user) - self.sa.commit() return new_user except: log.error(traceback.format_exc()) - self.sa.rollback() raise @@ -159,7 +160,6 @@ class UserModel(BaseModel): new_user.lastname = attrs['lastname'] self.sa.add(new_user) - self.sa.commit() return new_user except (DatabaseError,): log.error(traceback.format_exc()) @@ -200,7 +200,6 @@ class UserModel(BaseModel): new_user.lastname = attrs['lastname'] self.sa.add(new_user) - self.sa.commit() return new_user except (DatabaseError,): log.error(traceback.format_exc()) @@ -258,10 +257,8 @@ class UserModel(BaseModel): setattr(user, k, v) self.sa.add(user) - self.sa.commit() except: log.error(traceback.format_exc()) - self.sa.rollback() raise def update_my_account(self, user_id, form_data): @@ -280,10 +277,8 @@ class UserModel(BaseModel): setattr(user, k, v) self.sa.add(user) - self.sa.commit() except: log.error(traceback.format_exc()) - self.sa.rollback() raise def delete(self, user_id): @@ -300,10 +295,8 @@ class UserModel(BaseModel): 'remove those repositories') \ % user.repositories) self.sa.delete(user) - self.sa.commit() except: log.error(traceback.format_exc()) - self.sa.rollback() raise def reset_password_link(self, data): @@ -477,3 +470,35 @@ class UserModel(BaseModel): return user + + + def has_perm(self, user, perm): + if not isinstance(perm, Permission): + raise Exception('perm needs to be an instance of Permission class') + + user = self.__get_user(user) + + return UserToPerm.query().filter(UserToPerm.user == user.user)\ + .filter(UserToPerm.permission == perm).scalar() is not None + + def grant_perm(self, user, perm): + if not isinstance(perm, Permission): + raise Exception('perm needs to be an instance of Permission class') + + user = self.__get_user(user) + + new = UserToPerm() + new.user = user.user + new.permission = perm + self.sa.add(new) + + + def revoke_perm(self, user, perm): + if not isinstance(perm, Permission): + raise Exception('perm needs to be an instance of Permission class') + + user = self.__get_user(user) + + obj = UserToPerm.query().filter(UserToPerm.user == user.user)\ + .filter(UserToPerm.permission == perm).one() + self.sa.delete(obj) 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 @@ -27,7 +27,9 @@ import logging import traceback from rhodecode.model import BaseModel -from rhodecode.model.db import UsersGroupMember, UsersGroup +from rhodecode.model.db import UsersGroupMember, UsersGroup,\ + UsersGroupRepoToPerm, Permission, UsersGroupToPerm +from rhodecode.lib.exceptions import UsersGroupsAssignedException log = logging.getLogger(__name__) @@ -44,15 +46,55 @@ class UsersGroupModel(BaseModel): return UsersGroup.get_by_group_name(name, cache, case_insensitive) def create(self, name, active=True): - new = UsersGroup() - new.users_group_name = name - new.users_group_active = active - self.sa.add(new) - return new + try: + new = UsersGroup() + new.users_group_name = name + new.users_group_active = active + self.sa.add(new) + return new + except: + log.error(traceback.format_exc()) + raise + + def update(self, users_group, form_data): + + try: + users_group = self.__get_users_group(users_group) + + for k, v in form_data.items(): + if k == 'users_group_members': + users_group.members = [] + self.sa.flush() + members_list = [] + if v: + v = [v] if isinstance(v, basestring) else v + for u_id in set(v): + member = UsersGroupMember(users_group.users_group_id, u_id) + members_list.append(member) + setattr(users_group, 'members', members_list) + setattr(users_group, k, v) + + self.sa.add(users_group) + except: + log.error(traceback.format_exc()) + raise def delete(self, users_group): - obj = self.__get_users_group(users_group) - self.sa.delete(obj) + try: + users_group = self.__get_users_group(users_group) + + # check if this group is not assigned to repo + assigned_groups = UsersGroupRepoToPerm.query()\ + .filter(UsersGroupRepoToPerm.users_group == users_group).all() + + if assigned_groups: + raise UsersGroupsAssignedException('RepoGroup assigned to %s' % + assigned_groups) + + self.sa.delete(users_group) + except: + log.error(traceback.format_exc()) + raise def add_user_to_group(self, users_group, user): for m in users_group.members: @@ -73,3 +115,38 @@ class UsersGroupModel(BaseModel): except: log.error(traceback.format_exc()) raise + + def has_perm(self, users_group, perm): + if not isinstance(perm, Permission): + raise Exception('perm needs to be an instance of Permission class') + + users_group = self.__get_users_group(users_group) + + return UsersGroupToPerm.query()\ + .filter(UsersGroupToPerm.users_group == users_group)\ + .filter(UsersGroupToPerm.permission == perm).scalar() is not None + + def grant_perm(self, users_group, perm): + if not isinstance(perm, Permission): + raise Exception('perm needs to be an instance of Permission class') + + users_group = self.__get_users_group(users_group) + + new = UsersGroupToPerm() + new.users_group = users_group + new.permission = perm + self.sa.add(new) + + + def revoke_perm(self, users_group, perm): + if not isinstance(perm, Permission): + raise Exception('perm needs to be an instance of Permission class') + + users_group = self.__get_users_group(users_group) + + obj = UsersGroupToPerm.query()\ + .filter(UsersGroupToPerm.users_group == users_group)\ + .filter(UsersGroupToPerm.permission == perm).one() + self.sa.delete(obj) + + diff --git a/rhodecode/templates/files/files.html b/rhodecode/templates/files/files.html --- a/rhodecode/templates/files/files.html +++ b/rhodecode/templates/files/files.html @@ -39,7 +39,7 @@