##// END OF EJS Templates
[API] Create groups needed when creating repo
Nicolas VINOT -
r1589:307ec693 beta
parent child Browse files
Show More
@@ -1,8 +1,6 b''
1 1 syntax: glob
2 2 *.pyc
3 3 *.swp
4 *.ini
5 Paste*
6 4
7 5 syntax: regexp
8 6 ^build
@@ -16,4 +14,3 b' syntax: regexp'
16 14 ^test\.db$
17 15 ^repositories\.config$
18 16 ^RhodeCode\.egg-info$
19 ^env$
@@ -11,6 +11,8 b' from rhodecode.model.user import UserMod'
11 11 from rhodecode.model.repo_permission import RepositoryPermissionModel
12 12 from rhodecode.model.users_group import UsersGroupModel
13 13 from rhodecode.model import users_group
14 from rhodecode.model.repos_group import ReposGroupModel
15 from sqlalchemy.orm.exc import NoResultFound
14 16
15 17 log = logging.getLogger( __name__ )
16 18
@@ -59,8 +61,9 b' class ApiController( JSONRPCController )'
59 61 :param username
60 62 """
61 63
62 user = User.by_username( username )
63 if not user:
64 try:
65 user = User.by_username( username )
66 except NoResultFound:
64 67 return None
65 68
66 69 return dict( id = user.user_id,
@@ -109,6 +112,9 b' class ApiController( JSONRPCController )'
109 112 :param ldap_dn:
110 113 """
111 114
115 if self.get_user( apiuser, username ):
116 raise JSONRPCError( "user %s already exist" % username )
117
112 118 try:
113 119 form_data = dict( username = username,
114 120 password = password,
@@ -191,6 +197,9 b' class ApiController( JSONRPCController )'
191 197 :param active:
192 198 """
193 199
200 if self.get_users_group( apiuser, name ):
201 raise JSONRPCError( "users group %s already exist" % name )
202
194 203 try:
195 204 form_data = dict( users_group_name = name,
196 205 users_group_active = active )
@@ -216,8 +225,9 b' class ApiController( JSONRPCController )'
216 225 if not users_group:
217 226 raise JSONRPCError( 'unknown users group %s' % group_name )
218 227
219 user = User.by_username( user_name )
220 if not user:
228 try:
229 user = User.by_username( user_name )
230 except NoResultFound:
221 231 raise JSONRPCError( 'unknown user %s' % user_name )
222 232
223 233 ugm = UsersGroupModel().add_user_to_group( users_group, user )
@@ -237,8 +247,9 b' class ApiController( JSONRPCController )'
237 247 :param repo_name
238 248 """
239 249
240 repo = Repository.by_repo_name( repo_name )
241 if not repo:
250 try:
251 repo = Repository.by_repo_name( repo_name )
252 except NoResultFound:
242 253 return None
243 254
244 255 members = []
@@ -287,8 +298,8 b' class ApiController( JSONRPCController )'
287 298 return result
288 299
289 300 @HasPermissionAnyDecorator( 'hg.admin', 'hg.create.repository' )
290 def create_repo( self, apiuser, name, owner_name, description = None, repo_type = 'hg', \
291 private = False, group_name = None ):
301 def create_repo( self, apiuser, name, owner_name, description = '', repo_type = 'hg', \
302 private = False ):
292 303 """
293 304 Create a repository
294 305
@@ -298,29 +309,36 b' class ApiController( JSONRPCController )'
298 309 :param type
299 310 :param private
300 311 :param owner_name
301 :param group_name
302 :param clone
303 312 """
304 313
305 314 try:
306 if group_name:
307 group = Group.get_by_group_name( group_name )
308 if group is None:
309 raise JSONRPCError( 'unknown group %s' % group_name )
310 else:
311 group = None
312
313 owner = User.by_username( owner_name )
314 if owner is None:
315 try:
316 owner = User.by_username( owner_name )
317 except NoResultFound:
315 318 raise JSONRPCError( 'unknown user %s' % owner )
316 319
317 RepoModel().create( { "repo_name" : name,
318 "repo_name_full" : name,
319 "description" : description,
320 "private" : private,
321 "repo_type" : repo_type,
322 "repo_group" : group,
323 "clone_uri" : None }, owner )
320 if self.get_repo( apiuser, name ):
321 raise JSONRPCError( "repo %s already exist" % name )
322
323 groups = name.split( '/' )
324 real_name = groups[-1]
325 groups = groups[:-1]
326 parent_id = None
327 for g in groups:
328 group = Group.get_by_group_name( g )
329 if not group:
330 group = ReposGroupModel().create( dict( group_name = g,
331 group_description = '',
332 group_parent_id = parent_id ) )
333 parent_id = group.group_id
334
335 RepoModel().create( dict( repo_name = real_name,
336 repo_name_full = name,
337 description = description,
338 private = private,
339 repo_type = repo_type,
340 repo_group = parent_id,
341 clone_uri = None ), owner )
324 342 except Exception:
325 343 log.error( traceback.format_exc() )
326 344 raise JSONRPCError( 'failed to create repository %s' % name )
@@ -337,12 +355,14 b' class ApiController( JSONRPCController )'
337 355 """
338 356
339 357 try:
340 repo = Repository.by_repo_name( repo_name )
341 if not repo:
358 try:
359 repo = Repository.by_repo_name( repo_name )
360 except NoResultFound:
342 361 raise JSONRPCError( 'unknown repository %s' % repo )
343 362
344 user = User.by_username( user_name )
345 if not user:
363 try:
364 user = User.by_username( user_name )
365 except NoResultFound:
346 366 raise JSONRPCError( 'unknown user %s' % user )
347 367
348 368 RepositoryPermissionModel().updateOrDeleteUserPermission( repo, user, perm )
@@ -27,18 +27,18 b' import logging'
27 27 from rhodecode.model.db import BaseModel, RepoToPerm, Permission
28 28 from rhodecode.model.meta import Session
29 29
30 log = logging.getLogger(__name__)
30 log = logging.getLogger( __name__ )
31 31
32 class RepositoryPermissionModel(BaseModel):
33 def getUserPermission(self, repository, user):
32 class RepositoryPermissionModel( BaseModel ):
33 def getUserPermission( self, repository, user ):
34 34 return RepoToPerm.query() \
35 .filter(RepoToPerm.user == user) \
36 .filter(RepoToPerm.repository == repository) \
35 .filter( RepoToPerm.user == user ) \
36 .filter( RepoToPerm.repository == repository ) \
37 37 .scalar()
38 38
39 def updateUserPermission(self, repository, user, permission):
40 permission = Permission.get_by_key(permission)
41 current = self.getUserPermission(repository, user)
39 def updateUserPermission( self, repository, user, permission ):
40 permission = Permission.get_by_key( permission )
41 current = self.getUserPermission( repository, user )
42 42 if current:
43 43 if not current.permission is permission:
44 44 current.permission = permission
@@ -47,16 +47,17 b' class RepositoryPermissionModel(BaseMode'
47 47 p.user = user
48 48 p.repository = repository
49 49 p.permission = permission
50 Session.add(p)
50 Session.add( p )
51 51 Session.commit()
52 52
53 def deleteUserPermission(self, repository, user):
54 current = self.getUserPermission(repository, user)
55 Session.delete(current)
56 Session.commit()
53 def deleteUserPermission( self, repository, user ):
54 current = self.getUserPermission( repository, user )
55 if current:
56 Session.delete( current )
57 Session.commit()
57 58
58 def updateOrDeleteUserPermission(self, repository, user, permission):
59 def updateOrDeleteUserPermission( self, repository, user, permission ):
59 60 if permission:
60 self.updateUserPermission(repository, user, permission)
61 self.updateUserPermission( repository, user, permission )
61 62 else:
62 self.deleteUserPermission(repository, user) No newline at end of file
63 self.deleteUserPermission( repository, user )
@@ -36,21 +36,21 b' from rhodecode.model import BaseModel'
36 36 from rhodecode.model.caching_query import FromCache
37 37 from rhodecode.model.db import Group, RhodeCodeUi
38 38
39 log = logging.getLogger(__name__)
39 log = logging.getLogger( __name__ )
40 40
41 41
42 class ReposGroupModel(BaseModel):
42 class ReposGroupModel( BaseModel ):
43 43
44 44 @LazyProperty
45 def repos_path(self):
45 def repos_path( self ):
46 46 """
47 47 Get's the repositories root path from database
48 48 """
49 49
50 q = RhodeCodeUi.get_by_key('/').one()
50 q = RhodeCodeUi.get_by_key( '/' ).one()
51 51 return q.ui_value
52 52
53 def __create_group(self, group_name, parent_id):
53 def __create_group( self, group_name, parent_id ):
54 54 """
55 55 makes repositories group on filesystem
56 56
@@ -59,64 +59,64 b' class ReposGroupModel(BaseModel):'
59 59 """
60 60
61 61 if parent_id:
62 paths = Group.get(parent_id).full_path.split(Group.url_sep())
63 parent_path = os.sep.join(paths)
62 paths = Group.get( parent_id ).full_path.split( Group.url_sep() )
63 parent_path = os.sep.join( paths )
64 64 else:
65 65 parent_path = ''
66 66
67 create_path = os.path.join(self.repos_path, parent_path, group_name)
68 log.debug('creating new group in %s', create_path)
67 create_path = os.path.join( self.repos_path, parent_path, group_name )
68 log.debug( 'creating new group in %s', create_path )
69 69
70 if os.path.isdir(create_path):
71 raise Exception('That directory already exists !')
70 if os.path.isdir( create_path ):
71 raise Exception( 'That directory already exists !' )
72 72
73 73
74 os.makedirs(create_path)
74 os.makedirs( create_path )
75 75
76 76
77 def __rename_group(self, old, old_parent_id, new, new_parent_id):
77 def __rename_group( self, old, old_parent_id, new, new_parent_id ):
78 78 """
79 79 Renames a group on filesystem
80 80
81 81 :param group_name:
82 82 """
83 log.debug('renaming repos group from %s to %s', old, new)
83 log.debug( 'renaming repos group from %s to %s', old, new )
84 84
85 85 if new_parent_id:
86 paths = Group.get(new_parent_id).full_path.split(Group.url_sep())
87 new_parent_path = os.sep.join(paths)
86 paths = Group.get( new_parent_id ).full_path.split( Group.url_sep() )
87 new_parent_path = os.sep.join( paths )
88 88 else:
89 89 new_parent_path = ''
90 90
91 91 if old_parent_id:
92 paths = Group.get(old_parent_id).full_path.split(Group.url_sep())
93 old_parent_path = os.sep.join(paths)
92 paths = Group.get( old_parent_id ).full_path.split( Group.url_sep() )
93 old_parent_path = os.sep.join( paths )
94 94 else:
95 95 old_parent_path = ''
96 96
97 old_path = os.path.join(self.repos_path, old_parent_path, old)
98 new_path = os.path.join(self.repos_path, new_parent_path, new)
97 old_path = os.path.join( self.repos_path, old_parent_path, old )
98 new_path = os.path.join( self.repos_path, new_parent_path, new )
99 99
100 log.debug('renaming repos paths from %s to %s', old_path, new_path)
100 log.debug( 'renaming repos paths from %s to %s', old_path, new_path )
101 101
102 if os.path.isdir(new_path):
103 raise Exception('Was trying to rename to already '
104 'existing dir %s' % new_path)
105 shutil.move(old_path, new_path)
102 if os.path.isdir( new_path ):
103 raise Exception( 'Was trying to rename to already '
104 'existing dir %s' % new_path )
105 shutil.move( old_path, new_path )
106 106
107 def __delete_group(self, group):
107 def __delete_group( self, group ):
108 108 """
109 109 Deletes a group from a filesystem
110 110
111 111 :param group: instance of group from database
112 112 """
113 paths = group.full_path.split(Group.url_sep())
114 paths = os.sep.join(paths)
113 paths = group.full_path.split( Group.url_sep() )
114 paths = os.sep.join( paths )
115 115
116 rm_path = os.path.join(self.repos_path, paths)
117 os.rmdir(rm_path)
116 rm_path = os.path.join( self.repos_path, paths )
117 os.rmdir( rm_path )
118 118
119 def create(self, form_data):
119 def create( self, form_data ):
120 120 try:
121 121 new_repos_group = Group()
122 122 new_repos_group.group_name = form_data['group_name']
@@ -124,21 +124,22 b' class ReposGroupModel(BaseModel):'
124 124 form_data['group_description']
125 125 new_repos_group.group_parent_id = form_data['group_parent_id']
126 126
127 self.sa.add(new_repos_group)
127 self.sa.add( new_repos_group )
128 128
129 self.__create_group(form_data['group_name'],
130 form_data['group_parent_id'])
129 self.__create_group( form_data['group_name'],
130 form_data['group_parent_id'] )
131 131
132 132 self.sa.commit()
133 return new_repos_group
133 134 except:
134 log.error(traceback.format_exc())
135 log.error( traceback.format_exc() )
135 136 self.sa.rollback()
136 137 raise
137 138
138 def update(self, repos_group_id, form_data):
139 def update( self, repos_group_id, form_data ):
139 140
140 141 try:
141 repos_group = Group.get(repos_group_id)
142 repos_group = Group.get( repos_group_id )
142 143 old_name = repos_group.group_name
143 144 old_parent_id = repos_group.group_parent_id
144 145
@@ -147,27 +148,27 b' class ReposGroupModel(BaseModel):'
147 148 form_data['group_description']
148 149 repos_group.group_parent_id = form_data['group_parent_id']
149 150
150 self.sa.add(repos_group)
151 self.sa.add( repos_group )
151 152
152 if old_name != form_data['group_name'] or (old_parent_id !=
153 form_data['group_parent_id']):
154 self.__rename_group(old=old_name, old_parent_id=old_parent_id,
155 new=form_data['group_name'],
156 new_parent_id=form_data['group_parent_id'])
153 if old_name != form_data['group_name'] or ( old_parent_id !=
154 form_data['group_parent_id'] ):
155 self.__rename_group( old = old_name, old_parent_id = old_parent_id,
156 new = form_data['group_name'],
157 new_parent_id = form_data['group_parent_id'] )
157 158
158 159 self.sa.commit()
159 160 except:
160 log.error(traceback.format_exc())
161 log.error( traceback.format_exc() )
161 162 self.sa.rollback()
162 163 raise
163 164
164 def delete(self, users_group_id):
165 def delete( self, users_group_id ):
165 166 try:
166 users_group = Group.get(users_group_id)
167 self.sa.delete(users_group)
168 self.__delete_group(users_group)
167 users_group = Group.get( users_group_id )
168 self.sa.delete( users_group )
169 self.__delete_group( users_group )
169 170 self.sa.commit()
170 171 except:
171 log.error(traceback.format_exc())
172 log.error( traceback.format_exc() )
172 173 self.sa.rollback()
173 174 raise
@@ -67,6 +67,11 b' class UsersGroupModel( BaseModel ):'
67 67 raise
68 68
69 69 def add_user_to_group( self, users_group, user ):
70 for m in users_group.members:
71 u = m.user
72 if u.user_id == user.user_id:
73 return m
74
70 75 try:
71 76 users_group_member = UsersGroupMember()
72 77 users_group_member.user = user
General Comments 0
You need to be logged in to leave comments. Login now