# HG changeset patch # User Marcin Kuzminski # Date 2013-03-07 19:06:45 # Node ID 7cde75eac0fe42e20a4d662b7740b88233eda141 # Parent 3d5f4d2f0826867264854f985dadc777ee8f6323 get_locks API function draft diff --git a/rhodecode/controllers/api/api.py b/rhodecode/controllers/api/api.py --- a/rhodecode/controllers/api/api.py +++ b/rhodecode/controllers/api/api.py @@ -33,7 +33,7 @@ from rhodecode.lib.auth import PasswordG HasPermissionAllDecorator, HasPermissionAnyDecorator, \ HasPermissionAnyApi, HasRepoPermissionAnyApi from rhodecode.lib.utils import map_groups, repo2db_mapper -from rhodecode.lib.utils2 import str2bool, time_to_datetime +from rhodecode.lib.utils2 import str2bool, time_to_datetime, safe_int from rhodecode.lib import helpers as h from rhodecode.model.meta import Session from rhodecode.model.scm import ScmModel @@ -42,6 +42,7 @@ from rhodecode.model.user import UserMod from rhodecode.model.users_group import UserGroupModel from rhodecode.model.permission import PermissionModel from rhodecode.model.db import Repository, RhodeCodeSetting, UserIpMap +from rhodecode.lib.compat import json log = logging.getLogger(__name__) @@ -274,7 +275,7 @@ class ApiController(JSONRPCController): return ('Repo `%s` locked by `%s`. Locked=`True`. ' 'Locked since: `%s`' % (repo.repo_name, user.username, - h.fmt_date(time_to_datetime(time_)))) + json.dumps(time_to_datetime(time_)))) else: locked = str2bool(locked) @@ -292,6 +293,44 @@ class ApiController(JSONRPCController): 'Error occurred locking repository `%s`' % repo.repo_name ) + def get_locks(self, apiuser, userid=Optional(OAttr('apiuser'))): + """ + Get all locks for given userid, if + this command is runned by non-admin account userid is set to user + who is calling this method, thus returning locks for himself + + :param apiuser: + :param userid: + """ + if HasPermissionAnyApi('hg.admin')(user=apiuser): + pass + else: + #make sure normal user does not pass someone else userid, + #he is not allowed to do that + if not isinstance(userid, Optional) and userid != apiuser.user_id: + raise JSONRPCError( + 'userid is not the same as your user' + ) + ret = [] + if isinstance(userid, Optional): + user = None + else: + user = get_user_or_error(userid) + + #show all locks + for r in Repository.getAll(): + userid, time_ = r.locked + if time_: + _api_data = r.get_api_data() + # if we use userfilter just show the locks for this user + if user: + if safe_int(userid) == user.user_id: + ret.append(_api_data) + else: + ret.append(_api_data) + + return ret + @HasPermissionAllDecorator('hg.admin') def show_ip(self, apiuser, userid): """ diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -47,7 +47,7 @@ from rhodecode.lib.vcs.utils.lazy import from rhodecode.lib.vcs.backends.base import EmptyChangeset from rhodecode.lib.utils2 import str2bool, safe_str, get_changeset_safe, \ - safe_unicode, remove_suffix, remove_prefix + safe_unicode, remove_suffix, remove_prefix, time_to_datetime from rhodecode.lib.compat import json from rhodecode.lib.caching_query import FromCache @@ -972,7 +972,11 @@ class Repository(Base, BaseModel): enable_statistics=repo.enable_statistics, enable_locking=repo.enable_locking, enable_downloads=repo.enable_downloads, - last_changeset=repo.changeset_cache + last_changeset=repo.changeset_cache, + locked_by=User.get(self.locked[0]).get_api_data() \ + if self.locked[0] else None, + locked_date=time_to_datetime(self.locked[1]) \ + if self.locked[1] else None ) rc_config = RhodeCodeSetting.get_app_settings() repository_fields = str2bool(rc_config.get('rhodecode_repository_fields')) diff --git a/rhodecode/tests/api/api_base.py b/rhodecode/tests/api/api_base.py --- a/rhodecode/tests/api/api_base.py +++ b/rhodecode/tests/api/api_base.py @@ -371,9 +371,8 @@ class BaseTestApi(object): self._compare_ok(id_, expected, given=response.body) def test_api_lock_repo_lock_optional_locked(self): - from rhodecode.lib import helpers from rhodecode.lib.utils2 import time_to_datetime - _locked_since = helpers.fmt_date(time_to_datetime(Repository\ + _locked_since = json.dumps(time_to_datetime(Repository\ .get_by_repo_name(self.REPO).locked[1])) id_, params = _build_data(self.apikey, 'lock', repoid=self.REPO) @@ -393,6 +392,32 @@ class BaseTestApi(object): expected = 'Error occurred locking repository `%s`' % self.REPO self._compare_error(id_, expected, given=response.body) + def test_api_get_locks_regular_user(self): + id_, params = _build_data(self.apikey_regular, 'get_locks') + response = api_call(self, params) + expected = [] + self._compare_ok(id_, expected, given=response.body) + + def test_api_get_locks_with_userid_regular_user(self): + id_, params = _build_data(self.apikey_regular, 'get_locks', + userid=TEST_USER_ADMIN_LOGIN) + response = api_call(self, params) + expected = 'userid is not the same as your user' + self._compare_error(id_, expected, given=response.body) + + def test_api_get_locks(self): + id_, params = _build_data(self.apikey, 'get_locks') + response = api_call(self, params) + expected = [] + self._compare_ok(id_, expected, given=response.body) + + def test_api_get_locks_with_userid(self): + id_, params = _build_data(self.apikey, 'get_locks', + userid=TEST_USER_REGULAR_LOGIN) + response = api_call(self, params) + expected = [] + self._compare_ok(id_, expected, given=response.body) + def test_api_create_existing_user(self): id_, params = _build_data(self.apikey, 'create_user', username=TEST_USER_ADMIN_LOGIN,