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
 
@@ -46,7 +46,7 @@ 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
 
@@ -82,17 +82,7 @@ class ReposController(BaseRepoController
 
            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)
kallithea/model/scm.py
Show inline comments
 
@@ -50,7 +50,7 @@ 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
 
@@ -884,3 +884,21 @@ class ScmModel(BaseModel):
 
                    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)