annotate rhodecode/controllers/api/api.py @ 1842:ea011e05fb0a

fixes #333 mismatch of argument for get_repo API method - a little code garden
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 07 Jan 2012 01:42:18 +0200
parents 9dae92a65e40
children 82a88013a3fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
1 # -*- coding: utf-8 -*-
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
2 """
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
3 rhodecode.controllers.api
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
5
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
6 API controller for RhodeCode
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
7
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
8 :created_on: Aug 20, 2011
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
9 :author: marcink
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
11 :license: GPLv3, see COPYING for more details.
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
12 """
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
13 # This program is free software; you can redistribute it and/or
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
14 # modify it under the terms of the GNU General Public License
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
15 # as published by the Free Software Foundation; version 2
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
16 # of the License or (at your opinion) any later version of the license.
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
17 #
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
18 # This program is distributed in the hope that it will be useful,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
21 # GNU General Public License for more details.
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
22 #
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
23 # You should have received a copy of the GNU General Public License
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
24 # along with this program; if not, write to the Free Software
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
26 # MA 02110-1301, USA.
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
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
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
32 from rhodecode.lib.auth import HasPermissionAllDecorator, \
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
33 HasPermissionAnyDecorator
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34 from rhodecode.model.scm import ScmModel
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
36 from rhodecode.model.db import User, UsersGroup, Group, Repository
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
37 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
38 from rhodecode.model.user import UserModel
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
39 from rhodecode.model.repo_permission import RepositoryPermissionModel
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
40 from rhodecode.model.users_group import UsersGroupModel
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
41 from rhodecode.model import users_group
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
42 from rhodecode.model.repos_group import ReposGroupModel
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
43 from sqlalchemy.orm.exc import NoResultFound
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
44
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
45
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
46 log = logging.getLogger(__name__)
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
47
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
48
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
49 class ApiController(JSONRPCController):
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
50 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
51 API Controller
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
52
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
53
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
54 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
55 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
56
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 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
58
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
59
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
60 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
61 errors that happens
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
62
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
65 @HasPermissionAllDecorator('hg.admin')
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
66 def pull(self, apiuser, repo):
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 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
69
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
70
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
71 :param user:
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
72 :param repo:
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 """
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
75 if Repository.is_valid(repo) is False:
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
76 raise JSONRPCError('Unknown repo "%s"' % repo)
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
77
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 try:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
79 ScmModel().pull_changes(repo, self.rhodecode_user.username)
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 return 'Pulled from %s' % repo
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
82 raise JSONRPCError('Unable to pull changes from "%s"' % repo)
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
84 @HasPermissionAllDecorator('hg.admin')
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
85 def get_user(self, apiuser, username):
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
86 """"
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
87 Get a user by username
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
88
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
89 :param apiuser:
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
90 :param username:
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
91 """
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
93 user = User.get_by_username(username)
1591
0b63a0d2cede Merge with upstream
Nicolas VINOT <aeris@imirhil.fr>
parents: 1589
diff changeset
94 if not user:
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
95 return None
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
96
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
97 return dict(
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
98 id=user.user_id,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
99 username=user.username,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
100 firstname=user.name,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
101 lastname=user.lastname,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
102 email=user.email,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
103 active=user.active,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
104 admin=user.admin,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
105 ldap=user.ldap_dn
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
106 )
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
107
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
108 @HasPermissionAllDecorator('hg.admin')
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
109 def get_users(self, apiuser):
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
110 """"
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
111 Get all users
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
112
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
113 :param apiuser:
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
114 """
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
115
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
116 result = []
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
117 for user in User.getAll():
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
118 result.append(
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
119 dict(
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
120 id=user.user_id,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
121 username=user.username,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
122 firstname=user.name,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
123 lastname=user.lastname,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
124 email=user.email,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
125 active=user.active,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
126 admin=user.admin,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
127 ldap=user.ldap_dn
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
128 )
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
129 )
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
130 return result
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
131
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
132 @HasPermissionAllDecorator('hg.admin')
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
133 def create_user(self, apiuser, username, password, firstname,
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
134 lastname, email, active=True, admin=False, ldap_dn=None):
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
135 """
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
136 Create new user
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
137
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
138 :param apiuser:
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
139 :param username:
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
140 :param password:
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
141 :param name:
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
142 :param lastname:
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
143 :param email:
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
144 :param active:
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
145 :param admin:
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
146 :param ldap_dn:
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
147 """
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
148
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
149 if User.get_by_username(username):
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
150 raise JSONRPCError("user %s already exist" % username)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
151
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
152 try:
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
153 form_data = dict(username=username,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
154 password=password,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
155 active=active,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
156 admin=admin,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
157 name=firstname,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
158 lastname=lastname,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
159 email=email,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
160 ldap_dn=ldap_dn)
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
161 UserModel().create_ldap(username, password, ldap_dn, form_data)
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
162 return dict(msg='created new user %s' % username)
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
163 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
164 log.error(traceback.format_exc())
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
165 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
166
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
167 @HasPermissionAllDecorator('hg.admin')
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
168 def get_users_group(self, apiuser, group_name):
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
169 """"
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
170 Get users group by name
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
171
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
172 :param apiuser:
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
173 :param group_name:
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
174 """
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
175
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
176 users_group = UsersGroup.get_by_group_name(group_name)
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
177 if not users_group:
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
178 return None
1445
c78f6bf52e9c Beginning of API implementation for rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
180 members = []
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
181 for user in users_group.members:
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
182 user = user.user
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
183 members.append(dict(id=user.user_id,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
184 username=user.username,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
185 firstname=user.name,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
186 lastname=user.lastname,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
187 email=user.email,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
188 active=user.active,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
189 admin=user.admin,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
190 ldap=user.ldap_dn))
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
191
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
192 return dict(id=users_group.users_group_id,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
193 name=users_group.users_group_name,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
194 active=users_group.users_group_active,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
195 members=members)
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
196
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
197 @HasPermissionAllDecorator('hg.admin')
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
198 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
199 """"
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
200 Get all users groups
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
201
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
202 :param apiuser:
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
203 """
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
204
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
205 result = []
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
206 for users_group in UsersGroup.getAll():
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
207 members = []
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
208 for user in users_group.members:
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
209 user = user.user
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
210 members.append(dict(id=user.user_id,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
211 username=user.username,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
212 firstname=user.name,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
213 lastname=user.lastname,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
214 email=user.email,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
215 active=user.active,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
216 admin=user.admin,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
217 ldap=user.ldap_dn))
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
218
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
219 result.append(dict(id=users_group.users_group_id,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
220 name=users_group.users_group_name,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
221 active=users_group.users_group_active,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
222 members=members))
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
223 return result
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
224
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
225 @HasPermissionAllDecorator('hg.admin')
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
226 def create_users_group(self, apiuser, name, active=True):
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
227 """
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
228 Creates an new usergroup
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
229
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
230 :param name:
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
231 :param active:
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
232 """
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
233
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
234 if self.get_users_group(apiuser, name):
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
235 raise JSONRPCError("users group %s already exist" % name)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
236
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
237 try:
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
238 form_data = dict(users_group_name=name,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
239 users_group_active=active)
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
240 ug = UsersGroup.create(form_data)
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
241 return dict(id=ug.users_group_id,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
242 msg='created new users group %s' % name)
1500
256e729a94cd Extended API
Marcin Kuzminski <marcin@python-works.com>
parents: 1445
diff changeset
243 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
244 log.error(traceback.format_exc())
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
245 raise JSONRPCError('failed to create group %s' % name)
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
246
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
247 @HasPermissionAllDecorator('hg.admin')
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
248 def add_user_to_users_group(self, apiuser, group_name, user_name):
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
249 """"
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
250 Add a user to a group
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
251
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
252 :param apiuser
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
253 :param group_name
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
254 :param user_name
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
255 """
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
256
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
257 try:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
258 users_group = UsersGroup.get_by_group_name(group_name)
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
259 if not users_group:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
260 raise JSONRPCError('unknown users group %s' % group_name)
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
261
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
262 try:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
263 user = User.get_by_username(user_name)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
264 except NoResultFound:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
265 raise JSONRPCError('unknown user %s' % user_name)
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
266
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
267 ugm = UsersGroupModel().add_user_to_group(users_group, user)
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
268
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
269 return dict(id=ugm.users_group_member_id,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
270 msg='created new users group member')
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
271 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
272 log.error(traceback.format_exc())
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
273 raise JSONRPCError('failed to create users group member')
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
274
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
275 @HasPermissionAnyDecorator('hg.admin')
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
276 def get_repo(self, apiuser, name):
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
277 """"
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
278 Get repository by name
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
279
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
280 :param apiuser
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
281 :param repo_name
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
282 """
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
283
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
284 try:
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
285 repo = Repository.get_by_repo_name(name)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
286 except NoResultFound:
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
287 return None
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
288
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
289 members = []
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
290 for user in repo.repo_to_perm:
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
291 perm = user.permission.permission_name
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
292 user = user.user
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
293 members.append(
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
294 dict(
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
295 type_="user",
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
296 id=user.user_id,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
297 username=user.username,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
298 firstname=user.name,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
299 lastname=user.lastname,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
300 email=user.email,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
301 active=user.active,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
302 admin=user.admin,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
303 ldap=user.ldap_dn,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
304 permission=perm
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
305 )
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
306 )
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
307 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
308 perm = users_group.permission.permission_name
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
309 users_group = users_group.users_group
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
310 members.append(
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
311 dict(
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
312 type_="users_group",
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
313 id=users_group.users_group_id,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
314 name=users_group.users_group_name,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
315 active=users_group.users_group_active,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
316 permission=perm
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
317 )
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
318 )
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
319
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
320 return dict(
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
321 id=repo.repo_id,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
322 name=repo.repo_name,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
323 type=repo.repo_type,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
324 description=repo.description,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
325 members=members
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
326 )
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
327
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
328 @HasPermissionAnyDecorator('hg.admin')
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
329 def get_repos(self, apiuser):
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
330 """"
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
331 Get all repositories
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
332
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
333 :param apiuser
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
334 """
1587
8898a79ac628 Implement all CRUD API operation for repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1586
diff changeset
335
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
336 result = []
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
337 for repository in Repository.getAll():
1842
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
338 result.append(
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
339 dict(
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
340 id=repository.repo_id,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
341 name=repository.repo_name,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
342 type=repository.repo_type,
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
343 description=repository.description
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
344 )
ea011e05fb0a fixes #333 mismatch of argument for get_repo API method
Marcin Kuzminski <marcin@python-works.com>
parents: 1594
diff changeset
345 )
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
346 return result
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
347
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
348 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
349 def create_repo(self, apiuser, name, owner_name, description='',
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
350 repo_type='hg', private=False):
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
351 """
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
352 Create a repository
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
353
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
354 :param apiuser
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
355 :param name
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
356 :param description
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
357 :param type
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
358 :param private
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
359 :param owner_name
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
360 """
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
361
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
362 try:
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
363 try:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
364 owner = User.get_by_username(owner_name)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
365 except NoResultFound:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
366 raise JSONRPCError('unknown user %s' % owner)
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
367
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
368 if self.get_repo(apiuser, name):
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
369 raise JSONRPCError("repo %s already exist" % name)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
370
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
371 groups = name.split('/')
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
372 real_name = groups[-1]
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
373 groups = groups[:-1]
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
374 parent_id = None
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
375 for g in groups:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
376 group = Group.get_by_group_name(g)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
377 if not group:
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
378 group = ReposGroupModel().create(dict(group_name=g,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
379 group_description='',
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
380 group_parent_id=parent_id))
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
381 parent_id = group.group_id
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
382
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
383 RepoModel().create(dict(repo_name=real_name,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
384 repo_name_full=name,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
385 description=description,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
386 private=private,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
387 repo_type=repo_type,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
388 repo_group=parent_id,
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
389 clone_uri=None), owner)
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
390 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
391 log.error(traceback.format_exc())
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
392 raise JSONRPCError('failed to create repository %s' % name)
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
393
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
394 @HasPermissionAnyDecorator('hg.admin')
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
395 def add_user_to_repo(self, apiuser, repo_name, user_name, perm):
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
396 """
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
397 Add permission for a user to a repository
1584
3338a0994472 Improve API with user/group/repo CRUD methods
Nicolas VINOT <aeris@imirhil.fr>
parents: 1508
diff changeset
398
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
399 :param apiuser
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
400 :param repo_name
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
401 :param user_name
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
402 :param perm
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
403 """
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
404
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
405 try:
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
406 try:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
407 repo = Repository.get_by_repo_name(repo_name)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
408 except NoResultFound:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
409 raise JSONRPCError('unknown repository %s' % repo)
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
410
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
411 try:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
412 user = User.get_by_username(user_name)
1589
307ec693bdf2 [API] Create groups needed when creating repo
Nicolas VINOT <aeris@imirhil.fr>
parents: 1587
diff changeset
413 except NoResultFound:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
414 raise JSONRPCError('unknown user %s' % user)
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
415
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
416 RepositoryPermissionModel()\
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
417 .update_or_delete_user_permission(repo, user, perm)
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
418 except Exception:
1593
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
419 log.error(traceback.format_exc())
92a4f7c496a5 Correct code style
Nicolas VINOT <aeris@imirhil.fr>
parents: 1591
diff changeset
420 raise JSONRPCError('failed to edit permission %(repo)s for %(user)s'
1594
9dae92a65e40 fixes #288
Marcin Kuzminski <marcin@python-works.com>
parents: 1593
diff changeset
421 % dict(user=user_name, repo=repo_name))
1586
2ccb32ddcfd7 Add API for repositories and groups (creation, permission)
Nicolas VINOT <aeris@imirhil.fr>
parents: 1584
diff changeset
422