diff --git a/kallithea/controllers/admin/repo_groups.py b/kallithea/controllers/admin/repo_groups.py --- a/kallithea/controllers/admin/repo_groups.py +++ b/kallithea/controllers/admin/repo_groups.py @@ -309,10 +309,7 @@ class RepoGroupsController(BaseControlle .filter(RepoGroup.group_parent_id == c.group.group_id).all() c.groups = self.scm_model.get_repo_groups(groups) - c.repos_list = Repository.query() \ - .filter(Repository.group_id == c.group.group_id) \ - .order_by(func.lower(Repository.repo_name)) \ - .all() + c.repos_list = Repository.query(sorted=True).filter_by(group=c.group).all() repos_data = RepoModel().get_repos_as_dict(repos_list=c.repos_list, admin=False, short_name=True) 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 @@ -98,9 +98,7 @@ class ReposController(BaseRepoController return defaults def index(self, format='html'): - _list = Repository.query() \ - .order_by(func.lower(Repository.repo_name)) \ - .all() + _list = Repository.query(sorted=True).all() c.repos_list = RepoList(_list, perm_set=['repository.admin']) repos_data = RepoModel().get_repos_as_dict(repos_list=c.repos_list, diff --git a/kallithea/controllers/home.py b/kallithea/controllers/home.py --- a/kallithea/controllers/home.py +++ b/kallithea/controllers/home.py @@ -57,10 +57,7 @@ class HomeController(BaseController): c.groups = self.scm_model.get_repo_groups() c.group = None - c.repos_list = Repository.query() \ - .filter(Repository.group_id == None) \ - .order_by(func.lower(Repository.repo_name)) \ - .all() + c.repos_list = Repository.query(sorted=True).filter_by(group=None).all() repos_data = RepoModel().get_repos_as_dict(repos_list=c.repos_list, admin=False, short_name=True) diff --git a/kallithea/controllers/journal.py b/kallithea/controllers/journal.py --- a/kallithea/controllers/journal.py +++ b/kallithea/controllers/journal.py @@ -211,10 +211,8 @@ class JournalController(BaseController): if request.environ.get('HTTP_X_PARTIAL_XHR'): return render('journal/journal_data.html') - repos_list = Session().query(Repository) \ - .filter(Repository.user_id == - self.authuser.user_id) \ - .order_by(func.lower(Repository.repo_name)).all() + repos_list = Repository.query(sorted=True) \ + .filter_by(user_id=self.authuser.user_id).all() repos_data = RepoModel().get_repos_as_dict(repos_list=repos_list, admin=True) diff --git a/kallithea/model/db.py b/kallithea/model/db.py --- a/kallithea/model/db.py +++ b/kallithea/model/db.py @@ -1090,6 +1090,19 @@ class Repository(Base, BaseModel): log.error(traceback.format_exc()) @classmethod + def query(cls, sorted=False): + """Add Repository-specific helpers for common query constructs. + + sorted: if True, apply the default ordering (name, case insensitive). + """ + q = super(Repository, cls).query() + + if sorted: + q = q.order_by(func.lower(Repository.repo_name)) + + return q + + @classmethod def url_sep(cls): return URL_SEP