Mercurial > kallithea
diff rhodecode/controllers/api/api.py @ 3502:7cde75eac0fe beta
get_locks API function draft
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Thu, 07 Mar 2013 20:06:45 +0100 |
parents | 08e8115585bd |
children | b44979649915 |
line wrap: on
line diff
--- a/rhodecode/controllers/api/api.py Thu Mar 07 20:06:25 2013 +0100 +++ b/rhodecode/controllers/api/api.py Thu Mar 07 20:06:45 2013 +0100 @@ -33,7 +33,7 @@ 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.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 @@ 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 @@ '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): """