Changeset - 28db71c6349e
[Not reviewed]
default
0 2 0
Mads Kiilerich - 10 years ago 2015-07-23 00:52:29
madski@unity3d.com
scm: introduce AvailableRepoGroupChoices

Extract reusable code to reduce code duplication.

Note: groups_choices from the db module is only used from this function and
should perhaps also be extracted ... but it is also closely related to other
stuff that is (mis)placed in the db module so perhaps not ...
2 files changed with 21 insertions and 13 deletions:
0 comments (0 inline, 0 general)
kallithea/controllers/admin/repos.py
Show inline comments
 
@@ -43,13 +43,13 @@ from kallithea.lib.base import BaseRepoC
 
from kallithea.lib.utils import action_logger, repo_name_slug, jsonify
 
from kallithea.lib.vcs import RepositoryError
 
from kallithea.model.meta import Session
 
from kallithea.model.db import User, Repository, UserFollowing, RepoGroup,\
 
    Setting, RepositoryField
 
from kallithea.model.forms import RepoForm, RepoFieldForm, RepoPermsForm
 
from kallithea.model.scm import ScmModel, RepoGroupList, RepoList
 
from kallithea.model.scm import ScmModel, AvailableRepoGroupChoices, RepoList
 
from kallithea.model.repo import RepoModel
 
from kallithea.lib.compat import json
 
from kallithea.lib.exceptions import AttachedForksError
 
from kallithea.lib.utils2 import safe_int
 

	
 
log = logging.getLogger(__name__)
 
@@ -79,23 +79,13 @@ class ReposController(BaseRepoController
 
        top_perms = ['hg.create.repository']
 
        repo_group_perms = ['group.admin']
 
        if HasPermissionAny('hg.create.write_on_repogroup.true')():
 
            repo_group_perms.append('group.write')
 
        extras = [] if repo is None else [repo.group]
 

	
 
        groups = RepoGroup.query().all()
 
        if HasPermissionAll('hg.admin')('available repo groups'):
 
            groups.append(None)
 
        else:
 
            groups = list(RepoGroupList(groups, perm_set=repo_group_perms))
 
            if top_perms and HasPermissionAny(*top_perms)('available repo groups'):
 
                groups.append(None)
 
            for extra in extras:
 
                if not any(rg == extra for rg in groups):
 
                    groups.append(extra)
 
        c.repo_groups = RepoGroup.groups_choices(groups=groups, show_empty_group=False)
 
        c.repo_groups = AvailableRepoGroupChoices(top_perms, repo_group_perms, extras)
 
        c.repo_groups_choices = [rg[0] for rg in c.repo_groups]
 

	
 
        c.landing_revs_choices, c.landing_revs = ScmModel().get_repo_landing_revs(repo)
 

	
 
    def __load_data(self, repo_name=None):
 
        """
kallithea/model/scm.py
Show inline comments
 
@@ -47,13 +47,13 @@ from kallithea.lib.vcs.backends.base imp
 

	
 
from kallithea import BACKENDS
 
from kallithea.lib import helpers as h
 
from kallithea.lib.utils2 import safe_str, safe_unicode, get_server_url,\
 
    _set_extras
 
from kallithea.lib.auth import HasRepoPermissionAny, HasRepoGroupPermissionAny,\
 
    HasUserGroupPermissionAny
 
    HasUserGroupPermissionAny, HasPermissionAny, HasPermissionAll
 
from kallithea.lib.utils import get_filesystem_repos, make_ui, \
 
    action_logger
 
from kallithea.model import BaseModel
 
from kallithea.model.db import Repository, Ui, CacheInvalidation, \
 
    UserFollowing, UserLog, User, RepoGroup, PullRequest
 
from kallithea.lib.hooks import log_push_action
 
@@ -881,6 +881,24 @@ class ScmModel(BaseModel):
 
                        f.write(tmpl)
 
                    os.chmod(_hook_file, 0755)
 
                except IOError, e:
 
                    log.error('error writing %s: %s' % (_hook_file, e))
 
            else:
 
                log.debug('skipping writing hook file')
 

	
 
def AvailableRepoGroupChoices(top_perms, repo_group_perms, extras=()):
 
    """Return group_id,string tuples with choices for all the repo groups where
 
    the user has the necessary permissions.
 

	
 
    Top level is -1.
 
    """
 
    groups = RepoGroup.query().all()
 
    if HasPermissionAll('hg.admin')('available repo groups'):
 
        groups.append(None)
 
    else:
 
        groups = list(RepoGroupList(groups, perm_set=repo_group_perms))
 
        if top_perms and HasPermissionAny(*top_perms)('available repo groups'):
 
            groups.append(None)
 
        for extra in extras:
 
            if not any(rg == extra for rg in groups):
 
                groups.append(extra)
 
    return RepoGroup.groups_choices(groups=groups, show_empty_group=False)
0 comments (0 inline, 0 general)