changeset 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 3d5f4d2f0826
children 7adeca7b99c2
files rhodecode/controllers/api/api.py rhodecode/model/db.py rhodecode/tests/api/api_base.py
diffstat 3 files changed, 74 insertions(+), 6 deletions(-) [+]
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):
         """
--- a/rhodecode/model/db.py	Thu Mar 07 20:06:25 2013 +0100
+++ b/rhodecode/model/db.py	Thu Mar 07 20:06:45 2013 +0100
@@ -47,7 +47,7 @@
 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 @@
             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'))
--- a/rhodecode/tests/api/api_base.py	Thu Mar 07 20:06:25 2013 +0100
+++ b/rhodecode/tests/api/api_base.py	Thu Mar 07 20:06:45 2013 +0100
@@ -371,9 +371,8 @@
         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 @@
         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,