changeset 5289:28db71c6349e

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 ...
author Mads Kiilerich <madski@unity3d.com>
date Thu, 23 Jul 2015 00:52:29 +0200
parents cb362e3439dd
children b52b0496b9f6
files kallithea/controllers/admin/repos.py kallithea/model/scm.py
diffstat 2 files changed, 21 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/admin/repos.py	Thu Jul 23 00:52:29 2015 +0200
+++ b/kallithea/controllers/admin/repos.py	Thu Jul 23 00:52:29 2015 +0200
@@ -46,7 +46,7 @@
 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 @@
             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)
--- a/kallithea/model/scm.py	Thu Jul 23 00:52:29 2015 +0200
+++ b/kallithea/model/scm.py	Thu Jul 23 00:52:29 2015 +0200
@@ -50,7 +50,7 @@
 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 @@
                     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)