annotate rhodecode/controllers/api/api.py @ 3759:12ca667b69b6 beta

api: don't report invalidated cache_keys after invalidating a repo The keys are an implementation detail and is never relevant outside RhodeCode internals and should not be leaked.
author Mads Kiilerich <madski@unity3d.com>
date Wed, 03 Apr 2013 15:56:12 +0200
parents e42e1d4e1c47
children a732bbc40c7e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1824
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
1 # -*- coding: utf-8 -*-
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
2 """
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
3 rhodecode.controllers.api
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
5
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
6 API controller for RhodeCode
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
7
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
8 :created_on: Aug 20, 2011
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
9 :author: marcink
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
11 :license: GPLv3, see COPYING for more details.
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
12 """
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
13 # This program is free software; you can redistribute it and/or
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
14 # modify it under the terms of the GNU General Public License
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
15 # as published by the Free Software Foundation; version 2
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
16 # of the License or (at your opinion) any later version of the license.
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
17 #
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
18 # This program is distributed in the hope that it will be useful,
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
21 # GNU General Public License for more details.
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
22 #
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
23 # You should have received a copy of the GNU General Public License
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
24 # along with this program; if not, write to the Free Software
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
26 # MA 02110-1301, USA.
89efedac4e6c 2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents: 1810
diff changeset
27
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
28 import traceback
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
29 import logging
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
30
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
32 from rhodecode.lib.auth import PasswordGenerator, AuthUser, \
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
33 HasPermissionAllDecorator, HasPermissionAnyDecorator, \
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
34 HasPermissionAnyApi, HasRepoPermissionAnyApi
2697
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
35 from rhodecode.lib.utils import map_groups, repo2db_mapper
3502
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
36 from rhodecode.lib.utils2 import str2bool, time_to_datetime, safe_int
3457
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
37 from rhodecode.lib import helpers as h
1734
48d4fcf04a29 another major refactoring with session management
Marcin Kuzminski <marcin@python-works.com>
parents: 1716
diff changeset
38 from rhodecode.model.meta import Session
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 from rhodecode.model.scm import ScmModel
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
40 from rhodecode.model.repo import RepoModel
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
41 from rhodecode.model.user import UserModel
3417
fa6ba6727475 further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents: 3410
diff changeset
42 from rhodecode.model.users_group import UserGroupModel
3730
e42e1d4e1c47 make the permission update function idempotent
Marcin Kuzminski <marcin@python-works.com>
parents: 3714
diff changeset
43 from rhodecode.model.db import Repository, RhodeCodeSetting, UserIpMap,\
e42e1d4e1c47 make the permission update function idempotent
Marcin Kuzminski <marcin@python-works.com>
parents: 3714
diff changeset
44 Permission
3502
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
45 from rhodecode.lib.compat import json
1734
48d4fcf04a29 another major refactoring with session management
Marcin Kuzminski <marcin@python-works.com>
parents: 1716
diff changeset
46
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
47 log = logging.getLogger(__name__)
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
48
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
49
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
50 class OptionalAttr(object):
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
51 """
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
52 Special Optional Option that defines other attribute
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
53 """
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
54 def __init__(self, attr_name):
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
55 self.attr_name = attr_name
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
56
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
57 def __repr__(self):
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
58 return '<OptionalAttr:%s>' % self.attr_name
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
59
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
60 def __call__(self):
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
61 return self
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
62 #alias
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
63 OAttr = OptionalAttr
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
64
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
65
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
66 class Optional(object):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
67 """
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
68 Defines an optional parameter::
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
69
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
70 param = param.getval() if isinstance(param, Optional) else param
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
71 param = param() if isinstance(param, Optional) else param
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
72
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
73 is equivalent of::
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
74
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
75 param = Optional.extract(param)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
76
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
77 """
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
78 def __init__(self, type_):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
79 self.type_ = type_
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
80
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
81 def __repr__(self):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
82 return '<Optional:%s>' % self.type_.__repr__()
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
83
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
84 def __call__(self):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
85 return self.getval()
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
86
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
87 def getval(self):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
88 """
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
89 returns value from this Optional instance
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
90 """
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
91 return self.type_
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
92
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
93 @classmethod
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
94 def extract(cls, val):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
95 if isinstance(val, cls):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
96 return val.getval()
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
97 return val
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
98
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
99
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
100 def get_user_or_error(userid):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
101 """
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
102 Get user by id or name or return JsonRPCError if not found
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
103
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
104 :param userid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
105 """
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
106 user = UserModel().get_user(userid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
107 if user is None:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
108 raise JSONRPCError("user `%s` does not exist" % userid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
109 return user
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
110
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
111
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
112 def get_repo_or_error(repoid):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
113 """
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
114 Get repo by id or name or return JsonRPCError if not found
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
115
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
116 :param userid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
117 """
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
118 repo = RepoModel().get_repo(repoid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
119 if repo is None:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
120 raise JSONRPCError('repository `%s` does not exist' % (repoid))
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
121 return repo
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
122
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
123
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
124 def get_users_group_or_error(usersgroupid):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
125 """
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
126 Get user group by id or name or return JsonRPCError if not found
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
127
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
128 :param userid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
129 """
3417
fa6ba6727475 further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents: 3410
diff changeset
130 users_group = UserGroupModel().get_group(usersgroupid)
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
131 if users_group is None:
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
132 raise JSONRPCError('user group `%s` does not exist' % usersgroupid)
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
133 return users_group
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
134
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
135
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
136 def get_perm_or_error(permid):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
137 """
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
138 Get permission by id or name or return JsonRPCError if not found
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
139
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
140 :param userid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
141 """
3730
e42e1d4e1c47 make the permission update function idempotent
Marcin Kuzminski <marcin@python-works.com>
parents: 3714
diff changeset
142 perm = Permission.get_by_key(permid)
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
143 if perm is None:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
144 raise JSONRPCError('permission `%s` does not exist' % (permid))
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
145 return perm
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
146
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
147
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
148 class ApiController(JSONRPCController):
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 API Controller
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
151
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
152
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 Each method needs to have USER as argument this is then based on given
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 API_KEY propagated as instance of user object
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
155
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 Preferably this should be first argument also
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
157
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
158
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
159 Each function should also **raise** JSONRPCError for any
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 errors that happens
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
161
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
164 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
165 def pull(self, apiuser, repoid):
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 Dispatch pull action on given repo
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
168
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
169 :param apiuser:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
170 :param repoid:
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
173 repo = get_repo_or_error(repoid)
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
174
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 try:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
176 ScmModel().pull_changes(repo.repo_name,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
177 self.rhodecode_user.username)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
178 return 'Pulled from `%s`' % repo.repo_name
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 except Exception:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
180 log.error(traceback.format_exc())
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
181 raise JSONRPCError(
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
182 'Unable to pull changes from `%s`' % repo.repo_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
183 )
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
185 @HasPermissionAllDecorator('hg.admin')
2697
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
186 def rescan_repos(self, apiuser, remove_obsolete=Optional(False)):
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
187 """
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
188 Dispatch rescan repositories action. If remove_obsolete is set
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
189 than also delete repos that are in database but not in the filesystem.
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
190 aka "clean zombies"
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
191
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
192 :param apiuser:
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
193 :param remove_obsolete:
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
194 """
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
195
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
196 try:
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
197 rm_obsolete = Optional.extract(remove_obsolete)
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
198 added, removed = repo2db_mapper(ScmModel().repo_scan(),
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
199 remove_obsolete=rm_obsolete)
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
200 return {'added': added, 'removed': removed}
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
201 except Exception:
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
202 log.error(traceback.format_exc())
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
203 raise JSONRPCError(
2737
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
204 'Error occurred during rescan repositories action'
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
205 )
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
206
3235
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
207 def invalidate_cache(self, apiuser, repoid):
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
208 """
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
209 Dispatch cache invalidation action on given repo
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
210
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
211 :param apiuser:
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
212 :param repoid:
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
213 """
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
214 repo = get_repo_or_error(repoid)
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
215 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False:
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
216 # check if we have admin permission for this repo !
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
217 if HasRepoPermissionAnyApi('repository.admin',
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
218 'repository.write')(user=apiuser,
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
219 repo_name=repo.repo_name) is False:
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
220 raise JSONRPCError('repository `%s` does not exist' % (repoid))
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
221
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
222 try:
3759
12ca667b69b6 api: don't report invalidated cache_keys after invalidating a repo
Mads Kiilerich <madski@unity3d.com>
parents: 3730
diff changeset
223 ScmModel().mark_for_invalidation(repo.repo_name)
12ca667b69b6 api: don't report invalidated cache_keys after invalidating a repo
Mads Kiilerich <madski@unity3d.com>
parents: 3730
diff changeset
224 return ('Caches of repository `%s` was invalidated' % repoid)
3235
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
225 except Exception:
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
226 log.error(traceback.format_exc())
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
227 raise JSONRPCError(
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
228 'Error occurred during cache invalidation action'
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
229 )
d6029dacbcc4 API invalidate_cache function ref #733
Marcin Kuzminski <marcin@python-works.com>
parents: 3195
diff changeset
230
3457
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
231 def lock(self, apiuser, repoid, locked=Optional(None),
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
232 userid=Optional(OAttr('apiuser'))):
2737
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
233 """
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
234 Set locking state on particular repository by given user, if
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
235 this command is runned by non-admin account userid is set to user
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
236 who is calling this method
2737
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
237
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
238 :param apiuser:
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
239 :param repoid:
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
240 :param userid:
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
241 :param locked:
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
242 """
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
243 repo = get_repo_or_error(repoid)
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
244 if HasPermissionAnyApi('hg.admin')(user=apiuser):
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
245 pass
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
246 elif HasRepoPermissionAnyApi('repository.admin',
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
247 'repository.write')(user=apiuser,
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
248 repo_name=repo.repo_name):
3168
2fb94c52e20e whitespace cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 3163
diff changeset
249 #make sure normal user does not pass someone else userid,
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
250 #he is not allowed to do that
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
251 if not isinstance(userid, Optional) and userid != apiuser.user_id:
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
252 raise JSONRPCError(
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
253 'userid is not the same as your user'
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
254 )
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
255 else:
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
256 raise JSONRPCError('repository `%s` does not exist' % (repoid))
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
257
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
258 if isinstance(userid, Optional):
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
259 userid = apiuser.user_id
3457
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
260
2737
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
261 user = get_user_or_error(userid)
3457
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
262
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
263 if isinstance(locked, Optional):
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
264 lockobj = Repository.getlock(repo)
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
265
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
266 if lockobj[0] is None:
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
267 return ('Repo `%s` not locked. Locked=`False`.'
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
268 % (repo.repo_name))
2737
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
269 else:
3457
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
270 userid, time_ = lockobj
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
271 user = get_user_or_error(userid)
2737
e21cb7b1a4a5 added API call for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2697
diff changeset
272
3457
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
273 return ('Repo `%s` locked by `%s`. Locked=`True`. '
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
274 'Locked since: `%s`'
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
275 % (repo.repo_name, user.username,
3502
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
276 json.dumps(time_to_datetime(time_))))
3457
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
277
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
278 else:
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
279 locked = str2bool(locked)
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
280 try:
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
281 if locked:
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
282 Repository.lock(repo, user.user_id)
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
283 else:
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
284 Repository.unlock(repo)
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
285
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
286 return ('User `%s` set lock state for repo `%s` to `%s`'
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
287 % (user.username, repo.repo_name, locked))
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
288 except Exception:
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
289 log.error(traceback.format_exc())
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
290 raise JSONRPCError(
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
291 'Error occurred locking repository `%s`' % repo.repo_name
08e8115585bd calling lock function without lock attribute, will return lock state
Marcin Kuzminski <marcin@python-works.com>
parents: 3450
diff changeset
292 )
2697
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
293
3502
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
294 def get_locks(self, apiuser, userid=Optional(OAttr('apiuser'))):
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
295 """
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
296 Get all locks for given userid, if
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
297 this command is runned by non-admin account userid is set to user
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
298 who is calling this method, thus returning locks for himself
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
299
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
300 :param apiuser:
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
301 :param userid:
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
302 """
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
303 if HasPermissionAnyApi('hg.admin')(user=apiuser):
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
304 pass
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
305 else:
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
306 #make sure normal user does not pass someone else userid,
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
307 #he is not allowed to do that
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
308 if not isinstance(userid, Optional) and userid != apiuser.user_id:
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
309 raise JSONRPCError(
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
310 'userid is not the same as your user'
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
311 )
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
312 ret = []
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
313 if isinstance(userid, Optional):
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
314 user = None
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
315 else:
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
316 user = get_user_or_error(userid)
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
317
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
318 #show all locks
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
319 for r in Repository.getAll():
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
320 userid, time_ = r.locked
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
321 if time_:
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
322 _api_data = r.get_api_data()
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
323 # if we use userfilter just show the locks for this user
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
324 if user:
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
325 if safe_int(userid) == user.user_id:
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
326 ret.append(_api_data)
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
327 else:
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
328 ret.append(_api_data)
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
329
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
330 return ret
7cde75eac0fe get_locks API function draft
Marcin Kuzminski <marcin@python-works.com>
parents: 3457
diff changeset
331
2697
4565e655ea2a API: Added option to rescann repositories via api call
Marcin Kuzminski <marcin@python-works.com>
parents: 2657
diff changeset
332 @HasPermissionAllDecorator('hg.admin')
3126
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
333 def show_ip(self, apiuser, userid):
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
334 """
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
335 Shows IP address as seen from RhodeCode server, together with all
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
336 defined IP addresses for given user
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
337
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
338 :param apiuser:
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
339 :param userid:
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
340 """
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
341 user = get_user_or_error(userid)
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
342 ips = UserIpMap.query().filter(UserIpMap.user == user).all()
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
343 return dict(
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
344 ip_addr_server=self.ip_addr,
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
345 user_ips=ips
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
346 )
703070153bc1 added API method for checking IP
Marcin Kuzminski <marcin@python-works.com>
parents: 3125
diff changeset
347
3162
a0a8f38e8fb8 API method get_user can be executed by non-admin users ref #539
Marcin Kuzminski <marcin@python-works.com>
parents: 3161
diff changeset
348 def get_user(self, apiuser, userid=Optional(OAttr('apiuser'))):
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
349 """"
3162
a0a8f38e8fb8 API method get_user can be executed by non-admin users ref #539
Marcin Kuzminski <marcin@python-works.com>
parents: 3161
diff changeset
350 Get a user by username, or userid, if userid is given
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
351
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
352 :param apiuser:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
353 :param userid:
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
354 """
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
355 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False:
3168
2fb94c52e20e whitespace cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 3163
diff changeset
356 #make sure normal user does not pass someone else userid,
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
357 #he is not allowed to do that
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
358 if not isinstance(userid, Optional) and userid != apiuser.user_id:
3162
a0a8f38e8fb8 API method get_user can be executed by non-admin users ref #539
Marcin Kuzminski <marcin@python-works.com>
parents: 3161
diff changeset
359 raise JSONRPCError(
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
360 'userid is not the same as your user'
3162
a0a8f38e8fb8 API method get_user can be executed by non-admin users ref #539
Marcin Kuzminski <marcin@python-works.com>
parents: 3161
diff changeset
361 )
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
362
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
363 if isinstance(userid, Optional):
3162
a0a8f38e8fb8 API method get_user can be executed by non-admin users ref #539
Marcin Kuzminski <marcin@python-works.com>
parents: 3161
diff changeset
364 userid = apiuser.user_id
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
365
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
366 user = get_user_or_error(userid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
367 data = user.get_api_data()
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
368 data['permissions'] = AuthUser(user_id=user.user_id).permissions
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
369 return data
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
370
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
371 @HasPermissionAllDecorator('hg.admin')
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
372 def get_users(self, apiuser):
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
373 """"
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
374 Get all users
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
375
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
376 :param apiuser:
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
377 """
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
378
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
379 result = []
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
380 for user in UserModel().get_all():
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
381 result.append(user.get_api_data())
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
382 return result
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
383
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
384 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
385 def create_user(self, apiuser, username, email, password,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
386 firstname=Optional(None), lastname=Optional(None),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
387 active=Optional(True), admin=Optional(False),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
388 ldap_dn=Optional(None)):
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
389 """
2002
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
390 Create new user
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
391
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
392 :param apiuser:
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
393 :param username:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
394 :param email:
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
395 :param password:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
396 :param firstname:
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
397 :param lastname:
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
398 :param active:
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
399 :param admin:
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
400 :param ldap_dn:
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
401 """
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
402
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
403 if UserModel().get_by_username(username):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
404 raise JSONRPCError("user `%s` already exist" % username)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
405
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
406 if UserModel().get_by_email(email, case_insensitive=True):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
407 raise JSONRPCError("email `%s` already exist" % email)
2008
9ddbfaeefb73 API: allowed password field to be null when used with ldap_dn ref #362
Marcin Kuzminski <marcin@python-works.com>
parents: 2006
diff changeset
408
2758
dc4709e7da51 fixed passing password via API create_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2737
diff changeset
409 if Optional.extract(ldap_dn):
2008
9ddbfaeefb73 API: allowed password field to be null when used with ldap_dn ref #362
Marcin Kuzminski <marcin@python-works.com>
parents: 2006
diff changeset
410 # generate temporary password if ldap_dn
9ddbfaeefb73 API: allowed password field to be null when used with ldap_dn ref #362
Marcin Kuzminski <marcin@python-works.com>
parents: 2006
diff changeset
411 password = PasswordGenerator().gen_password(length=8)
9ddbfaeefb73 API: allowed password field to be null when used with ldap_dn ref #362
Marcin Kuzminski <marcin@python-works.com>
parents: 2006
diff changeset
412
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
413 try:
2365
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
414 user = UserModel().create_or_update(
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
415 username=Optional.extract(username),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
416 password=Optional.extract(password),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
417 email=Optional.extract(email),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
418 firstname=Optional.extract(firstname),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
419 lastname=Optional.extract(lastname),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
420 active=Optional.extract(active),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
421 admin=Optional.extract(admin),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
422 ldap_dn=Optional.extract(ldap_dn)
1843
0771f0f5ab28 api review
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
423 )
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
424 Session().commit()
1843
0771f0f5ab28 api review
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
425 return dict(
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
426 msg='created new user `%s`' % username,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
427 user=user.get_api_data()
1843
0771f0f5ab28 api review
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
428 )
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
429 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
430 log.error(traceback.format_exc())
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
431 raise JSONRPCError('failed to create user `%s`' % username)
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
432
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
433 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
434 def update_user(self, apiuser, userid, username=Optional(None),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
435 email=Optional(None), firstname=Optional(None),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
436 lastname=Optional(None), active=Optional(None),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
437 admin=Optional(None), ldap_dn=Optional(None),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
438 password=Optional(None)):
2002
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
439 """
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
440 Updates given user
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
441
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
442 :param apiuser:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
443 :param userid:
2002
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
444 :param username:
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
445 :param email:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
446 :param firstname:
2002
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
447 :param lastname:
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
448 :param active:
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
449 :param admin:
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
450 :param ldap_dn:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
451 :param password:
2002
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
452 """
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
453
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
454 user = get_user_or_error(userid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
455
2657
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
456 # call function and store only updated arguments
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
457 updates = {}
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
458
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
459 def store_update(attr, name):
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
460 if not isinstance(attr, Optional):
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
461 updates[name] = attr
2002
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
462
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
463 try:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
464
2657
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
465 store_update(username, 'username')
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
466 store_update(password, 'password')
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
467 store_update(email, 'email')
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
468 store_update(firstname, 'name')
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
469 store_update(lastname, 'lastname')
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
470 store_update(active, 'active')
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
471 store_update(admin, 'admin')
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
472 store_update(ldap_dn, 'ldap_dn')
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
473
001c7e2ae986 fixed api issue with changing username during update_user
Marcin Kuzminski <marcin@python-works.com>
parents: 2653
diff changeset
474 user = UserModel().update_user(user, **updates)
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
475 Session().commit()
2002
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
476 return dict(
2507
374693af2849 API: update_user returns new updated user data
Marcin Kuzminski <marcin@python-works.com>
parents: 2506
diff changeset
477 msg='updated user ID:%s %s' % (user.user_id, user.username),
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
478 user=user.get_api_data()
2002
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
479 )
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
480 except Exception:
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
481 log.error(traceback.format_exc())
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
482 raise JSONRPCError('failed to update user `%s`' % userid)
2365
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
483
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
484 @HasPermissionAllDecorator('hg.admin')
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
485 def delete_user(self, apiuser, userid):
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
486 """"
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
487 Deletes an user
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
488
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
489 :param apiuser:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
490 :param userid:
2365
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
491 """
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
492 user = get_user_or_error(userid)
2365
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
493
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
494 try:
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
495 UserModel().delete(userid)
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
496 Session().commit()
2365
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
497 return dict(
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
498 msg='deleted user ID:%s %s' % (user.user_id, user.username),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
499 user=None
2365
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
500 )
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
501 except Exception:
b902baeaa494 API, added delete_user method.
Marcin Kuzminski <marcin@python-works.com>
parents: 2338
diff changeset
502 log.error(traceback.format_exc())
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
503 raise JSONRPCError('failed to delete ID:%s %s' % (user.user_id,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
504 user.username))
2002
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
505
bdc0ad168006 API added explicit method for updating user account
Marcin Kuzminski <marcin@python-works.com>
parents: 1989
diff changeset
506 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
507 def get_users_group(self, apiuser, usersgroupid):
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
508 """"
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
509 Get user group by name or id
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
510
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
511 :param apiuser:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
512 :param usersgroupid:
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
513 """
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
514 users_group = get_users_group_or_error(usersgroupid)
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
515
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
516 data = users_group.get_api_data()
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
517
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
518 members = []
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
519 for user in users_group.members:
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
520 user = user.user
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
521 members.append(user.get_api_data())
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
522 data['members'] = members
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
523 return data
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
524
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
525 @HasPermissionAllDecorator('hg.admin')
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
526 def get_users_groups(self, apiuser):
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
527 """"
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
528 Get all user groups
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
529
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
530 :param apiuser:
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
531 """
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
532
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
533 result = []
3417
fa6ba6727475 further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents: 3410
diff changeset
534 for users_group in UserGroupModel().get_all():
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
535 result.append(users_group.get_api_data())
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
536 return result
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
537
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
538 @HasPermissionAllDecorator('hg.admin')
3714
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
539 def create_users_group(self, apiuser, group_name,
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
540 owner=Optional(OAttr('apiuser')),
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
541 active=Optional(True)):
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
542 """
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
543 Creates an new usergroup
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
544
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
545 :param apiuser:
1843
0771f0f5ab28 api review
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
546 :param group_name:
3714
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
547 :param owner:
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
548 :param active:
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
549 """
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
550
3417
fa6ba6727475 further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents: 3410
diff changeset
551 if UserGroupModel().get_by_name(group_name):
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
552 raise JSONRPCError("user group `%s` already exist" % group_name)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
553
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
554 try:
3714
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
555 if isinstance(owner, Optional):
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
556 owner = apiuser.user_id
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
557
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
558 owner = get_user_or_error(owner)
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
559 active = Optional.extract(active)
3714
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
560 ug = UserGroupModel().create(name=group_name,
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
561 owner=owner,
7e3d89d9d3a2 - Manage User’s Groups: create, delete, rename, add/remove users inside.
Marcin Kuzminski <marcin@python-works.com>
parents: 3681
diff changeset
562 active=active)
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
563 Session().commit()
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
564 return dict(
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
565 msg='created new user group `%s`' % group_name,
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
566 users_group=ug.get_api_data()
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
567 )
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
568 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
569 log.error(traceback.format_exc())
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
570 raise JSONRPCError('failed to create group `%s`' % group_name)
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
571
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
572 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
573 def add_user_to_users_group(self, apiuser, usersgroupid, userid):
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
574 """"
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
575 Add a user to a user group
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
576
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
577 :param apiuser:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
578 :param usersgroupid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
579 :param userid:
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
580 """
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
581 user = get_user_or_error(userid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
582 users_group = get_users_group_or_error(usersgroupid)
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
583
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
584 try:
3417
fa6ba6727475 further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents: 3410
diff changeset
585 ugm = UserGroupModel().add_user_to_group(users_group, user)
1989
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
586 success = True if ugm != True else False
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
587 msg = 'added member `%s` to user group `%s`' % (
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
588 user.username, users_group.users_group_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
589 )
1989
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
590 msg = msg if success else 'User is already in that group'
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
591 Session().commit()
1989
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
592
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
593 return dict(
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
594 success=success,
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
595 msg=msg
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
596 )
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
597 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
598 log.error(traceback.format_exc())
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
599 raise JSONRPCError(
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
600 'failed to add member to user group `%s`' % (
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
601 users_group.users_group_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
602 )
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
603 )
1989
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
604
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
605 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
606 def remove_user_from_users_group(self, apiuser, usersgroupid, userid):
1989
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
607 """
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
608 Remove user from a group
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
609
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
610 :param apiuser:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
611 :param usersgroupid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
612 :param userid:
1989
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
613 """
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
614 user = get_user_or_error(userid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
615 users_group = get_users_group_or_error(usersgroupid)
1989
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
616
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
617 try:
3417
fa6ba6727475 further cleanup of UsersGroup
Mads Kiilerich <madski@unity3d.com>
parents: 3410
diff changeset
618 success = UserGroupModel().remove_user_from_group(users_group,
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
619 user)
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
620 msg = 'removed member `%s` from user group `%s`' % (
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
621 user.username, users_group.users_group_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
622 )
1989
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
623 msg = msg if success else "User wasn't in group"
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
624 Session().commit()
1989
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
625 return dict(success=success, msg=msg)
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
626 except Exception:
0f87c784756e API changes
Marcin Kuzminski <marcin@python-works.com>
parents: 1982
diff changeset
627 log.error(traceback.format_exc())
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
628 raise JSONRPCError(
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
629 'failed to remove member from user group `%s`' % (
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
630 users_group.users_group_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
631 )
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
632 )
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
633
2010
14dffcfebb02 API get_user and get_repo methods can fetch by id or names
Marcin Kuzminski <marcin@python-works.com>
parents: 2009
diff changeset
634 def get_repo(self, apiuser, repoid):
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
635 """"
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
636 Get repository by name
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
637
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
638 :param apiuser:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
639 :param repoid:
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
640 """
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
641 repo = get_repo_or_error(repoid)
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
642
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
643 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False:
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
644 # check if we have admin permission for this repo !
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
645 if HasRepoPermissionAnyApi('repository.admin')(user=apiuser,
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
646 repo_name=repo.repo_name) is False:
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
647 raise JSONRPCError('repository `%s` does not exist' % (repoid))
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
648
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
649 members = []
3195
a50901f2108d Added repository followers to the get_repo api function result.
aparkar <aparkar@icloud.com>
parents: 3168
diff changeset
650 followers = []
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
651 for user in repo.repo_to_perm:
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
652 perm = user.permission.permission_name
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
653 user = user.user
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
654 user_data = user.get_api_data()
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
655 user_data['type'] = "user"
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
656 user_data['permission'] = perm
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
657 members.append(user_data)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
658
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
659 for users_group in repo.users_group_to_perm:
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
660 perm = users_group.permission.permission_name
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
661 users_group = users_group.users_group
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
662 users_group_data = users_group.get_api_data()
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
663 users_group_data['type'] = "users_group"
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
664 users_group_data['permission'] = perm
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
665 members.append(users_group_data)
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
666
3195
a50901f2108d Added repository followers to the get_repo api function result.
aparkar <aparkar@icloud.com>
parents: 3168
diff changeset
667 for user in repo.followers:
a50901f2108d Added repository followers to the get_repo api function result.
aparkar <aparkar@icloud.com>
parents: 3168
diff changeset
668 followers.append(user.user.get_api_data())
a50901f2108d Added repository followers to the get_repo api function result.
aparkar <aparkar@icloud.com>
parents: 3168
diff changeset
669
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
670 data = repo.get_api_data()
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
671 data['members'] = members
3195
a50901f2108d Added repository followers to the get_repo api function result.
aparkar <aparkar@icloud.com>
parents: 3168
diff changeset
672 data['followers'] = followers
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
673 return data
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
674
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
675 def get_repos(self, apiuser):
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
676 """"
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
677 Get all repositories
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
678
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
679 :param apiuser:
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
680 """
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
681 result = []
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
682 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False:
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
683 repos = RepoModel().get_all_user_repos(user=apiuser)
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
684 else:
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
685 repos = RepoModel().get_all()
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
686
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
687 for repo in repos:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
688 result.append(repo.get_api_data())
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
689 return result
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
690
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
691 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
692 def get_repo_nodes(self, apiuser, repoid, revision, root_path,
1810
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
693 ret_type='all'):
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
694 """
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
695 returns a list of nodes and it's children
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
696 for a given path at given revision. It's possible to specify ret_type
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
697 to show only files or dirs
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
698
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
699 :param apiuser:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
700 :param repoid: name or id of repository
1810
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
701 :param revision: revision for which listing should be done
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
702 :param root_path: path from which start displaying
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
703 :param ret_type: return type 'all|files|dirs' nodes
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
704 """
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
705 repo = get_repo_or_error(repoid)
1810
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
706 try:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
707 _d, _f = ScmModel().get_nodes(repo, revision, root_path,
1810
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
708 flat=False)
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
709 _map = {
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
710 'all': _d + _f,
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
711 'files': _f,
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
712 'dirs': _d,
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
713 }
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
714 return _map[ret_type]
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
715 except KeyError:
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
716 raise JSONRPCError('ret_type must be one of %s' % _map.keys())
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
717 except Exception:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
718 log.error(traceback.format_exc())
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
719 raise JSONRPCError(
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
720 'failed to get repo: `%s` nodes' % repo.repo_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
721 )
1810
203af05539e0 implements #330 api method for listing nodes at particular revision
Marcin Kuzminski <marcin@python-works.com>
parents: 1793
diff changeset
722
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
723 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
724 def create_repo(self, apiuser, repo_name, owner=Optional(OAttr('apiuser')),
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
725 repo_type=Optional('hg'),
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
726 description=Optional(''), private=Optional(False),
3115
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
727 clone_uri=Optional(None), landing_rev=Optional('tip'),
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
728 enable_statistics=Optional(False),
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
729 enable_locking=Optional(False),
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
730 enable_downloads=Optional(False)):
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
731 """
2006
34d009e5147a added clone_uri to API method for creating users
Marcin Kuzminski <marcin@python-works.com>
parents: 2004
diff changeset
732 Create repository, if clone_url is given it makes a remote clone
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
733 if repo_name is within a group name the groups will be created
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
734 automatically if they aren't present
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
735
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
736 :param apiuser:
1843
0771f0f5ab28 api review
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
737 :param repo_name:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
738 :param onwer:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
739 :param repo_type:
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
740 :param description:
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
741 :param private:
2006
34d009e5147a added clone_uri to API method for creating users
Marcin Kuzminski <marcin@python-works.com>
parents: 2004
diff changeset
742 :param clone_uri:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
743 :param landing_rev:
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
744 """
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
745 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False:
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
746 if not isinstance(owner, Optional):
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
747 #forbid setting owner for non-admins
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
748 raise JSONRPCError(
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
749 'Only RhodeCode admin can specify `owner` param'
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
750 )
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
751 if isinstance(owner, Optional):
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
752 owner = apiuser.user_id
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
753
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
754 owner = get_user_or_error(owner)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
755
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
756 if RepoModel().get_by_repo_name(repo_name):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
757 raise JSONRPCError("repo `%s` already exist" % repo_name)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
758
3115
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
759 defs = RhodeCodeSetting.get_default_repo_settings(strip_prefix=True)
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
760 if isinstance(private, Optional):
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
761 private = defs.get('repo_private') or Optional.extract(private)
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
762 if isinstance(repo_type, Optional):
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
763 repo_type = defs.get('repo_type')
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
764 if isinstance(enable_statistics, Optional):
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
765 enable_statistics = defs.get('repo_enable_statistics')
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
766 if isinstance(enable_locking, Optional):
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
767 enable_locking = defs.get('repo_enable_locking')
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
768 if isinstance(enable_downloads, Optional):
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
769 enable_downloads = defs.get('repo_enable_downloads')
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
770
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
771 clone_uri = Optional.extract(clone_uri)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
772 description = Optional.extract(description)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
773 landing_rev = Optional.extract(landing_rev)
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
774
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
775 try:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
776 # create structure of groups and return the last group
2120
d5527cebf76a Resolve error occurring during recursive group creation in API create-repo function
Marcin Kuzminski <marcin@python-works.com>
parents: 2010
diff changeset
777 group = map_groups(repo_name)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
778
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
779 repo = RepoModel().create_repo(
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
780 repo_name=repo_name,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
781 repo_type=repo_type,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
782 description=description,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
783 owner=owner,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
784 private=private,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
785 clone_uri=clone_uri,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
786 repos_group=group,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
787 landing_rev=landing_rev,
3115
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
788 enable_statistics=enable_statistics,
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
789 enable_downloads=enable_downloads,
ebd76deee70d Default parameters are now also used for creating repos using API calls, and initial repo scanner
Marcin Kuzminski <marcin@python-works.com>
parents: 3024
diff changeset
790 enable_locking=enable_locking
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
791 )
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
792
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
793 Session().commit()
1843
0771f0f5ab28 api review
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
794 return dict(
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
795 msg="Created new repository `%s`" % (repo.repo_name),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
796 repo=repo.get_api_data()
1843
0771f0f5ab28 api review
Marcin Kuzminski <marcin@python-works.com>
parents: 1824
diff changeset
797 )
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
798 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
799 log.error(traceback.format_exc())
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
800 raise JSONRPCError('failed to create repository `%s`' % repo_name)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
801
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
802 @HasPermissionAnyDecorator('hg.admin', 'hg.fork.repository')
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
803 def fork_repo(self, apiuser, repoid, fork_name, owner=Optional(OAttr('apiuser')),
2653
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
804 description=Optional(''), copy_permissions=Optional(False),
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
805 private=Optional(False), landing_rev=Optional('tip')):
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
806 repo = get_repo_or_error(repoid)
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
807 repo_name = repo.repo_name
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
808
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
809 _repo = RepoModel().get_by_repo_name(fork_name)
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
810 if _repo:
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
811 type_ = 'fork' if _repo.fork else 'repo'
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
812 raise JSONRPCError("%s `%s` already exist" % (type_, fork_name))
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
813
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
814 if HasPermissionAnyApi('hg.admin')(user=apiuser):
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
815 pass
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
816 elif HasRepoPermissionAnyApi('repository.admin',
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
817 'repository.write',
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
818 'repository.read')(user=apiuser,
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
819 repo_name=repo.repo_name):
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
820 if not isinstance(owner, Optional):
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
821 #forbid setting owner for non-admins
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
822 raise JSONRPCError(
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
823 'Only RhodeCode admin can specify `owner` param'
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
824 )
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
825 else:
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
826 raise JSONRPCError('repository `%s` does not exist' % (repoid))
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
827
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
828 if isinstance(owner, Optional):
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
829 owner = apiuser.user_id
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
830
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
831 owner = get_user_or_error(owner)
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
832
2653
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
833 try:
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
834 # create structure of groups and return the last group
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
835 group = map_groups(fork_name)
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
836
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
837 form_data = dict(
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
838 repo_name=fork_name,
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
839 repo_name_full=fork_name,
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
840 repo_group=group,
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
841 repo_type=repo.repo_type,
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
842 description=Optional.extract(description),
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
843 private=Optional.extract(private),
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
844 copy_permissions=Optional.extract(copy_permissions),
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
845 landing_rev=Optional.extract(landing_rev),
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
846 update_after_clone=False,
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
847 fork_parent_id=repo.repo_id,
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
848 )
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
849 RepoModel().create_fork(form_data, cur_user=owner)
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
850 return dict(
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
851 msg='Created fork of `%s` as `%s`' % (repo.repo_name,
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
852 fork_name),
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
853 success=True # cannot return the repo data here since fork
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
854 # cann be done async
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
855 )
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
856 except Exception:
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
857 log.error(traceback.format_exc())
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
858 raise JSONRPCError(
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
859 'failed to fork repository `%s` as `%s`' % (repo_name,
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
860 fork_name)
8be70a4d72c7 Implemented #354 added forking option to API
Marcin Kuzminski <marcin@python-works.com>
parents: 2526
diff changeset
861 )
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
862
3641
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
863 def delete_repo(self, apiuser, repoid, forks=Optional(None)):
2003
7dfcdf4c7dd2 implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2002
diff changeset
864 """
7dfcdf4c7dd2 implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2002
diff changeset
865 Deletes a given repository
7dfcdf4c7dd2 implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2002
diff changeset
866
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
867 :param apiuser:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
868 :param repoid:
3641
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
869 :param forks: detach or delete, what do do with attached forks for repo
2003
7dfcdf4c7dd2 implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2002
diff changeset
870 """
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
871 repo = get_repo_or_error(repoid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
872
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
873 if HasPermissionAnyApi('hg.admin')(user=apiuser) is False:
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
874 # check if we have admin permission for this repo !
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
875 if HasRepoPermissionAnyApi('repository.admin')(user=apiuser,
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
876 repo_name=repo.repo_name) is False:
3641
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
877 raise JSONRPCError('repository `%s` does not exist' % (repoid))
3163
28571535dd61 API methods create_repo, fork_repo, delete_repo, get_repo, get_repos
Marcin Kuzminski <marcin@python-works.com>
parents: 3162
diff changeset
878
2003
7dfcdf4c7dd2 implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2002
diff changeset
879 try:
3641
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
880 handle_forks = Optional.extract(forks)
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
881 _forks_msg = ''
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
882 _forks = [f for f in repo.forks]
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
883 if handle_forks == 'detach':
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
884 _forks_msg = ' ' + _('Detached %s forks') % len(_forks)
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
885 elif handle_forks == 'delete':
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
886 _forks_msg = ' ' + _('Deleted %s forks') % len(_forks)
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
887 elif _forks:
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
888 raise JSONRPCError(
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
889 'Cannot delete `%s` it still contains attached forks'
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
890 % repo.repo_name
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
891 )
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
892
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
893 RepoModel().delete(repo, forks=forks)
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
894 Session().commit()
2003
7dfcdf4c7dd2 implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2002
diff changeset
895 return dict(
3641
b44979649915 recursive forks detach
Marcin Kuzminski <marcin@python-works.com>
parents: 3502
diff changeset
896 msg='Deleted repository `%s`%s' % (repo.repo_name, _forks_msg),
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
897 success=True
2003
7dfcdf4c7dd2 implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2002
diff changeset
898 )
7dfcdf4c7dd2 implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2002
diff changeset
899 except Exception:
7dfcdf4c7dd2 implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2002
diff changeset
900 log.error(traceback.format_exc())
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
901 raise JSONRPCError(
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
902 'failed to delete repository `%s`' % repo.repo_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
903 )
2003
7dfcdf4c7dd2 implements #361 API method for deleting repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 2002
diff changeset
904
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
905 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
906 def grant_user_permission(self, apiuser, repoid, userid, perm):
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
907 """
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
908 Grant permission for user on given repository, or update existing one
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
909 if found
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
910
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
911 :param repoid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
912 :param userid:
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
913 :param perm:
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
914 """
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
915 repo = get_repo_or_error(repoid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
916 user = get_user_or_error(userid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
917 perm = get_perm_or_error(perm)
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
918
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
919 try:
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
920
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
921 RepoModel().grant_user_permission(repo=repo, user=user, perm=perm)
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
922
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
923 Session().commit()
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
924 return dict(
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
925 msg='Granted perm: `%s` for user: `%s` in repo: `%s`' % (
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
926 perm.permission_name, user.username, repo.repo_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
927 ),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
928 success=True
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
929 )
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
930 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
931 log.error(traceback.format_exc())
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
932 raise JSONRPCError(
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
933 'failed to edit permission for user: `%s` in repo: `%s`' % (
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
934 userid, repoid
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
935 )
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
936 )
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
937
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
938 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
939 def revoke_user_permission(self, apiuser, repoid, userid):
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
940 """
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
941 Revoke permission for user on given repository
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
942
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
943 :param apiuser:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
944 :param repoid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
945 :param userid:
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
946 """
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
947
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
948 repo = get_repo_or_error(repoid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
949 user = get_user_or_error(userid)
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
950 try:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
951
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
952 RepoModel().revoke_user_permission(repo=repo, user=user)
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
953
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
954 Session().commit()
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
955 return dict(
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
956 msg='Revoked perm for user: `%s` in repo: `%s`' % (
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
957 user.username, repo.repo_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
958 ),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
959 success=True
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
960 )
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
961 except Exception:
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
962 log.error(traceback.format_exc())
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
963 raise JSONRPCError(
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
964 'failed to edit permission for user: `%s` in repo: `%s`' % (
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
965 userid, repoid
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
966 )
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
967 )
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
968
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
969 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
970 def grant_users_group_permission(self, apiuser, repoid, usersgroupid,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
971 perm):
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
972 """
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
973 Grant permission for user group on given repository, or update
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
974 existing one if found
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
975
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
976 :param apiuser:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
977 :param repoid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
978 :param usersgroupid:
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
979 :param perm:
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
980 """
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
981 repo = get_repo_or_error(repoid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
982 perm = get_perm_or_error(perm)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
983 users_group = get_users_group_or_error(usersgroupid)
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
984
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
985 try:
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
986 RepoModel().grant_users_group_permission(repo=repo,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
987 group_name=users_group,
1982
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
988 perm=perm)
87f0800abc7b #227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents: 1950
diff changeset
989
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
990 Session().commit()
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
991 return dict(
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
992 msg='Granted perm: `%s` for user group: `%s` in '
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
993 'repo: `%s`' % (
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
994 perm.permission_name, users_group.users_group_name,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
995 repo.repo_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
996 ),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
997 success=True
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
998 )
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
999 except Exception:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1000 log.error(traceback.format_exc())
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1001 raise JSONRPCError(
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
1002 'failed to edit permission for user group: `%s` in '
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1003 'repo: `%s`' % (
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1004 usersgroupid, repo.repo_name
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
1005 )
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
1006 )
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1007
3161
3563c47e52fd Implemented API calls for non-admin users for locking/unlocking repositories
Marcin Kuzminski <marcin@python-works.com>
parents: 3126
diff changeset
1008 @HasPermissionAllDecorator('hg.admin')
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1009 def revoke_users_group_permission(self, apiuser, repoid, usersgroupid):
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1010 """
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
1011 Revoke permission for user group on given repository
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1012
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1013 :param apiuser:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1014 :param repoid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1015 :param usersgroupid:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1016 """
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1017 repo = get_repo_or_error(repoid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1018 users_group = get_users_group_or_error(usersgroupid)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1019
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1020 try:
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1021 RepoModel().revoke_users_group_permission(repo=repo,
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1022 group_name=users_group)
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1023
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1024 Session().commit()
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1025 return dict(
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
1026 msg='Revoked perm for user group: `%s` in repo: `%s`' % (
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1027 users_group.users_group_name, repo.repo_name
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1028 ),
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1029 success=True
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1030 )
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
1031 except Exception:
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
1032 log.error(traceback.format_exc())
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
1033 raise JSONRPCError(
3410
5f1850e4712a "Users groups" is grammatically incorrect English - rename to "user groups"
Mads Kiilerich <madski@unity3d.com>
parents: 3235
diff changeset
1034 'failed to edit permission for user group: `%s` in '
2526
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1035 'repo: `%s`' % (
473794943022 Refactored API
Marcin Kuzminski <marcin@python-works.com>
parents: 2507
diff changeset
1036 users_group.users_group_name, repo.repo_name
1793
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
1037 )
631caf880b87 implements #329
Marcin Kuzminski <marcin@python-works.com>
parents: 1749
diff changeset
1038 )