Changeset - 8ecc6b8229a5
[Not reviewed]
beta
0 38 0
Marcin Kuzminski - 14 years ago 2011-12-02 21:31:13
marcin@python-works.com
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
38 files changed with 312 insertions and 339 deletions:
0 comments (0 inline, 0 general)
rhodecode/controllers/admin/permissions.py
Show inline comments
 
@@ -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')
 

	
rhodecode/controllers/admin/repos.py
Show inline comments
 
@@ -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())
rhodecode/controllers/admin/repos_groups.py
Show inline comments
 
@@ -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:
rhodecode/controllers/admin/settings.py
Show inline comments
 
@@ -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)\
rhodecode/controllers/admin/users.py
Show inline comments
 
@@ -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')
 

	
rhodecode/controllers/admin/users_groups.py
Show inline comments
 
@@ -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))
rhodecode/controllers/api/api.py
Show inline comments
 
@@ -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'
rhodecode/controllers/changeset.py
Show inline comments
 
@@ -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()
rhodecode/controllers/journal.py
Show inline comments
 
@@ -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()
rhodecode/controllers/login.py
Show inline comments
 
@@ -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')
rhodecode/controllers/settings.py
Show inline comments
 
@@ -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,
rhodecode/lib/auth.py
Show inline comments
 
@@ -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
rhodecode/lib/base.py
Show inline comments
 
@@ -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):
rhodecode/lib/celerylib/tasks.py
Show inline comments
 
@@ -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:
rhodecode/lib/db_manage.py
Show inline comments
 
@@ -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
rhodecode/lib/dbmigrate/schema/db_1_2_0.py
Show inline comments
 
@@ -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)
rhodecode/lib/utils.py
Show inline comments
 
@@ -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')
 

	
rhodecode/model/__init__.py
Show inline comments
 
@@ -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):
 
        """
rhodecode/model/db.py
Show inline comments
 
@@ -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'
rhodecode/model/forms.py
Show inline comments
 
@@ -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,
rhodecode/model/repo.py
Show inline comments
 
@@ -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'),
rhodecode/model/user.py
Show inline comments
 
@@ -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)
rhodecode/model/users_group.py
Show inline comments
 
@@ -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)
 

	
 

	
rhodecode/templates/files/files.html
Show inline comments
 
@@ -39,7 +39,7 @@
 
<script type="text/javascript">
 
var YPJAX_TITLE = "${c.repo_name} ${_('Files')} - ${c.rhodecode_name}";
 
var current_url = "${h.url.current()}";
 
var node_list_url = '${h.url("files_home",repo_name=c.repo_name,revision=c.changeset.raw_id,f_path=c.file.path)}';
 
var node_list_url = '${h.url("files_home",repo_name=c.repo_name,revision=c.changeset.raw_id,f_path='__FPATH__')}';
 
var url_base = '${h.url("files_nodelist_home",repo_name=c.repo_name,revision=c.changeset.raw_id,f_path=c.file.path)}';
 
var truncated_lbl = "${_('search truncated')}";
 
var nomatch_lbl = "${_('no matching files')}";
rhodecode/tests/_test_concurency.py
Show inline comments
 
@@ -81,7 +81,7 @@ class Command(object):
 
def get_session():
 
    engine = engine_from_config(conf, 'sqlalchemy.db1.')
 
    init_model(engine)
 
    sa = meta.Session()
 
    sa = meta.Session
 
    return sa
 

	
 

	
rhodecode/tests/functional/test_admin_notifications.py
Show inline comments
 
@@ -11,8 +11,8 @@ class TestNotificationsController(TestCo
 
    def tearDown(self):
 
        for n in Notification.query().all():
 
            inst = Notification.get(n.notification_id)
 
            Session().delete(inst)
 
        Session().commit()
 
            Session.delete(inst)
 
        Session.commit()
 

	
 
    def test_index(self):
 
        self.log_user()
 
@@ -30,7 +30,7 @@ class TestNotificationsController(TestCo
 
        NotificationModel().create(created_by=u1, subject=u'test_notification_1',
 
                                   body=u'notification_1',
 
                                   recipients=[cur_user])
 
        Session().commit()
 
        Session.commit()
 
        response = self.app.get(url('notifications'))
 
        self.assertTrue(u'test_notification_1' in response.body)
 

	
 
@@ -68,7 +68,7 @@ class TestNotificationsController(TestCo
 
                                                  subject=u'test',
 
                                                  body=u'hi there',
 
                                                  recipients=[cur_user, u1, u2])
 
        Session().commit()
 
        Session.commit()
 
        u1 = User.get(u1.user_id)
 
        u2 = User.get(u2.user_id)
 

	
rhodecode/tests/functional/test_admin_repos.py
Show inline comments
 
@@ -32,11 +32,10 @@ class TestAdminReposController(TestContr
 
                                                'repo_group':'',
 
                                                'description':description,
 
                                                'private':private})
 

	
 
        self.checkSessionFlash(response, 'created repository %s' % (repo_name))
 

	
 
        #test if the repo was created in the database
 
        new_repo = self.Session().query(Repository).filter(Repository.repo_name ==
 
        new_repo = self.Session.query(Repository).filter(Repository.repo_name ==
 
                                                    repo_name).one()
 

	
 
        self.assertEqual(new_repo.repo_name, repo_name)
 
@@ -73,7 +72,7 @@ class TestAdminReposController(TestContr
 
                               'created repository %s' % (repo_name_unicode))
 

	
 
        #test if the repo was created in the database
 
        new_repo = self.Session().query(Repository).filter(Repository.repo_name ==
 
        new_repo = self.Session.query(Repository).filter(Repository.repo_name ==
 
                                                repo_name_unicode).one()
 

	
 
        self.assertEqual(new_repo.repo_name, repo_name_unicode)
 
@@ -113,7 +112,7 @@ class TestAdminReposController(TestContr
 
        assert '''created repository %s''' % (repo_name) in response.session['flash'][0], 'No flash message about new repo'
 

	
 
        #test if the fork was created in the database
 
        new_repo = self.Session().query(Repository).filter(Repository.repo_name == repo_name).one()
 
        new_repo = self.Session.query(Repository).filter(Repository.repo_name == repo_name).one()
 

	
 
        assert new_repo.repo_name == repo_name, 'wrong name of repo name in db'
 
        assert new_repo.description == description, 'wrong description'
 
@@ -162,7 +161,7 @@ class TestAdminReposController(TestContr
 
                        response.session['flash'][0])
 

	
 
        #test if the repo was created in the database
 
        new_repo = self.Session().query(Repository).filter(Repository.repo_name ==
 
        new_repo = self.Session.query(Repository).filter(Repository.repo_name ==
 
                                                    repo_name).one()
 

	
 
        self.assertEqual(new_repo.repo_name, repo_name)
 
@@ -182,7 +181,7 @@ class TestAdminReposController(TestContr
 
        response.follow()
 

	
 
        #check if repo was deleted from db
 
        deleted_repo = self.Session().query(Repository).filter(Repository.repo_name
 
        deleted_repo = self.Session.query(Repository).filter(Repository.repo_name
 
                                                        == repo_name).scalar()
 

	
 
        self.assertEqual(deleted_repo, None)
rhodecode/tests/functional/test_admin_settings.py
Show inline comments
 
@@ -145,7 +145,7 @@ class TestAdminSettingsController(TestCo
 
        response.follow()
 

	
 
        assert 'Your account was updated successfully' in response.session['flash'][0][1], 'no flash message about success of change'
 
        user = self.Session().query(User).filter(User.username == 'test_admin').one()
 
        user = self.Session.query(User).filter(User.username == 'test_admin').one()
 
        assert user.email == new_email , 'incorrect user email after update got %s vs %s' % (user.email, new_email)
 
        assert user.name == new_name, 'updated field mismatch %s vs %s' % (user.name, new_name)
 
        assert user.lastname == new_lastname, 'updated field mismatch %s vs %s' % (user.lastname, new_lastname)
 
@@ -171,7 +171,7 @@ class TestAdminSettingsController(TestCo
 
        self.checkSessionFlash(response,
 
                               'Your account was updated successfully')
 

	
 
        user = self.Session().query(User).filter(User.username == 'test_admin').one()
 
        user = self.Session.query(User).filter(User.username == 'test_admin').one()
 
        assert user.email == old_email , 'incorrect user email after update got %s vs %s' % (user.email, old_email)
 

	
 
        assert user.email == old_email , 'incorrect user email after update got %s vs %s' % (user.email, old_email)
rhodecode/tests/functional/test_admin_users.py
Show inline comments
 
@@ -32,7 +32,7 @@ class TestAdminUsersController(TestContr
 

	
 
        assert '''created user %s''' % (username) in response.session['flash'][0], 'No flash message about new user'
 

	
 
        new_user = self.Session().query(User).filter(User.username == username).one()
 
        new_user = self.Session.query(User).filter(User.username == username).one()
 

	
 

	
 
        assert new_user.username == username, 'wrong info about username'
 
@@ -66,7 +66,7 @@ class TestAdminUsersController(TestContr
 
        assert """<span class="error-message">An email address must contain a single @</span>""" in response.body
 

	
 
        def get_user():
 
            self.Session().query(User).filter(User.username == username).one()
 
            self.Session.query(User).filter(User.username == username).one()
 

	
 
        self.assertRaises(NoResultFound, get_user), 'found user in database'
 

	
 
@@ -100,7 +100,7 @@ class TestAdminUsersController(TestContr
 

	
 
        response = response.follow()
 

	
 
        new_user = self.Session().query(User).filter(User.username == username).one()
 
        new_user = self.Session.query(User).filter(User.username == username).one()
 
        response = self.app.delete(url('user', id=new_user.user_id))
 

	
 
        assert """successfully deleted user""" in response.session['flash'][0], 'No info about user deletion'
rhodecode/tests/functional/test_admin_users_groups.py
Show inline comments
 
@@ -23,10 +23,6 @@ class TestAdminUsersGroupsController(Tes
 
        self.checkSessionFlash(response,
 
                               'created users group %s' % TEST_USERS_GROUP)
 

	
 

	
 

	
 

	
 

	
 
    def test_new(self):
 
        response = self.app.get(url('new_users_group'))
 

	
 
@@ -52,13 +48,13 @@ class TestAdminUsersGroupsController(Tes
 
                               'created users group %s' % users_group_name)
 

	
 

	
 
        gr = self.Session().query(UsersGroup)\
 
        gr = self.Session.query(UsersGroup)\
 
                           .filter(UsersGroup.users_group_name ==
 
                                   users_group_name).one()
 

	
 
        response = self.app.delete(url('users_group', id=gr.users_group_id))
 

	
 
        gr = self.Session().query(UsersGroup)\
 
        gr = self.Session.query(UsersGroup)\
 
                           .filter(UsersGroup.users_group_name ==
 
                                   users_group_name).scalar()
 

	
rhodecode/tests/functional/test_changeset_comments.py
Show inline comments
 
@@ -6,21 +6,21 @@ class TestChangeSetCommentrController(Te
 

	
 
    def setUp(self):
 
        for x in ChangesetComment.query().all():
 
            self.Session().delete(x)
 
        self.Session().commit()
 
            self.Session.delete(x)
 
        self.Session.commit()
 

	
 
        for x in Notification.query().all():
 
            self.Session().delete(x)
 
        self.Session().commit()
 
            self.Session.delete(x)
 
        self.Session.commit()
 

	
 
    def tearDown(self):
 
        for x in ChangesetComment.query().all():
 
            self.Session().delete(x)
 
        self.Session().commit()
 
            self.Session.delete(x)
 
        self.Session.commit()
 

	
 
        for x in Notification.query().all():
 
            self.Session().delete(x)
 
        self.Session().commit()
 
            self.Session.delete(x)
 
        self.Session.commit()
 

	
 
    def test_create(self):
 
        self.log_user()
rhodecode/tests/functional/test_files.py
Show inline comments
 
@@ -311,3 +311,16 @@ removed extra unicode conversion in diff
 
                                    f_path=f_path))
 

	
 
        assert "There is no file nor directory at the given path: %r at revision %r" % (f_path, rev[:12]) in response.session['flash'][0][1], 'No flash message'
 

	
 
    def test_ajaxed_files_list(self):
 
        self.log_user()
 
        rev = '27cd5cce30c96924232dffcd24178a07ffeb5dfc' 
 
        response = self.app.get(url('files_nodelist_home',repo_name=HG_REPO,
 
                                    f_path='/',
 
                                    revision=rev),
 
                                extra_environ={'HTTP_X_PARTIAL_XHR':'1'},
 
                                )
 
        self.assertTrue("vcs/web/simplevcs/views/repository.py" in response.body)
 

	
 

	
 

	
rhodecode/tests/functional/test_forks.py
Show inline comments
 
@@ -65,7 +65,7 @@ class TestForksController(TestController
 
                      % (repo_name, fork_name) in response.session['flash'][0])
 

	
 
        #test if the fork was created in the database
 
        fork_repo = self.Session().query(Repository)\
 
        fork_repo = self.Session.query(Repository)\
 
            .filter(Repository.repo_name == fork_name).one()
 

	
 
        self.assertEqual(fork_repo.repo_name, fork_name)
rhodecode/tests/functional/test_journal.py
Show inline comments
 
@@ -16,10 +16,10 @@ class TestJournalController(TestControll
 

	
 
    def test_stop_following_repository(self):
 
        session = self.log_user()
 
#        usr = self.Session().query(User).filter(User.username == 'test_admin').one()
 
#        repo = self.Session().query(Repository).filter(Repository.repo_name == HG_REPO).one()
 
#        usr = self.Session.query(User).filter(User.username == 'test_admin').one()
 
#        repo = self.Session.query(Repository).filter(Repository.repo_name == HG_REPO).one()
 
#
 
#        followings = self.Session().query(UserFollowing)\
 
#        followings = self.Session.query(UserFollowing)\
 
#            .filter(UserFollowing.user == usr)\
 
#            .filter(UserFollowing.follows_repository == repo).all()
 
#
rhodecode/tests/functional/test_login.py
Show inline comments
 
@@ -9,9 +9,9 @@ class TestLoginController(TestController
 

	
 
    def tearDown(self):
 
        for n in Notification.query().all():
 
            Session().delete(n)
 
            Session.delete(n)
 

	
 
        Session().commit()
 
        Session.commit()
 
        self.assertEqual(Notification.query().all(), [])
 

	
 
    def test_index(self):
 
@@ -199,7 +199,7 @@ class TestLoginController(TestController
 
        self.assertEqual(response.status , '302 Found')
 
        assert 'You have successfully registered into rhodecode' in response.session['flash'][0], 'No flash message about user registration'
 

	
 
        ret = self.Session().query(User).filter(User.username == 'test_regular4').one()
 
        ret = self.Session.query(User).filter(User.username == 'test_regular4').one()
 
        assert ret.username == username , 'field mismatch %s %s' % (ret.username, username)
 
        assert check_password(password, ret.password) == True , 'password mismatch'
 
        assert ret.email == email , 'field mismatch %s %s' % (ret.email, email)
 
@@ -231,8 +231,8 @@ class TestLoginController(TestController
 
        new.name = name
 
        new.lastname = lastname
 
        new.api_key = generate_api_key(username)
 
        self.Session().add(new)
 
        self.Session().commit()
 
        self.Session.add(new)
 
        self.Session.commit()
 

	
 
        response = self.app.post(url(controller='login',
 
                                     action='password_reset'),
rhodecode/tests/functional/test_summary.py
Show inline comments
 
@@ -37,11 +37,11 @@ class TestSummaryController(TestControll
 
                        in response.body)
 

	
 
        # clone url...
 
        self.assertTrue("""<input type="text" id="clone_url" readonly="readonly" value="hg clone http://test_admin@localhost:80/%s" size="70"/>""" % HG_REPO in response.body)
 
        self.assertTrue("""<input type="text" id="clone_url" readonly="readonly" value="http://test_admin@localhost:80/%s" size="70"/>""" % HG_REPO in response.body)
 

	
 

	
 
    def _enable_stats(self):
 
        r = Repository.get_by_repo_name(HG_REPO)
 
        r.enable_statistics = True
 
        self.Session().add(r)
 
        self.Session().commit()
 
        self.Session.add(r)
 
        self.Session.commit()
rhodecode/tests/test_models.py
Show inline comments
 
@@ -66,7 +66,7 @@ class TestReposGroups(unittest.TestCase)
 

	
 
    def test_create_same_name_group(self):
 
        self.assertRaises(IntegrityError, lambda:self.__make_group('newGroup'))
 
        Session().rollback()
 
        Session.rollback()
 

	
 
    def test_same_subgroup(self):
 
        sg1 = self.__make_group('sub1', parent_id=self.g1.group_id)
 
@@ -163,20 +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()
 
        Session.commit()
 
        self.assertEqual(User.get_by_username(u'test_user'), usr)
 

	
 
        # make users group
 
        users_group = UsersGroupModel().create('some_example_group')
 
        Session().commit()
 
        Session.commit()
 

	
 
        UsersGroupModel().add_user_to_group(users_group, usr)
 
        Session().commit()
 
        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()
 
        Session.commit()
 

	
 
        self.assertEqual(UsersGroupMember.query().all(), [])
 

	
 
@@ -209,9 +209,9 @@ class TestNotifications(unittest.TestCas
 

	
 
    def _clean_notifications(self):
 
        for n in Notification.query().all():
 
            Session().delete(n)
 
            Session.delete(n)
 

	
 
        Session().commit()
 
        Session.commit()
 
        self.assertEqual(Notification.query().all(), [])
 

	
 

	
 
@@ -223,7 +223,7 @@ class TestNotifications(unittest.TestCas
 
        notification = NotificationModel().create(created_by=self.u1,
 
                                           subject=u'subj', body=u'hi there',
 
                                           recipients=usrs)
 
        Session().commit()
 
        Session.commit()
 
        u1 = User.get(self.u1)
 
        u2 = User.get(self.u2)
 
        u3 = User.get(self.u3)
 
@@ -248,12 +248,12 @@ class TestNotifications(unittest.TestCas
 
        notification1 = NotificationModel().create(created_by=self.u1,
 
                                            subject=u'subj', body=u'hi there1',
 
                                            recipients=[self.u3])
 
        Session().commit()
 
        Session.commit()
 
        notification2 = NotificationModel().create(created_by=self.u1,
 
                                            subject=u'subj', body=u'hi there2',
 
                                            recipients=[self.u3])
 
        Session().commit()
 
        u3 = Session().query(User).get(self.u3)
 
        Session.commit()
 
        u3 = Session.query(User).get(self.u3)
 

	
 
        self.assertEqual(sorted([x.notification for x in u3.notifications]),
 
                         sorted([notification2, notification1]))
 
@@ -266,12 +266,12 @@ class TestNotifications(unittest.TestCas
 
        notification = NotificationModel().create(created_by=self.u1,
 
                                           subject=u'title', body=u'hi there3',
 
                                    recipients=[self.u3, self.u1, self.u2])
 
        Session().commit()
 
        Session.commit()
 
        notifications = Notification.query().all()
 
        self.assertTrue(notification in notifications)
 

	
 
        Notification.delete(notification.notification_id)
 
        Session().commit()
 
        Session.commit()
 

	
 
        notifications = Notification.query().all()
 
        self.assertFalse(notification in notifications)
 
@@ -290,7 +290,7 @@ class TestNotifications(unittest.TestCas
 
        notification = NotificationModel().create(created_by=self.u1,
 
                                           subject=u'title', body=u'hi there3',
 
                                    recipients=[self.u3, self.u1, self.u2])
 
        Session().commit()
 
        Session.commit()
 

	
 
        unotification = UserNotification.query()\
 
                            .filter(UserNotification.notification ==
 
@@ -302,7 +302,7 @@ class TestNotifications(unittest.TestCas
 

	
 
        NotificationModel().delete(self.u3,
 
                                   notification.notification_id)
 
        Session().commit()
 
        Session.commit()
 

	
 
        u3notification = UserNotification.query()\
 
                            .filter(UserNotification.notification ==
 
@@ -339,7 +339,7 @@ class TestNotifications(unittest.TestCas
 
        NotificationModel().create(created_by=self.u1,
 
                            subject=u'title', body=u'hi there_delete',
 
                            recipients=[self.u3, self.u1])
 
        Session().commit()
 
        Session.commit()
 

	
 
        self.assertEqual(NotificationModel()
 
                         .get_unread_cnt_for_user(self.u1), 1)
 
@@ -351,7 +351,7 @@ class TestNotifications(unittest.TestCas
 
        notification = NotificationModel().create(created_by=self.u1,
 
                                           subject=u'title', body=u'hi there3',
 
                                    recipients=[self.u3, self.u1, self.u2])
 
        Session().commit()
 
        Session.commit()
 

	
 
        self.assertEqual(NotificationModel()
 
                         .get_unread_cnt_for_user(self.u1), 2)
rhodecode/websetup.py
Show inline comments
 
@@ -46,5 +46,5 @@ def setup_app(command, conf, vars):
 
    dbmanage.admin_prompt()
 
    dbmanage.create_permissions()
 
    dbmanage.populate_default_permissions()
 
    Session().commit()
 
    Session.commit()
 
    load_environment(conf.global_conf, conf.local_conf, initial=True)
0 comments (0 inline, 0 general)