# HG changeset patch # User Søren Løvborg # Date 2016-09-23 14:05:42 # Node ID 7bffccee3a49be53e51b127cebc935a6f9418b59 # Parent af3539a458f6e039224710f99f3dd178c03136b6 db: inline calls to get_all This method saves basically no typing, compared to "query().all()". Additionally, "all()" returns a list, forcing all records to be loaded into a memory at the same time, but some callers just need to iterate over the objects one at a time, in which case "query()" alone is more efficient. In one case, the caller can even use "count()" and avoid loading any objects from the database at all. diff --git a/kallithea/controllers/admin/settings.py b/kallithea/controllers/admin/settings.py --- a/kallithea/controllers/admin/settings.py +++ b/kallithea/controllers/admin/settings.py @@ -167,7 +167,7 @@ class SettingsController(BaseController) if invalidate_cache: log.debug('invalidating all repositories cache') - for repo in Repository.get_all(): + for repo in Repository.query(): ScmModel().mark_for_invalidation(repo.repo_name) filesystem_repos = ScmModel().repo_scan() diff --git a/kallithea/controllers/api/api.py b/kallithea/controllers/api/api.py --- a/kallithea/controllers/api/api.py +++ b/kallithea/controllers/api/api.py @@ -443,7 +443,7 @@ class ApiController(JSONRPCController): user = get_user_or_error(userid) # show all locks - for r in Repository.get_all(): + for r in Repository.query(): userid, time_ = r.locked if time_: _api_data = r.get_api_data() @@ -848,7 +848,7 @@ class ApiController(JSONRPCController): result = [] _perms = ('usergroup.read', 'usergroup.write', 'usergroup.admin',) - for user_group in UserGroupList(UserGroup.get_all(), + for user_group in UserGroupList(UserGroup.query().all(), perm_set=_perms): result.append(user_group.get_api_data()) return result @@ -1272,7 +1272,7 @@ class ApiController(JSONRPCController): if not HasPermissionAny('hg.admin')(): repos = RepoModel().get_all_user_repos(user=self.authuser.user_id) else: - repos = Repository.get_all() + repos = Repository.query() for repo in repos: result.append(repo.get_api_data()) @@ -1950,7 +1950,7 @@ class ApiController(JSONRPCController): """ result = [] - for repo_group in RepoGroup.get_all(): + for repo_group in RepoGroup.query(): result.append(repo_group.get_api_data()) return result diff --git a/kallithea/lib/paster_commands/update_repoinfo.py b/kallithea/lib/paster_commands/update_repoinfo.py --- a/kallithea/lib/paster_commands/update_repoinfo.py +++ b/kallithea/lib/paster_commands/update_repoinfo.py @@ -54,7 +54,7 @@ class Command(BasePasterCommand): if self.options.repo_update_list is None: - repo_list = Repository.get_all() + repo_list = Repository.query().all() else: repo_names = [safe_unicode(n.strip()) for n in self.options.repo_update_list.split(',')] diff --git a/kallithea/model/db.py b/kallithea/model/db.py --- a/kallithea/model/db.py +++ b/kallithea/model/db.py @@ -160,10 +160,6 @@ class BaseModel(object): return res @classmethod - def get_all(cls): - return cls.query().all() - - @classmethod def delete(cls, id_): obj = cls.query().get(id_) Session().delete(obj) diff --git a/kallithea/model/repo.py b/kallithea/model/repo.py --- a/kallithea/model/repo.py +++ b/kallithea/model/repo.py @@ -179,7 +179,7 @@ class RepoModel(BaseModel): @classmethod def update_repoinfo(cls, repositories=None): if repositories is None: - repositories = Repository.get_all() + repositories = Repository.query() for repo in repositories: repo.update_changeset_cache() diff --git a/kallithea/tests/api/api_base.py b/kallithea/tests/api/api_base.py --- a/kallithea/tests/api/api_base.py +++ b/kallithea/tests/api/api_base.py @@ -880,7 +880,7 @@ class _BaseTestApi(object): response = api_call(self, params) result = [] - for repo in Repository.get_all(): + for repo in Repository.query(): result.append(repo.get_api_data()) ret = jsonify(result) diff --git a/kallithea/tests/fixture.py b/kallithea/tests/fixture.py --- a/kallithea/tests/fixture.py +++ b/kallithea/tests/fixture.py @@ -252,7 +252,7 @@ class Fixture(object): return gist def destroy_gists(self, gistid=None): - for g in Gist.get_all(): + for g in Gist.query(): if gistid: if gistid == g.gist_access_id: GistModel().delete(g) diff --git a/kallithea/tests/functional/test_admin_gists.py b/kallithea/tests/functional/test_admin_gists.py --- a/kallithea/tests/functional/test_admin_gists.py +++ b/kallithea/tests/functional/test_admin_gists.py @@ -21,7 +21,7 @@ def _create_gist(f_name, content='some g class TestGistsController(TestController): def teardown_method(self, method): - for g in Gist.get_all(): + for g in Gist.query(): GistModel().delete(g) Session().commit() diff --git a/kallithea/tests/functional/test_home.py b/kallithea/tests/functional/test_home.py --- a/kallithea/tests/functional/test_home.py +++ b/kallithea/tests/functional/test_home.py @@ -17,7 +17,7 @@ class TestHomeController(TestController) #if global permission is set response.mustcontain('Add Repository') # html in javascript variable: - response.mustcontain('var data = {"totalRecords": %s' % len(Repository.get_all())) + response.mustcontain('var data = {"totalRecords": %s' % Repository.query().count()) response.mustcontain(r'href=\"/%s\"' % HG_REPO) response.mustcontain(r'git') diff --git a/kallithea/tests/models/test_user_groups.py b/kallithea/tests/models/test_user_groups.py --- a/kallithea/tests/models/test_user_groups.py +++ b/kallithea/tests/models/test_user_groups.py @@ -14,7 +14,7 @@ class TestUserGroups(TestController): def teardown_method(self, method): # delete all groups - for gr in UserGroup.get_all(): + for gr in UserGroup.query(): fixture.destroy_user_group(gr) Session().commit() @@ -30,7 +30,7 @@ class TestUserGroups(TestController): def test_enforce_groups(self, pre_existing, regular_should_be, external_should_be, groups, expected): # delete all groups - for gr in UserGroup.get_all(): + for gr in UserGroup.query(): fixture.destroy_user_group(gr) Session().commit() diff --git a/kallithea/tests/other/manual_test_vcs_operations.py b/kallithea/tests/other/manual_test_vcs_operations.py --- a/kallithea/tests/other/manual_test_vcs_operations.py +++ b/kallithea/tests/other/manual_test_vcs_operations.py @@ -272,7 +272,7 @@ class TestVCSOperations(TestController): fixture.create_fork(GIT_REPO, fork_name) clone_url = _construct_url(fork_name).split()[0] stdout, stderr = _add_files_and_push('git', DEST, clone_url=clone_url) - print [(x.repo_full_path,x.repo_path) for x in Repository.get_all()] # TODO: what is this for + print [(x.repo_full_path,x.repo_path) for x in Repository.query()] # TODO: what is this for _check_proper_git_push(stdout, stderr) def test_push_invalidates_cache_hg(self): @@ -527,7 +527,7 @@ class TestVCSOperations(TestController): assert 'abort: HTTP Error 403: Forbidden' in stderr finally: #release IP restrictions - for ip in UserIpMap.get_all(): + for ip in UserIpMap.query(): UserIpMap.delete(ip.ip_id) Session().commit() @@ -555,7 +555,7 @@ class TestVCSOperations(TestController): assert re.search(r'\b403\b', stderr) finally: #release IP restrictions - for ip in UserIpMap.get_all(): + for ip in UserIpMap.query(): UserIpMap.delete(ip.ip_id) Session().commit()