# HG changeset patch # User Mads Kiilerich # Date 2015-07-23 00:52:29 # Node ID 28db71c6349e92505209a46f246097f35f1ff5cb # Parent cb362e3439dd8724650100e4e78c1e3e78fc38e6 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 ... diff --git a/kallithea/controllers/admin/repos.py b/kallithea/controllers/admin/repos.py --- a/kallithea/controllers/admin/repos.py +++ b/kallithea/controllers/admin/repos.py @@ -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) diff --git a/kallithea/model/scm.py b/kallithea/model/scm.py --- a/kallithea/model/scm.py +++ b/kallithea/model/scm.py @@ -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)