Changeset - 51b70569c330
[Not reviewed]
beta
0 6 0
Marcin Kuzminski - 15 years ago 2011-02-13 01:38:39
marcin@python-works.com
extended admin rescan to show what repositories was added and what removed
fixed found bugs, added expunge to cached getter since it introduce some more problems.
Added some relationship to properly clean related objects from database.
6 files changed with 19 insertions and 10 deletions:
0 comments (0 inline, 0 general)
rhodecode/controllers/admin/repos.py
Show inline comments
 
@@ -144,26 +144,26 @@ class ReposController(BaseController):
 
            changed_name = form_result['repo_name']
 
            action_logger(self.rhodecode_user, 'admin_updated_repo',
 
                              changed_name, '', self.sa)
 

	
 
        except formencode.Invalid, errors:
 
            c.repo_info = repo_model.get_by_repo_name(repo_name)
 

	
 
            if c.repo_info.stats:
 
                last_rev = c.repo_info.stats.stat_on_revision
 
            else:
 
                last_rev = 0
 
            c.stats_revision = last_rev
 
            r = ScmModel().get(repo_name)
 
            c.repo_last_rev = r.revisions[-1] if r.revisions else 0
 
            repo, dbrepo = ScmModel().get(repo_name, retval='repo')
 
            c.repo_last_rev = repo.revisions[-1] if repo.revisions else 0
 

	
 
            if last_rev == 0:
 
                c.stats_percentage = 0
 
            else:
 
                c.stats_percentage = '%.2f' % ((float((last_rev)) /
 
                                                c.repo_last_rev) * 100)
 

	
 
            c.users_array = repo_model.get_users_js()
 
            c.users_groups_array = repo_model.get_users_groups_js()
 

	
 
            errors.value.update({'user':c.repo_info.user.username})
 
            return htmlfill.render(
 
@@ -273,45 +273,45 @@ class ReposController(BaseController):
 
                    category='error')
 
        return redirect(url('edit_repo', repo_name=repo_name))
 

	
 
    @HasPermissionAllDecorator('hg.admin')
 
    def show(self, repo_name, format='html'):
 
        """GET /repos/repo_name: Show a specific item"""
 
        # url('repo', repo_name=ID)
 

	
 
    @HasPermissionAllDecorator('hg.admin')
 
    def edit(self, repo_name, format='html'):
 
        """GET /repos/repo_name/edit: Form to edit an existing item"""
 
        # url('edit_repo', repo_name=ID)
 
        r = ScmModel().get(repo_name)[0]
 
        repo, dbrepo = ScmModel().get(repo_name, retval='repo')
 

	
 
        repo_model = RepoModel()
 
        c.repo_info = repo_model.get_by_repo_name(repo_name)
 

	
 
        if c.repo_info is None:
 
            h.flash(_('%s repository is not mapped to db perhaps'
 
                      ' it was created or renamed from the filesystem'
 
                      ' please run the application again'
 
                      ' in order to rescan repositories') % repo_name,
 
                      category='error')
 

	
 
            return redirect(url('repos'))
 

	
 
        if c.repo_info.stats:
 
            last_rev = c.repo_info.stats.stat_on_revision
 
        else:
 
            last_rev = 0
 
        c.stats_revision = last_rev
 

	
 
        c.repo_last_rev = r.revisions[-1] if r.revisions else 0
 
        c.repo_last_rev = repo.revisions[-1] if repo.revisions else 0
 

	
 
        if last_rev == 0 or c.repo_last_rev == 0:
 
            c.stats_percentage = 0
 
        else:
 
            c.stats_percentage = '%.2f' % ((float((last_rev)) /
 
                                            c.repo_last_rev) * 100)
 

	
 
        c.users_array = repo_model.get_users_js()
 
        c.users_groups_array = repo_model.get_users_groups_js()
 

	
 
        defaults = c.repo_info.get_dict()
 

	
rhodecode/controllers/admin/settings.py
Show inline comments
 
@@ -96,30 +96,33 @@ class SettingsController(BaseController)
 
    def update(self, setting_id):
 
        """PUT /admin/settings/setting_id: Update an existing item"""
 
        # Forms posted to this method should contain a hidden field:
 
        #    <input type="hidden" name="_method" value="PUT" />
 
        # Or using helpers:
 
        #    h.form(url('admin_setting', setting_id=ID),
 
        #           method='put')
 
        # url('admin_setting', setting_id=ID)
 
        if setting_id == 'mapping':
 
            rm_obsolete = request.POST.get('destroy', False)
 
            log.debug('Rescanning directories with destroy=%s', rm_obsolete)
 
            initial = ScmModel().repo_scan()
 
            log.debug('invalidating all repositories')
 
            for repo_name in initial.keys():
 
                invalidate_cache('get_repo_cached_%s' % repo_name)
 

	
 
            repo2db_mapper(initial, rm_obsolete)
 
            added, removed = repo2db_mapper(initial, rm_obsolete)
 

	
 
            h.flash(_('Repositories successfully rescanned'), category='success')
 
            h.flash(_('Repositories successfully'
 
                      ' rescanned added: %s,removed: %s') % (added, removed)
 
                      , category='success')
 

	
 
        if setting_id == 'whoosh':
 
            repo_location = self.get_hg_ui_settings()['paths_root_path']
 
            full_index = request.POST.get('full_index', False)
 
            task = run_task(tasks.whoosh_index, repo_location, full_index)
 

	
 
            h.flash(_('Whoosh reindex task scheduled'), category='success')
 
        if setting_id == 'global':
 

	
 
            application_form = ApplicationSettingsForm()()
 
            try:
 
                form_result = application_form.to_python(dict(request.POST))
rhodecode/lib/utils.py
Show inline comments
 
@@ -354,48 +354,51 @@ def map_groups(groups):
 
def repo2db_mapper(initial_repo_list, remove_obsolete=False):
 
    """maps all repos given in initial_repo_list, non existing repositories
 
    are created, if remove_obsolete is True it also check for db entries
 
    that are not in initial_repo_list and removes them.
 
    
 
    :param initial_repo_list: list of repositories found by scanning methods
 
    :param remove_obsolete: check for obsolete entries in database
 
    """
 

	
 
    sa = meta.Session()
 
    rm = RepoModel()
 
    user = sa.query(User).filter(User.admin == True).first()
 

	
 
    added = []
 
    for name, repo in initial_repo_list.items():
 
        group = map_groups(name.split('/'))
 
        if not rm.get_by_repo_name(name, cache=False):
 
            log.info('repository %s not found creating default', name)
 

	
 
            added.append(name)
 
            form_data = {
 
                         'repo_name':name,
 
                         'repo_type':repo.alias,
 
                         'description':repo.description \
 
                            if repo.description != 'unknown' else \
 
                                        '%s repository' % name,
 
                         'private':False,
 
                         'group_id':getattr(group, 'group_id', None)
 
                         }
 
            rm.create(form_data, user, just_db=True)
 

	
 
    removed = []
 
    if remove_obsolete:
 
        #remove from database those repositories that are not in the filesystem
 
        for repo in sa.query(Repository).all():
 
            if repo.repo_name not in initial_repo_list.keys():
 
                removed.append(repo.repo_name)
 
                sa.delete(repo)
 
                sa.commit()
 

	
 
    return added, removed
 
class OrderedDict(dict, DictMixin):
 

	
 
    def __init__(self, *args, **kwds):
 
        if len(args) > 1:
 
            raise TypeError('expected at most 1 arguments, got %d' % len(args))
 
        try:
 
            self.__end
 
        except AttributeError:
 
            self.clear()
 
        self.update(*args, **kwds)
 

	
 
    def clear(self):
rhodecode/model/db.py
Show inline comments
 
@@ -105,24 +105,25 @@ class User(Base, BaseModel):
 
    name = Column("name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 
    lastname = Column("lastname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 
    email = Column("email", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 
    last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None)
 
    ldap_dn = Column("ldap_dn", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 

	
 
    user_log = relationship('UserLog', cascade='all')
 
    user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all')
 

	
 
    repositories = relationship('Repository')
 
    user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
 

	
 
    group_member = relationship('UsersGroupMember', cascade='all')
 
    @property
 
    def full_contact(self):
 
        return '%s %s <%s>' % (self.name, self.lastname, self.email)
 

	
 

	
 
    @property
 
    def is_admin(self):
 
        return self.admin
 

	
 
    def __repr__(self):
 
        return "<%s('id:%s:%s')>" % (self.__class__.__name__,
 
                                     self.user_id, self.username)
rhodecode/model/repo.py
Show inline comments
 
@@ -81,25 +81,27 @@ class RepoModel(BaseModel):
 
            .options(joinedload(Repository.user))\
 
            .options(joinedload(Repository.followers))\
 
            .options(joinedload(Repository.repo_to_perm))\
 
            .options(joinedload(Repository.users_group_to_perm))\
 
            .filter(Repository.repo_name == repo_name)\
 

	
 
        if cache:
 
            repo = repo.options(FromCache("sql_cache_long",
 
                                          "get_repo_full_%s" % repo_name))
 
        if invalidate and cache:
 
            repo.invalidate()
 

	
 
        return repo.scalar()
 
        ret = repo.scalar()
 
        self.sa.expunge_all()
 
        return ret
 

	
 

	
 
    def get_users_js(self):
 

	
 
        users = self.sa.query(User).filter(User.active == True).all()
 
        u_tmpl = '''{id:%s, fname:"%s", lname:"%s", nname:"%s"},'''
 
        users_array = '[%s]' % '\n'.join([u_tmpl % (u.user_id, u.name,
 
                                                    u.lastname, u.username)
 
                                        for u in users])
 
        return users_array
 

	
 

	
rhodecode/model/scm.py
Show inline comments
 
@@ -208,25 +208,25 @@ class ScmModel(BaseModel):
 

	
 
        pre_invalidate = True
 
        dbinvalidate = False
 

	
 
        if invalidation_list is not None:
 
            pre_invalidate = repo_name in invalidation_list
 

	
 
        if pre_invalidate:
 
            #this returns object to invalidate
 
            invalidate = self._should_invalidate(repo_name)
 
            if invalidate:
 
                log.info('invalidating cache for repository %s', repo_name)
 
                #region_invalidate(_get_repo, None, repo_name)
 
                region_invalidate(_get_repo, None, repo_name)
 
                self._mark_invalidated(invalidate)
 
                dbinvalidate = True
 

	
 
        r, dbr = None, None
 
        if retval == 'repo' or 'all':
 
            r = _get_repo(repo_name)
 
        if retval == 'dbrepo' or 'all':
 
            dbr = RepoModel(self.sa).get_full(repo_name, cache=True,
 
                                          invalidate=dbinvalidate)
 

	
 

	
 
        return r, dbr
0 comments (0 inline, 0 general)