Show More
@@ -14,10 +14,10 b' from rhodecode.model import users_group' | |||||
14 | from rhodecode.model.repos_group import ReposGroupModel |
|
14 | from rhodecode.model.repos_group import ReposGroupModel | |
15 | from sqlalchemy.orm.exc import NoResultFound |
|
15 | from sqlalchemy.orm.exc import NoResultFound | |
16 |
|
16 | |||
17 |
log = logging.getLogger( |
|
17 | log = logging.getLogger(__name__) | |
18 |
|
18 | |||
19 |
|
19 | |||
20 |
class ApiController( |
|
20 | class ApiController(JSONRPCController): | |
21 | """ |
|
21 | """ | |
22 | API Controller |
|
22 | API Controller | |
23 |
|
23 | |||
@@ -33,8 +33,8 b' class ApiController( JSONRPCController )' | |||||
33 |
|
33 | |||
34 | """ |
|
34 | """ | |
35 |
|
35 | |||
36 |
@HasPermissionAllDecorator( |
|
36 | @HasPermissionAllDecorator('hg.admin') | |
37 |
def pull( |
|
37 | def pull(self, apiuser, repo): | |
38 | """ |
|
38 | """ | |
39 | Dispatch pull action on given repo |
|
39 | Dispatch pull action on given repo | |
40 |
|
40 | |||
@@ -43,17 +43,17 b' class ApiController( JSONRPCController )' | |||||
43 | :param repo: |
|
43 | :param repo: | |
44 | """ |
|
44 | """ | |
45 |
|
45 | |||
46 |
if Repository.is_valid( |
|
46 | if Repository.is_valid(repo) is False: | |
47 |
raise JSONRPCError( |
|
47 | raise JSONRPCError('Unknown repo "%s"' % repo) | |
48 |
|
48 | |||
49 | try: |
|
49 | try: | |
50 |
ScmModel().pull_changes( |
|
50 | ScmModel().pull_changes(repo, self.rhodecode_user.username) | |
51 | return 'Pulled from %s' % repo |
|
51 | return 'Pulled from %s' % repo | |
52 | except Exception: |
|
52 | except Exception: | |
53 |
raise JSONRPCError( |
|
53 | raise JSONRPCError('Unable to pull changes from "%s"' % repo) | |
54 |
|
54 | |||
55 |
@HasPermissionAllDecorator( |
|
55 | @HasPermissionAllDecorator('hg.admin') | |
56 |
def get_user( |
|
56 | def get_user(self, apiuser, username): | |
57 | """" |
|
57 | """" | |
58 | Get a user by username |
|
58 | Get a user by username | |
59 |
|
59 | |||
@@ -61,21 +61,21 b' class ApiController( JSONRPCController )' | |||||
61 | :param username |
|
61 | :param username | |
62 | """ |
|
62 | """ | |
63 |
|
63 | |||
64 |
user = User.get_by_username( |
|
64 | user = User.get_by_username(username) | |
65 | if not user: |
|
65 | if not user: | |
66 | return None |
|
66 | return None | |
67 |
|
67 | |||
68 |
return dict( |
|
68 | return dict(id = user.user_id, | |
69 | username = user.username, |
|
69 | username = user.username, | |
70 | firstname = user.name, |
|
70 | firstname = user.name, | |
71 | lastname = user.lastname, |
|
71 | lastname = user.lastname, | |
72 | email = user.email, |
|
72 | email = user.email, | |
73 | active = user.active, |
|
73 | active = user.active, | |
74 | admin = user.admin, |
|
74 | admin = user.admin, | |
75 |
ldap = user.ldap_dn |
|
75 | ldap = user.ldap_dn) | |
76 |
|
76 | |||
77 |
@HasPermissionAllDecorator( |
|
77 | @HasPermissionAllDecorator('hg.admin') | |
78 |
def get_users( |
|
78 | def get_users(self, apiuser): | |
79 | """" |
|
79 | """" | |
80 | Get all users |
|
80 | Get all users | |
81 |
|
81 | |||
@@ -84,19 +84,19 b' class ApiController( JSONRPCController )' | |||||
84 |
|
84 | |||
85 | result = [] |
|
85 | result = [] | |
86 | for user in User.getAll(): |
|
86 | for user in User.getAll(): | |
87 |
result.append( |
|
87 | result.append(dict(id = user.user_id, | |
88 | username = user.username, |
|
88 | username = user.username, | |
89 | firstname = user.name, |
|
89 | firstname = user.name, | |
90 | lastname = user.lastname, |
|
90 | lastname = user.lastname, | |
91 | email = user.email, |
|
91 | email = user.email, | |
92 | active = user.active, |
|
92 | active = user.active, | |
93 | admin = user.admin, |
|
93 | admin = user.admin, | |
94 |
ldap = user.ldap_dn |
|
94 | ldap = user.ldap_dn)) | |
95 | return result |
|
95 | return result | |
96 |
|
96 | |||
97 |
@HasPermissionAllDecorator( |
|
97 | @HasPermissionAllDecorator('hg.admin') | |
98 |
def create_user( |
|
98 | def create_user(self, apiuser, username, password, firstname, | |
99 |
lastname, email, active = True, admin = False, ldap_dn = None |
|
99 | lastname, email, active = True, admin = False, ldap_dn = None): | |
100 | """ |
|
100 | """ | |
101 | Create new user |
|
101 | Create new user | |
102 |
|
102 | |||
@@ -111,26 +111,26 b' class ApiController( JSONRPCController )' | |||||
111 | :param ldap_dn: |
|
111 | :param ldap_dn: | |
112 | """ |
|
112 | """ | |
113 |
|
113 | |||
114 |
if self.get_user( |
|
114 | if self.get_user(apiuser, username): | |
115 |
raise JSONRPCError( |
|
115 | raise JSONRPCError("user %s already exist" % username) | |
116 |
|
116 | |||
117 | try: |
|
117 | try: | |
118 |
form_data = dict( |
|
118 | form_data = dict(username = username, | |
119 | password = password, |
|
119 | password = password, | |
120 | active = active, |
|
120 | active = active, | |
121 | admin = admin, |
|
121 | admin = admin, | |
122 | name = firstname, |
|
122 | name = firstname, | |
123 | lastname = lastname, |
|
123 | lastname = lastname, | |
124 | email = email, |
|
124 | email = email, | |
125 |
ldap_dn = ldap_dn |
|
125 | ldap_dn = ldap_dn) | |
126 |
UserModel().create_ldap( |
|
126 | UserModel().create_ldap(username, password, ldap_dn, form_data) | |
127 |
return dict( |
|
127 | return dict(msg = 'created new user %s' % username) | |
128 | except Exception: |
|
128 | except Exception: | |
129 |
log.error( |
|
129 | log.error(traceback.format_exc()) | |
130 |
raise JSONRPCError( |
|
130 | raise JSONRPCError('failed to create user %s' % username) | |
131 |
|
131 | |||
132 |
@HasPermissionAllDecorator( |
|
132 | @HasPermissionAllDecorator('hg.admin') | |
133 |
def get_users_group( |
|
133 | def get_users_group(self, apiuser, group_name): | |
134 | """" |
|
134 | """" | |
135 | Get users group by name |
|
135 | Get users group by name | |
136 |
|
136 | |||
@@ -138,29 +138,29 b' class ApiController( JSONRPCController )' | |||||
138 | :param group_name |
|
138 | :param group_name | |
139 | """ |
|
139 | """ | |
140 |
|
140 | |||
141 |
users_group = UsersGroup.get_by_group_name( |
|
141 | users_group = UsersGroup.get_by_group_name(group_name) | |
142 | if not users_group: |
|
142 | if not users_group: | |
143 | return None |
|
143 | return None | |
144 |
|
144 | |||
145 | members = [] |
|
145 | members = [] | |
146 | for user in users_group.members: |
|
146 | for user in users_group.members: | |
147 | user = user.user |
|
147 | user = user.user | |
148 |
members.append( |
|
148 | members.append(dict(id = user.user_id, | |
149 | username = user.username, |
|
149 | username = user.username, | |
150 | firstname = user.name, |
|
150 | firstname = user.name, | |
151 | lastname = user.lastname, |
|
151 | lastname = user.lastname, | |
152 | email = user.email, |
|
152 | email = user.email, | |
153 | active = user.active, |
|
153 | active = user.active, | |
154 | admin = user.admin, |
|
154 | admin = user.admin, | |
155 |
ldap = user.ldap_dn |
|
155 | ldap = user.ldap_dn)) | |
156 |
|
156 | |||
157 |
return dict( |
|
157 | return dict(id = users_group.users_group_id, | |
158 | name = users_group.users_group_name, |
|
158 | name = users_group.users_group_name, | |
159 | active = users_group.users_group_active, |
|
159 | active = users_group.users_group_active, | |
160 |
members = members |
|
160 | members = members) | |
161 |
|
161 | |||
162 |
@HasPermissionAllDecorator( |
|
162 | @HasPermissionAllDecorator('hg.admin') | |
163 |
def get_users_groups( |
|
163 | def get_users_groups(self, apiuser): | |
164 | """" |
|
164 | """" | |
165 | Get all users groups |
|
165 | Get all users groups | |
166 |
|
166 | |||
@@ -172,23 +172,23 b' class ApiController( JSONRPCController )' | |||||
172 | members = [] |
|
172 | members = [] | |
173 | for user in users_group.members: |
|
173 | for user in users_group.members: | |
174 | user = user.user |
|
174 | user = user.user | |
175 |
members.append( |
|
175 | members.append(dict(id = user.user_id, | |
176 | username = user.username, |
|
176 | username = user.username, | |
177 | firstname = user.name, |
|
177 | firstname = user.name, | |
178 | lastname = user.lastname, |
|
178 | lastname = user.lastname, | |
179 | email = user.email, |
|
179 | email = user.email, | |
180 | active = user.active, |
|
180 | active = user.active, | |
181 | admin = user.admin, |
|
181 | admin = user.admin, | |
182 |
ldap = user.ldap_dn |
|
182 | ldap = user.ldap_dn)) | |
183 |
|
183 | |||
184 |
result.append( |
|
184 | result.append(dict(id = users_group.users_group_id, | |
185 | name = users_group.users_group_name, |
|
185 | name = users_group.users_group_name, | |
186 | active = users_group.users_group_active, |
|
186 | active = users_group.users_group_active, | |
187 |
members = members |
|
187 | members = members)) | |
188 | return result |
|
188 | return result | |
189 |
|
189 | |||
190 |
@HasPermissionAllDecorator( |
|
190 | @HasPermissionAllDecorator('hg.admin') | |
191 |
def create_users_group( |
|
191 | def create_users_group(self, apiuser, name, active = True): | |
192 | """ |
|
192 | """ | |
193 | Creates an new usergroup |
|
193 | Creates an new usergroup | |
194 |
|
194 | |||
@@ -196,21 +196,21 b' class ApiController( JSONRPCController )' | |||||
196 | :param active: |
|
196 | :param active: | |
197 | """ |
|
197 | """ | |
198 |
|
198 | |||
199 |
if self.get_users_group( |
|
199 | if self.get_users_group(apiuser, name): | |
200 |
raise JSONRPCError( |
|
200 | raise JSONRPCError("users group %s already exist" % name) | |
201 |
|
201 | |||
202 | try: |
|
202 | try: | |
203 |
form_data = dict( |
|
203 | form_data = dict(users_group_name = name, | |
204 |
users_group_active = active |
|
204 | users_group_active = active) | |
205 |
ug = UsersGroup.create( |
|
205 | ug = UsersGroup.create(form_data) | |
206 |
return dict( |
|
206 | return dict(id = ug.users_group_id, | |
207 |
msg = 'created new users group %s' % name |
|
207 | msg = 'created new users group %s' % name) | |
208 | except Exception: |
|
208 | except Exception: | |
209 |
log.error( |
|
209 | log.error(traceback.format_exc()) | |
210 |
raise JSONRPCError( |
|
210 | raise JSONRPCError('failed to create group %s' % name) | |
211 |
|
211 | |||
212 |
@HasPermissionAllDecorator( |
|
212 | @HasPermissionAllDecorator('hg.admin') | |
213 |
def add_user_to_users_group( |
|
213 | def add_user_to_users_group(self, apiuser, group_name, user_name): | |
214 | """" |
|
214 | """" | |
215 | Add a user to a group |
|
215 | Add a user to a group | |
216 |
|
216 | |||
@@ -220,25 +220,25 b' class ApiController( JSONRPCController )' | |||||
220 | """ |
|
220 | """ | |
221 |
|
221 | |||
222 | try: |
|
222 | try: | |
223 |
users_group = UsersGroup.get_by_group_name( |
|
223 | users_group = UsersGroup.get_by_group_name(group_name) | |
224 | if not users_group: |
|
224 | if not users_group: | |
225 |
raise JSONRPCError( |
|
225 | raise JSONRPCError('unknown users group %s' % group_name) | |
226 |
|
226 | |||
227 | try: |
|
227 | try: | |
228 |
user = User.get_by_username( |
|
228 | user = User.get_by_username(user_name) | |
229 | except NoResultFound: |
|
229 | except NoResultFound: | |
230 |
raise JSONRPCError( |
|
230 | raise JSONRPCError('unknown user %s' % user_name) | |
231 |
|
231 | |||
232 |
ugm = UsersGroupModel().add_user_to_group( |
|
232 | ugm = UsersGroupModel().add_user_to_group(users_group, user) | |
233 |
|
233 | |||
234 |
return dict( |
|
234 | return dict(id = ugm.users_group_member_id, | |
235 |
msg = 'created new users group member' |
|
235 | msg = 'created new users group member') | |
236 | except Exception: |
|
236 | except Exception: | |
237 |
log.error( |
|
237 | log.error(traceback.format_exc()) | |
238 |
raise JSONRPCError( |
|
238 | raise JSONRPCError('failed to create users group member') | |
239 |
|
239 | |||
240 |
@HasPermissionAnyDecorator( |
|
240 | @HasPermissionAnyDecorator('hg.admin') | |
241 |
def get_repo( |
|
241 | def get_repo(self, apiuser, repo_name): | |
242 | """" |
|
242 | """" | |
243 | Get repository by name |
|
243 | Get repository by name | |
244 |
|
244 | |||
@@ -247,7 +247,7 b' class ApiController( JSONRPCController )' | |||||
247 | """ |
|
247 | """ | |
248 |
|
248 | |||
249 | try: |
|
249 | try: | |
250 |
repo = Repository.get_by_repo_name( |
|
250 | repo = Repository.get_by_repo_name(repo_name) | |
251 | except NoResultFound: |
|
251 | except NoResultFound: | |
252 | return None |
|
252 | return None | |
253 |
|
253 | |||
@@ -255,7 +255,7 b' class ApiController( JSONRPCController )' | |||||
255 | for user in repo.repo_to_perm: |
|
255 | for user in repo.repo_to_perm: | |
256 | perm = user.permission.permission_name |
|
256 | perm = user.permission.permission_name | |
257 | user = user.user |
|
257 | user = user.user | |
258 |
members.append( |
|
258 | members.append(dict(type_ = "user", | |
259 | id = user.user_id, |
|
259 | id = user.user_id, | |
260 | username = user.username, |
|
260 | username = user.username, | |
261 | firstname = user.name, |
|
261 | firstname = user.name, | |
@@ -264,24 +264,24 b' class ApiController( JSONRPCController )' | |||||
264 | active = user.active, |
|
264 | active = user.active, | |
265 | admin = user.admin, |
|
265 | admin = user.admin, | |
266 | ldap = user.ldap_dn, |
|
266 | ldap = user.ldap_dn, | |
267 |
permission = perm |
|
267 | permission = perm)) | |
268 | for users_group in repo.users_group_to_perm: |
|
268 | for users_group in repo.users_group_to_perm: | |
269 | perm = users_group.permission.permission_name |
|
269 | perm = users_group.permission.permission_name | |
270 | users_group = users_group.users_group |
|
270 | users_group = users_group.users_group | |
271 |
members.append( |
|
271 | members.append(dict(type_ = "users_group", | |
272 | id = users_group.users_group_id, |
|
272 | id = users_group.users_group_id, | |
273 | name = users_group.users_group_name, |
|
273 | name = users_group.users_group_name, | |
274 | active = users_group.users_group_active, |
|
274 | active = users_group.users_group_active, | |
275 |
permission = perm |
|
275 | permission = perm)) | |
276 |
|
276 | |||
277 |
return dict( |
|
277 | return dict(id = repo.repo_id, | |
278 | name = repo.repo_name, |
|
278 | name = repo.repo_name, | |
279 | type = repo.repo_type, |
|
279 | type = repo.repo_type, | |
280 | description = repo.description, |
|
280 | description = repo.description, | |
281 |
members = members |
|
281 | members = members) | |
282 |
|
282 | |||
283 |
@HasPermissionAnyDecorator( |
|
283 | @HasPermissionAnyDecorator('hg.admin') | |
284 |
def get_repos( |
|
284 | def get_repos(self, apiuser): | |
285 | """" |
|
285 | """" | |
286 | Get all repositories |
|
286 | Get all repositories | |
287 |
|
287 | |||
@@ -290,15 +290,15 b' class ApiController( JSONRPCController )' | |||||
290 |
|
290 | |||
291 | result = [] |
|
291 | result = [] | |
292 | for repository in Repository.getAll(): |
|
292 | for repository in Repository.getAll(): | |
293 |
result.append( |
|
293 | result.append(dict(id = repository.repo_id, | |
294 | name = repository.repo_name, |
|
294 | name = repository.repo_name, | |
295 | type = repository.repo_type, |
|
295 | type = repository.repo_type, | |
296 |
description = repository.description |
|
296 | description = repository.description)) | |
297 | return result |
|
297 | return result | |
298 |
|
298 | |||
299 |
@HasPermissionAnyDecorator( |
|
299 | @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository') | |
300 |
def create_repo( |
|
300 | def create_repo(self, apiuser, name, owner_name, description = '', repo_type = 'hg', \ | |
301 |
private = False |
|
301 | private = False): | |
302 | """ |
|
302 | """ | |
303 | Create a repository |
|
303 | Create a repository | |
304 |
|
304 | |||
@@ -312,38 +312,38 b' class ApiController( JSONRPCController )' | |||||
312 |
|
312 | |||
313 | try: |
|
313 | try: | |
314 | try: |
|
314 | try: | |
315 |
owner = User.get_by_username( |
|
315 | owner = User.get_by_username(owner_name) | |
316 | except NoResultFound: |
|
316 | except NoResultFound: | |
317 |
raise JSONRPCError( |
|
317 | raise JSONRPCError('unknown user %s' % owner) | |
318 |
|
318 | |||
319 |
if self.get_repo( |
|
319 | if self.get_repo(apiuser, name): | |
320 |
raise JSONRPCError( |
|
320 | raise JSONRPCError("repo %s already exist" % name) | |
321 |
|
321 | |||
322 |
groups = name.split( |
|
322 | groups = name.split('/') | |
323 | real_name = groups[-1] |
|
323 | real_name = groups[-1] | |
324 | groups = groups[:-1] |
|
324 | groups = groups[:-1] | |
325 | parent_id = None |
|
325 | parent_id = None | |
326 | for g in groups: |
|
326 | for g in groups: | |
327 |
group = Group.get_by_group_name( |
|
327 | group = Group.get_by_group_name(g) | |
328 | if not group: |
|
328 | if not group: | |
329 |
group = ReposGroupModel().create( |
|
329 | group = ReposGroupModel().create(dict(group_name = g, | |
330 | group_description = '', |
|
330 | group_description = '', | |
331 |
group_parent_id = parent_id |
|
331 | group_parent_id = parent_id)) | |
332 | parent_id = group.group_id |
|
332 | parent_id = group.group_id | |
333 |
|
333 | |||
334 |
RepoModel().create( |
|
334 | RepoModel().create(dict(repo_name = real_name, | |
335 | repo_name_full = name, |
|
335 | repo_name_full = name, | |
336 | description = description, |
|
336 | description = description, | |
337 | private = private, |
|
337 | private = private, | |
338 | repo_type = repo_type, |
|
338 | repo_type = repo_type, | |
339 | repo_group = parent_id, |
|
339 | repo_group = parent_id, | |
340 |
clone_uri = None |
|
340 | clone_uri = None), owner) | |
341 | except Exception: |
|
341 | except Exception: | |
342 |
log.error( |
|
342 | log.error(traceback.format_exc()) | |
343 |
raise JSONRPCError( |
|
343 | raise JSONRPCError('failed to create repository %s' % name) | |
344 |
|
344 | |||
345 |
@HasPermissionAnyDecorator( |
|
345 | @HasPermissionAnyDecorator('hg.admin') | |
346 |
def add_user_to_repo( |
|
346 | def add_user_to_repo(self, apiuser, repo_name, user_name, perm): | |
347 | """ |
|
347 | """ | |
348 | Add permission for a user to a repository |
|
348 | Add permission for a user to a repository | |
349 |
|
349 | |||
@@ -355,18 +355,18 b' class ApiController( JSONRPCController )' | |||||
355 |
|
355 | |||
356 | try: |
|
356 | try: | |
357 | try: |
|
357 | try: | |
358 |
repo = Repository.get_by_repo_name( |
|
358 | repo = Repository.get_by_repo_name(repo_name) | |
359 | except NoResultFound: |
|
359 | except NoResultFound: | |
360 |
raise JSONRPCError( |
|
360 | raise JSONRPCError('unknown repository %s' % repo) | |
361 |
|
361 | |||
362 | try: |
|
362 | try: | |
363 |
user = User.get_by_username( |
|
363 | user = User.get_by_username(user_name) | |
364 | except NoResultFound: |
|
364 | except NoResultFound: | |
365 |
raise JSONRPCError( |
|
365 | raise JSONRPCError('unknown user %s' % user) | |
366 |
|
366 | |||
367 |
RepositoryPermissionModel().update |
|
367 | RepositoryPermissionModel().update_or_delete_user_permission(repo, user, perm) | |
368 | except Exception: |
|
368 | except Exception: | |
369 |
log.error( |
|
369 | log.error(traceback.format_exc()) | |
370 |
raise JSONRPCError( |
|
370 | raise JSONRPCError('failed to edit permission %(repo)s for %(user)s' | |
371 |
% dict( |
|
371 | % dict(user = user_name, repo = repo_name)) | |
372 |
|
372 |
This diff has been collapsed as it changes many lines, (836 lines changed) Show them Hide them | |||||
@@ -51,13 +51,13 b' from rhodecode.model.meta import Base, S' | |||||
51 | from rhodecode.model.caching_query import FromCache |
|
51 | from rhodecode.model.caching_query import FromCache | |
52 |
|
52 | |||
53 |
|
53 | |||
54 |
log = logging.getLogger( |
|
54 | log = logging.getLogger(__name__) | |
55 |
|
55 | |||
56 | #============================================================================== |
|
56 | #============================================================================== | |
57 | # BASE CLASSES |
|
57 | # BASE CLASSES | |
58 | #============================================================================== |
|
58 | #============================================================================== | |
59 |
|
59 | |||
60 |
class ModelSerializer( |
|
60 | class ModelSerializer(json.JSONEncoder): | |
61 | """ |
|
61 | """ | |
62 | Simple Serializer for JSON, |
|
62 | Simple Serializer for JSON, | |
63 |
|
63 | |||
@@ -80,120 +80,120 b' class ModelSerializer( json.JSONEncoder ' | |||||
80 |
|
80 | |||
81 | """ |
|
81 | """ | |
82 |
|
82 | |||
83 |
def default( |
|
83 | def default(self, obj): | |
84 |
|
84 | |||
85 |
if hasattr( |
|
85 | if hasattr(obj, '__json__'): | |
86 | return obj.__json__() |
|
86 | return obj.__json__() | |
87 | else: |
|
87 | else: | |
88 |
return json.JSONEncoder.default( |
|
88 | return json.JSONEncoder.default(self, obj) | |
89 |
|
89 | |||
90 |
class BaseModel( |
|
90 | class BaseModel(object): | |
91 | """Base Model for all classess |
|
91 | """Base Model for all classess | |
92 |
|
92 | |||
93 | """ |
|
93 | """ | |
94 |
|
94 | |||
95 | @classmethod |
|
95 | @classmethod | |
96 |
def _get_keys( |
|
96 | def _get_keys(cls): | |
97 | """return column names for this model """ |
|
97 | """return column names for this model """ | |
98 |
return class_mapper( |
|
98 | return class_mapper(cls).c.keys() | |
99 |
|
99 | |||
100 |
def get_dict( |
|
100 | def get_dict(self): | |
101 | """return dict with keys and values corresponding |
|
101 | """return dict with keys and values corresponding | |
102 | to this model data """ |
|
102 | to this model data """ | |
103 |
|
103 | |||
104 | d = {} |
|
104 | d = {} | |
105 | for k in self._get_keys(): |
|
105 | for k in self._get_keys(): | |
106 |
d[k] = getattr( |
|
106 | d[k] = getattr(self, k) | |
107 | return d |
|
107 | return d | |
108 |
|
108 | |||
109 |
def get_appstruct( |
|
109 | def get_appstruct(self): | |
110 | """return list with keys and values tupples corresponding |
|
110 | """return list with keys and values tupples corresponding | |
111 | to this model data """ |
|
111 | to this model data """ | |
112 |
|
112 | |||
113 | l = [] |
|
113 | l = [] | |
114 | for k in self._get_keys(): |
|
114 | for k in self._get_keys(): | |
115 |
l.append( |
|
115 | l.append((k, getattr(self, k),)) | |
116 | return l |
|
116 | return l | |
117 |
|
117 | |||
118 |
def populate_obj( |
|
118 | def populate_obj(self, populate_dict): | |
119 | """populate model with data from given populate_dict""" |
|
119 | """populate model with data from given populate_dict""" | |
120 |
|
120 | |||
121 | for k in self._get_keys(): |
|
121 | for k in self._get_keys(): | |
122 | if k in populate_dict: |
|
122 | if k in populate_dict: | |
123 |
setattr( |
|
123 | setattr(self, k, populate_dict[k]) | |
124 |
|
124 | |||
125 | @classmethod |
|
125 | @classmethod | |
126 |
def query( |
|
126 | def query(cls): | |
127 |
return Session.query( |
|
127 | return Session.query(cls) | |
128 |
|
128 | |||
129 | @classmethod |
|
129 | @classmethod | |
130 |
def get( |
|
130 | def get(cls, id_): | |
131 |
return cls.query().get( |
|
131 | return cls.query().get(id_) | |
132 |
|
132 | |||
133 | @classmethod |
|
133 | @classmethod | |
134 |
def getAll( |
|
134 | def getAll(cls): | |
135 | return cls.query().all() |
|
135 | return cls.query().all() | |
136 |
|
136 | |||
137 | @classmethod |
|
137 | @classmethod | |
138 |
def delete( |
|
138 | def delete(cls, id_): | |
139 |
obj = cls.query().get( |
|
139 | obj = cls.query().get(id_) | |
140 |
Session.delete( |
|
140 | Session.delete(obj) | |
141 | Session.commit() |
|
141 | Session.commit() | |
142 |
|
142 | |||
143 |
|
143 | |||
144 |
class RhodeCodeSettings( |
|
144 | class RhodeCodeSettings(Base, BaseModel): | |
145 | __tablename__ = 'rhodecode_settings' |
|
145 | __tablename__ = 'rhodecode_settings' | |
146 |
__table_args__ = ( |
|
146 | __table_args__ = (UniqueConstraint('app_settings_name'), {'extend_existing':True}) | |
147 |
app_settings_id = Column( |
|
147 | app_settings_id = Column("app_settings_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
148 |
app_settings_name = Column( |
|
148 | app_settings_name = Column("app_settings_name", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
149 |
_app_settings_value = Column( |
|
149 | _app_settings_value = Column("app_settings_value", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
150 |
|
150 | |||
151 |
def __init__( |
|
151 | def __init__(self, k = '', v = ''): | |
152 | self.app_settings_name = k |
|
152 | self.app_settings_name = k | |
153 | self.app_settings_value = v |
|
153 | self.app_settings_value = v | |
154 |
|
154 | |||
155 |
|
155 | |||
156 |
@validates( |
|
156 | @validates('_app_settings_value') | |
157 |
def validate_settings_value( |
|
157 | def validate_settings_value(self, key, val): | |
158 |
assert type( |
|
158 | assert type(val) == unicode | |
159 | return val |
|
159 | return val | |
160 |
|
160 | |||
161 | @hybrid_property |
|
161 | @hybrid_property | |
162 |
def app_settings_value( |
|
162 | def app_settings_value(self): | |
163 | v = self._app_settings_value |
|
163 | v = self._app_settings_value | |
164 | if v == 'ldap_active': |
|
164 | if v == 'ldap_active': | |
165 |
v = str2bool( |
|
165 | v = str2bool(v) | |
166 | return v |
|
166 | return v | |
167 |
|
167 | |||
168 | @app_settings_value.setter |
|
168 | @app_settings_value.setter | |
169 |
def app_settings_value( |
|
169 | def app_settings_value(self, val): | |
170 | """ |
|
170 | """ | |
171 | Setter that will always make sure we use unicode in app_settings_value |
|
171 | Setter that will always make sure we use unicode in app_settings_value | |
172 |
|
172 | |||
173 | :param val: |
|
173 | :param val: | |
174 | """ |
|
174 | """ | |
175 |
self._app_settings_value = safe_unicode( |
|
175 | self._app_settings_value = safe_unicode(val) | |
176 |
|
176 | |||
177 |
def __repr__( |
|
177 | def __repr__(self): | |
178 |
return "<%s('%s:%s')>" % ( |
|
178 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
179 |
self.app_settings_name, self.app_settings_value |
|
179 | self.app_settings_name, self.app_settings_value) | |
180 |
|
180 | |||
181 |
|
181 | |||
182 | @classmethod |
|
182 | @classmethod | |
183 |
def get_by_name( |
|
183 | def get_by_name(cls, ldap_key): | |
184 | return cls.query()\ |
|
184 | return cls.query()\ | |
185 |
.filter( |
|
185 | .filter(cls.app_settings_name == ldap_key).scalar() | |
186 |
|
186 | |||
187 | @classmethod |
|
187 | @classmethod | |
188 |
def get_app_settings( |
|
188 | def get_app_settings(cls, cache = False): | |
189 |
|
189 | |||
190 | ret = cls.query() |
|
190 | ret = cls.query() | |
191 |
|
191 | |||
192 | if cache: |
|
192 | if cache: | |
193 |
ret = ret.options( |
|
193 | ret = ret.options(FromCache("sql_cache_short", "get_hg_settings")) | |
194 |
|
194 | |||
195 | if not ret: |
|
195 | if not ret: | |
196 |
raise Exception( |
|
196 | raise Exception('Could not get application settings !') | |
197 | settings = {} |
|
197 | settings = {} | |
198 | for each in ret: |
|
198 | for each in ret: | |
199 | settings['rhodecode_' + each.app_settings_name] = \ |
|
199 | settings['rhodecode_' + each.app_settings_name] = \ | |
@@ -202,222 +202,222 b' class RhodeCodeSettings( Base, BaseModel' | |||||
202 | return settings |
|
202 | return settings | |
203 |
|
203 | |||
204 | @classmethod |
|
204 | @classmethod | |
205 |
def get_ldap_settings( |
|
205 | def get_ldap_settings(cls, cache = False): | |
206 | ret = cls.query()\ |
|
206 | ret = cls.query()\ | |
207 |
.filter( |
|
207 | .filter(cls.app_settings_name.startswith('ldap_')).all() | |
208 | fd = {} |
|
208 | fd = {} | |
209 | for row in ret: |
|
209 | for row in ret: | |
210 |
fd.update( |
|
210 | fd.update({row.app_settings_name:row.app_settings_value}) | |
211 |
|
211 | |||
212 | return fd |
|
212 | return fd | |
213 |
|
213 | |||
214 |
|
214 | |||
215 |
class RhodeCodeUi( |
|
215 | class RhodeCodeUi(Base, BaseModel): | |
216 | __tablename__ = 'rhodecode_ui' |
|
216 | __tablename__ = 'rhodecode_ui' | |
217 |
__table_args__ = ( |
|
217 | __table_args__ = (UniqueConstraint('ui_key'), {'extend_existing':True}) | |
218 |
|
218 | |||
219 | HOOK_UPDATE = 'changegroup.update' |
|
219 | HOOK_UPDATE = 'changegroup.update' | |
220 | HOOK_REPO_SIZE = 'changegroup.repo_size' |
|
220 | HOOK_REPO_SIZE = 'changegroup.repo_size' | |
221 | HOOK_PUSH = 'pretxnchangegroup.push_logger' |
|
221 | HOOK_PUSH = 'pretxnchangegroup.push_logger' | |
222 | HOOK_PULL = 'preoutgoing.pull_logger' |
|
222 | HOOK_PULL = 'preoutgoing.pull_logger' | |
223 |
|
223 | |||
224 |
ui_id = Column( |
|
224 | ui_id = Column("ui_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
225 |
ui_section = Column( |
|
225 | ui_section = Column("ui_section", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
226 |
ui_key = Column( |
|
226 | ui_key = Column("ui_key", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
227 |
ui_value = Column( |
|
227 | ui_value = Column("ui_value", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
228 |
ui_active = Column( |
|
228 | ui_active = Column("ui_active", Boolean(), nullable = True, unique = None, default = True) | |
229 |
|
229 | |||
230 |
|
230 | |||
231 | @classmethod |
|
231 | @classmethod | |
232 |
def get_by_key( |
|
232 | def get_by_key(cls, key): | |
233 |
return cls.query().filter( |
|
233 | return cls.query().filter(cls.ui_key == key) | |
234 |
|
234 | |||
235 |
|
235 | |||
236 | @classmethod |
|
236 | @classmethod | |
237 |
def get_builtin_hooks( |
|
237 | def get_builtin_hooks(cls): | |
238 | q = cls.query() |
|
238 | q = cls.query() | |
239 |
q = q.filter( |
|
239 | q = q.filter(cls.ui_key.in_([cls.HOOK_UPDATE, | |
240 | cls.HOOK_REPO_SIZE, |
|
240 | cls.HOOK_REPO_SIZE, | |
241 |
cls.HOOK_PUSH, cls.HOOK_PULL] |
|
241 | cls.HOOK_PUSH, cls.HOOK_PULL])) | |
242 | return q.all() |
|
242 | return q.all() | |
243 |
|
243 | |||
244 | @classmethod |
|
244 | @classmethod | |
245 |
def get_custom_hooks( |
|
245 | def get_custom_hooks(cls): | |
246 | q = cls.query() |
|
246 | q = cls.query() | |
247 |
q = q.filter( |
|
247 | q = q.filter(~cls.ui_key.in_([cls.HOOK_UPDATE, | |
248 | cls.HOOK_REPO_SIZE, |
|
248 | cls.HOOK_REPO_SIZE, | |
249 |
cls.HOOK_PUSH, cls.HOOK_PULL] |
|
249 | cls.HOOK_PUSH, cls.HOOK_PULL])) | |
250 |
q = q.filter( |
|
250 | q = q.filter(cls.ui_section == 'hooks') | |
251 | return q.all() |
|
251 | return q.all() | |
252 |
|
252 | |||
253 | @classmethod |
|
253 | @classmethod | |
254 |
def create_or_update_hook( |
|
254 | def create_or_update_hook(cls, key, val): | |
255 |
new_ui = cls.get_by_key( |
|
255 | new_ui = cls.get_by_key(key).scalar() or cls() | |
256 | new_ui.ui_section = 'hooks' |
|
256 | new_ui.ui_section = 'hooks' | |
257 | new_ui.ui_active = True |
|
257 | new_ui.ui_active = True | |
258 | new_ui.ui_key = key |
|
258 | new_ui.ui_key = key | |
259 | new_ui.ui_value = val |
|
259 | new_ui.ui_value = val | |
260 |
|
260 | |||
261 |
Session.add( |
|
261 | Session.add(new_ui) | |
262 | Session.commit() |
|
262 | Session.commit() | |
263 |
|
263 | |||
264 |
|
264 | |||
265 |
class User( |
|
265 | class User(Base, BaseModel): | |
266 | __tablename__ = 'users' |
|
266 | __tablename__ = 'users' | |
267 |
__table_args__ = ( |
|
267 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'extend_existing':True}) | |
268 |
user_id = Column( |
|
268 | user_id = Column("user_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
269 |
username = Column( |
|
269 | username = Column("username", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
270 |
password = Column( |
|
270 | password = Column("password", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
271 |
active = Column( |
|
271 | active = Column("active", Boolean(), nullable = True, unique = None, default = None) | |
272 |
admin = Column( |
|
272 | admin = Column("admin", Boolean(), nullable = True, unique = None, default = False) | |
273 |
name = Column( |
|
273 | name = Column("name", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
274 |
lastname = Column( |
|
274 | lastname = Column("lastname", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
275 |
email = Column( |
|
275 | email = Column("email", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
276 |
last_login = Column( |
|
276 | last_login = Column("last_login", DateTime(timezone = False), nullable = True, unique = None, default = None) | |
277 |
ldap_dn = Column( |
|
277 | ldap_dn = Column("ldap_dn", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
278 |
api_key = Column( |
|
278 | api_key = Column("api_key", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
279 |
|
279 | |||
280 |
user_log = relationship( |
|
280 | user_log = relationship('UserLog', cascade = 'all') | |
281 |
user_perms = relationship( |
|
281 | user_perms = relationship('UserToPerm', primaryjoin = "User.user_id==UserToPerm.user_id", cascade = 'all') | |
282 |
|
282 | |||
283 |
repositories = relationship( |
|
283 | repositories = relationship('Repository') | |
284 |
user_followers = relationship( |
|
284 | user_followers = relationship('UserFollowing', primaryjoin = 'UserFollowing.follows_user_id==User.user_id', cascade = 'all') | |
285 |
repo_to_perm = relationship( |
|
285 | repo_to_perm = relationship('RepoToPerm', primaryjoin = 'RepoToPerm.user_id==User.user_id', cascade = 'all') | |
286 |
|
286 | |||
287 |
group_member = relationship( |
|
287 | group_member = relationship('UsersGroupMember', cascade = 'all') | |
288 |
|
288 | |||
289 | @property |
|
289 | @property | |
290 |
def full_contact( |
|
290 | def full_contact(self): | |
291 |
return '%s %s <%s>' % ( |
|
291 | return '%s %s <%s>' % (self.name, self.lastname, self.email) | |
292 |
|
292 | |||
293 | @property |
|
293 | @property | |
294 |
def short_contact( |
|
294 | def short_contact(self): | |
295 |
return '%s %s' % ( |
|
295 | return '%s %s' % (self.name, self.lastname) | |
296 |
|
296 | |||
297 | @property |
|
297 | @property | |
298 |
def is_admin( |
|
298 | def is_admin(self): | |
299 | return self.admin |
|
299 | return self.admin | |
300 |
|
300 | |||
301 |
def __repr__( |
|
301 | def __repr__(self): | |
302 | try: |
|
302 | try: | |
303 |
return "<%s('id:%s:%s')>" % ( |
|
303 | return "<%s('id:%s:%s')>" % (self.__class__.__name__, | |
304 |
self.user_id, self.username |
|
304 | self.user_id, self.username) | |
305 | except: |
|
305 | except: | |
306 | return self.__class__.__name__ |
|
306 | return self.__class__.__name__ | |
307 |
|
307 | |||
308 | @classmethod |
|
308 | @classmethod | |
309 |
def get_by_username( |
|
309 | def get_by_username(cls, username, case_insensitive = False): | |
310 | if case_insensitive: |
|
310 | if case_insensitive: | |
311 |
return Session.query( |
|
311 | return Session.query(cls).filter(cls.username.ilike(username)).scalar() | |
312 | else: |
|
312 | else: | |
313 |
return Session.query( |
|
313 | return Session.query(cls).filter(cls.username == username).scalar() | |
314 |
|
314 | |||
315 | @classmethod |
|
315 | @classmethod | |
316 |
def get_by_api_key( |
|
316 | def get_by_api_key(cls, api_key): | |
317 |
return cls.query().filter( |
|
317 | return cls.query().filter(cls.api_key == api_key).one() | |
318 |
|
318 | |||
319 |
def update_lastlogin( |
|
319 | def update_lastlogin(self): | |
320 | """Update user lastlogin""" |
|
320 | """Update user lastlogin""" | |
321 |
|
321 | |||
322 | self.last_login = datetime.datetime.now() |
|
322 | self.last_login = datetime.datetime.now() | |
323 |
Session.add( |
|
323 | Session.add(self) | |
324 | Session.commit() |
|
324 | Session.commit() | |
325 |
log.debug( |
|
325 | log.debug('updated user %s lastlogin', self.username) | |
326 |
|
326 | |||
327 | @classmethod |
|
327 | @classmethod | |
328 |
def create( |
|
328 | def create(cls, form_data): | |
329 | from rhodecode.lib.auth import get_crypt_password |
|
329 | from rhodecode.lib.auth import get_crypt_password | |
330 |
|
330 | |||
331 | try: |
|
331 | try: | |
332 | new_user = cls() |
|
332 | new_user = cls() | |
333 | for k, v in form_data.items(): |
|
333 | for k, v in form_data.items(): | |
334 | if k == 'password': |
|
334 | if k == 'password': | |
335 |
v = get_crypt_password( |
|
335 | v = get_crypt_password(v) | |
336 |
setattr( |
|
336 | setattr(new_user, k, v) | |
337 |
|
337 | |||
338 |
new_user.api_key = generate_api_key( |
|
338 | new_user.api_key = generate_api_key(form_data['username']) | |
339 |
Session.add( |
|
339 | Session.add(new_user) | |
340 | Session.commit() |
|
340 | Session.commit() | |
341 | return new_user |
|
341 | return new_user | |
342 | except: |
|
342 | except: | |
343 |
log.error( |
|
343 | log.error(traceback.format_exc()) | |
344 | Session.rollback() |
|
344 | Session.rollback() | |
345 | raise |
|
345 | raise | |
346 |
|
346 | |||
347 |
class UserLog( |
|
347 | class UserLog(Base, BaseModel): | |
348 | __tablename__ = 'user_logs' |
|
348 | __tablename__ = 'user_logs' | |
349 | __table_args__ = {'extend_existing':True} |
|
349 | __table_args__ = {'extend_existing':True} | |
350 |
user_log_id = Column( |
|
350 | user_log_id = Column("user_log_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
351 |
user_id = Column( |
|
351 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable = False, unique = None, default = None) | |
352 |
repository_id = Column( |
|
352 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable = False, unique = None, default = None) | |
353 |
repository_name = Column( |
|
353 | repository_name = Column("repository_name", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
354 |
user_ip = Column( |
|
354 | user_ip = Column("user_ip", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
355 |
action = Column( |
|
355 | action = Column("action", UnicodeText(length = 1200000, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
356 |
action_date = Column( |
|
356 | action_date = Column("action_date", DateTime(timezone = False), nullable = True, unique = None, default = None) | |
357 |
|
357 | |||
358 | @property |
|
358 | @property | |
359 |
def action_as_day( |
|
359 | def action_as_day(self): | |
360 |
return date( |
|
360 | return date(*self.action_date.timetuple()[:3]) | |
361 |
|
361 | |||
362 |
user = relationship( |
|
362 | user = relationship('User') | |
363 |
repository = relationship( |
|
363 | repository = relationship('Repository') | |
364 |
|
364 | |||
365 |
|
365 | |||
366 |
class UsersGroup( |
|
366 | class UsersGroup(Base, BaseModel): | |
367 | __tablename__ = 'users_groups' |
|
367 | __tablename__ = 'users_groups' | |
368 | __table_args__ = {'extend_existing':True} |
|
368 | __table_args__ = {'extend_existing':True} | |
369 |
|
369 | |||
370 |
users_group_id = Column( |
|
370 | users_group_id = Column("users_group_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
371 |
users_group_name = Column( |
|
371 | users_group_name = Column("users_group_name", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = False, unique = True, default = None) | |
372 |
users_group_active = Column( |
|
372 | users_group_active = Column("users_group_active", Boolean(), nullable = True, unique = None, default = None) | |
373 |
|
373 | |||
374 |
members = relationship( |
|
374 | members = relationship('UsersGroupMember', cascade = "all, delete, delete-orphan", lazy = "joined") | |
375 |
|
375 | |||
376 |
def __repr__( |
|
376 | def __repr__(self): | |
377 |
return '<userGroup(%s)>' % ( |
|
377 | return '<userGroup(%s)>' % (self.users_group_name) | |
378 |
|
378 | |||
379 | @classmethod |
|
379 | @classmethod | |
380 |
def get_by_group_name( |
|
380 | def get_by_group_name(cls, group_name, cache = False, case_insensitive = False): | |
381 | if case_insensitive: |
|
381 | if case_insensitive: | |
382 | gr = cls.query()\ |
|
382 | gr = cls.query()\ | |
383 |
.filter( |
|
383 | .filter(cls.users_group_name.ilike(group_name)) | |
384 | else: |
|
384 | else: | |
385 | gr = cls.query()\ |
|
385 | gr = cls.query()\ | |
386 |
.filter( |
|
386 | .filter(cls.users_group_name == group_name) | |
387 | if cache: |
|
387 | if cache: | |
388 |
gr = gr.options( |
|
388 | gr = gr.options(FromCache("sql_cache_short", | |
389 |
"get_user_%s" % group_name |
|
389 | "get_user_%s" % group_name)) | |
390 | return gr.scalar() |
|
390 | return gr.scalar() | |
391 |
|
391 | |||
392 |
|
392 | |||
393 | @classmethod |
|
393 | @classmethod | |
394 |
def get( |
|
394 | def get(cls, users_group_id, cache = False): | |
395 | users_group = cls.query() |
|
395 | users_group = cls.query() | |
396 | if cache: |
|
396 | if cache: | |
397 |
users_group = users_group.options( |
|
397 | users_group = users_group.options(FromCache("sql_cache_short", | |
398 |
"get_users_group_%s" % users_group_id |
|
398 | "get_users_group_%s" % users_group_id)) | |
399 |
return users_group.get( |
|
399 | return users_group.get(users_group_id) | |
400 |
|
400 | |||
401 | @classmethod |
|
401 | @classmethod | |
402 |
def create( |
|
402 | def create(cls, form_data): | |
403 | try: |
|
403 | try: | |
404 | new_users_group = cls() |
|
404 | new_users_group = cls() | |
405 | for k, v in form_data.items(): |
|
405 | for k, v in form_data.items(): | |
406 |
setattr( |
|
406 | setattr(new_users_group, k, v) | |
407 |
|
407 | |||
408 |
Session.add( |
|
408 | Session.add(new_users_group) | |
409 | Session.commit() |
|
409 | Session.commit() | |
410 | return new_users_group |
|
410 | return new_users_group | |
411 | except: |
|
411 | except: | |
412 |
log.error( |
|
412 | log.error(traceback.format_exc()) | |
413 | Session.rollback() |
|
413 | Session.rollback() | |
414 | raise |
|
414 | raise | |
415 |
|
415 | |||
416 | @classmethod |
|
416 | @classmethod | |
417 |
def update( |
|
417 | def update(cls, users_group_id, form_data): | |
418 |
|
418 | |||
419 | try: |
|
419 | try: | |
420 |
users_group = cls.get( |
|
420 | users_group = cls.get(users_group_id, cache = False) | |
421 |
|
421 | |||
422 | for k, v in form_data.items(): |
|
422 | for k, v in form_data.items(): | |
423 | if k == 'users_group_members': |
|
423 | if k == 'users_group_members': | |
@@ -427,181 +427,181 b' class UsersGroup( Base, BaseModel ):' | |||||
427 | if v: |
|
427 | if v: | |
428 | v = [v] if isinstance(v, basestring) else v |
|
428 | v = [v] if isinstance(v, basestring) else v | |
429 | for u_id in set(v): |
|
429 | for u_id in set(v): | |
430 |
member = UsersGroupMember( |
|
430 | member = UsersGroupMember(users_group_id, u_id) | |
431 |
members_list.append( |
|
431 | members_list.append(member) | |
432 |
setattr( |
|
432 | setattr(users_group, 'members', members_list) | |
433 |
setattr( |
|
433 | setattr(users_group, k, v) | |
434 |
|
434 | |||
435 |
Session.add( |
|
435 | Session.add(users_group) | |
436 | Session.commit() |
|
436 | Session.commit() | |
437 | except: |
|
437 | except: | |
438 |
log.error( |
|
438 | log.error(traceback.format_exc()) | |
439 | Session.rollback() |
|
439 | Session.rollback() | |
440 | raise |
|
440 | raise | |
441 |
|
441 | |||
442 | @classmethod |
|
442 | @classmethod | |
443 |
def delete( |
|
443 | def delete(cls, users_group_id): | |
444 | try: |
|
444 | try: | |
445 |
|
445 | |||
446 | # check if this group is not assigned to repo |
|
446 | # check if this group is not assigned to repo | |
447 | assigned_groups = UsersGroupRepoToPerm.query()\ |
|
447 | assigned_groups = UsersGroupRepoToPerm.query()\ | |
448 |
.filter( |
|
448 | .filter(UsersGroupRepoToPerm.users_group_id == | |
449 |
users_group_id |
|
449 | users_group_id).all() | |
450 |
|
450 | |||
451 | if assigned_groups: |
|
451 | if assigned_groups: | |
452 |
raise UsersGroupsAssignedException( |
|
452 | raise UsersGroupsAssignedException('Group assigned to %s' % | |
453 |
assigned_groups |
|
453 | assigned_groups) | |
454 |
|
454 | |||
455 |
users_group = cls.get( |
|
455 | users_group = cls.get(users_group_id, cache = False) | |
456 |
Session.delete( |
|
456 | Session.delete(users_group) | |
457 | Session.commit() |
|
457 | Session.commit() | |
458 | except: |
|
458 | except: | |
459 |
log.error( |
|
459 | log.error(traceback.format_exc()) | |
460 | Session.rollback() |
|
460 | Session.rollback() | |
461 | raise |
|
461 | raise | |
462 |
|
462 | |||
463 |
class UsersGroupMember( |
|
463 | class UsersGroupMember(Base, BaseModel): | |
464 | __tablename__ = 'users_groups_members' |
|
464 | __tablename__ = 'users_groups_members' | |
465 | __table_args__ = {'extend_existing':True} |
|
465 | __table_args__ = {'extend_existing':True} | |
466 |
|
466 | |||
467 |
users_group_member_id = Column( |
|
467 | users_group_member_id = Column("users_group_member_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
468 |
users_group_id = Column( |
|
468 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable = False, unique = None, default = None) | |
469 |
user_id = Column( |
|
469 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable = False, unique = None, default = None) | |
470 |
|
470 | |||
471 |
user = relationship( |
|
471 | user = relationship('User', lazy = 'joined') | |
472 |
users_group = relationship( |
|
472 | users_group = relationship('UsersGroup') | |
473 |
|
473 | |||
474 |
def __init__( |
|
474 | def __init__(self, gr_id = '', u_id = ''): | |
475 | self.users_group_id = gr_id |
|
475 | self.users_group_id = gr_id | |
476 | self.user_id = u_id |
|
476 | self.user_id = u_id | |
477 |
|
477 | |||
478 | @staticmethod |
|
478 | @staticmethod | |
479 |
def add_user_to_group( |
|
479 | def add_user_to_group(group, user): | |
480 | ugm = UsersGroupMember() |
|
480 | ugm = UsersGroupMember() | |
481 | ugm.users_group = group |
|
481 | ugm.users_group = group | |
482 | ugm.user = user |
|
482 | ugm.user = user | |
483 |
Session.add( |
|
483 | Session.add(ugm) | |
484 | Session.commit() |
|
484 | Session.commit() | |
485 | return ugm |
|
485 | return ugm | |
486 |
|
486 | |||
487 |
class Repository( |
|
487 | class Repository(Base, BaseModel): | |
488 | __tablename__ = 'repositories' |
|
488 | __tablename__ = 'repositories' | |
489 |
__table_args__ = ( |
|
489 | __table_args__ = (UniqueConstraint('repo_name'), {'extend_existing':True},) | |
490 |
|
490 | |||
491 |
repo_id = Column( |
|
491 | repo_id = Column("repo_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
492 |
repo_name = Column( |
|
492 | repo_name = Column("repo_name", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = False, unique = True, default = None) | |
493 |
clone_uri = Column( |
|
493 | clone_uri = Column("clone_uri", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = False, default = None) | |
494 |
repo_type = Column( |
|
494 | repo_type = Column("repo_type", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = False, unique = False, default = 'hg') | |
495 |
user_id = Column( |
|
495 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable = False, unique = False, default = None) | |
496 |
private = Column( |
|
496 | private = Column("private", Boolean(), nullable = True, unique = None, default = None) | |
497 |
enable_statistics = Column( |
|
497 | enable_statistics = Column("statistics", Boolean(), nullable = True, unique = None, default = True) | |
498 |
enable_downloads = Column( |
|
498 | enable_downloads = Column("downloads", Boolean(), nullable = True, unique = None, default = True) | |
499 |
description = Column( |
|
499 | description = Column("description", String(length = 10000, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
500 |
created_on = Column( |
|
500 | created_on = Column('created_on', DateTime(timezone = False), nullable = True, unique = None, default = datetime.datetime.now) | |
501 |
|
501 | |||
502 |
fork_id = Column( |
|
502 | fork_id = Column("fork_id", Integer(), ForeignKey('repositories.repo_id'), nullable = True, unique = False, default = None) | |
503 |
group_id = Column( |
|
503 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable = True, unique = False, default = None) | |
504 |
|
504 | |||
505 |
|
505 | |||
506 |
user = relationship( |
|
506 | user = relationship('User') | |
507 |
fork = relationship( |
|
507 | fork = relationship('Repository', remote_side = repo_id) | |
508 |
group = relationship( |
|
508 | group = relationship('Group') | |
509 |
repo_to_perm = relationship( |
|
509 | repo_to_perm = relationship('RepoToPerm', cascade = 'all', order_by = 'RepoToPerm.repo_to_perm_id') | |
510 |
users_group_to_perm = relationship( |
|
510 | users_group_to_perm = relationship('UsersGroupRepoToPerm', cascade = 'all') | |
511 |
stats = relationship( |
|
511 | stats = relationship('Statistics', cascade = 'all', uselist = False) | |
512 |
|
512 | |||
513 |
followers = relationship( |
|
513 | followers = relationship('UserFollowing', primaryjoin = 'UserFollowing.follows_repo_id==Repository.repo_id', cascade = 'all') | |
514 |
|
514 | |||
515 |
logs = relationship( |
|
515 | logs = relationship('UserLog', cascade = 'all') | |
516 |
|
516 | |||
517 |
def __repr__( |
|
517 | def __repr__(self): | |
518 |
return "<%s('%s:%s')>" % ( |
|
518 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
519 |
self.repo_id, self.repo_name |
|
519 | self.repo_id, self.repo_name) | |
520 |
|
520 | |||
521 | @classmethod |
|
521 | @classmethod | |
522 |
def url_sep( |
|
522 | def url_sep(cls): | |
523 | return '/' |
|
523 | return '/' | |
524 |
|
524 | |||
525 | @classmethod |
|
525 | @classmethod | |
526 |
def get_by_repo_name( |
|
526 | def get_by_repo_name(cls, repo_name): | |
527 |
q = Session.query( |
|
527 | q = Session.query(cls).filter(cls.repo_name == repo_name) | |
528 |
q = q.options( |
|
528 | q = q.options(joinedload(Repository.fork))\ | |
529 |
.options( |
|
529 | .options(joinedload(Repository.user))\ | |
530 |
.options( |
|
530 | .options(joinedload(Repository.group)) | |
531 | return q.one() |
|
531 | return q.one() | |
532 |
|
532 | |||
533 | @classmethod |
|
533 | @classmethod | |
534 |
def get_repo_forks( |
|
534 | def get_repo_forks(cls, repo_id): | |
535 |
return cls.query().filter( |
|
535 | return cls.query().filter(Repository.fork_id == repo_id) | |
536 |
|
536 | |||
537 | @classmethod |
|
537 | @classmethod | |
538 |
def base_path( |
|
538 | def base_path(cls): | |
539 | """ |
|
539 | """ | |
540 | Returns base path when all repos are stored |
|
540 | Returns base path when all repos are stored | |
541 |
|
541 | |||
542 | :param cls: |
|
542 | :param cls: | |
543 | """ |
|
543 | """ | |
544 |
q = Session.query( |
|
544 | q = Session.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == | |
545 |
cls.url_sep() |
|
545 | cls.url_sep()) | |
546 |
q.options( |
|
546 | q.options(FromCache("sql_cache_short", "repository_repo_path")) | |
547 | return q.one().ui_value |
|
547 | return q.one().ui_value | |
548 |
|
548 | |||
549 | @property |
|
549 | @property | |
550 |
def just_name( |
|
550 | def just_name(self): | |
551 |
return self.repo_name.split( |
|
551 | return self.repo_name.split(Repository.url_sep())[-1] | |
552 |
|
552 | |||
553 | @property |
|
553 | @property | |
554 |
def groups_with_parents( |
|
554 | def groups_with_parents(self): | |
555 | groups = [] |
|
555 | groups = [] | |
556 | if self.group is None: |
|
556 | if self.group is None: | |
557 | return groups |
|
557 | return groups | |
558 |
|
558 | |||
559 | cur_gr = self.group |
|
559 | cur_gr = self.group | |
560 |
groups.insert( |
|
560 | groups.insert(0, cur_gr) | |
561 | while 1: |
|
561 | while 1: | |
562 |
gr = getattr( |
|
562 | gr = getattr(cur_gr, 'parent_group', None) | |
563 | cur_gr = cur_gr.parent_group |
|
563 | cur_gr = cur_gr.parent_group | |
564 | if gr is None: |
|
564 | if gr is None: | |
565 | break |
|
565 | break | |
566 |
groups.insert( |
|
566 | groups.insert(0, gr) | |
567 |
|
567 | |||
568 | return groups |
|
568 | return groups | |
569 |
|
569 | |||
570 | @property |
|
570 | @property | |
571 |
def groups_and_repo( |
|
571 | def groups_and_repo(self): | |
572 | return self.groups_with_parents, self.just_name |
|
572 | return self.groups_with_parents, self.just_name | |
573 |
|
573 | |||
574 | @LazyProperty |
|
574 | @LazyProperty | |
575 |
def repo_path( |
|
575 | def repo_path(self): | |
576 | """ |
|
576 | """ | |
577 | Returns base full path for that repository means where it actually |
|
577 | Returns base full path for that repository means where it actually | |
578 | exists on a filesystem |
|
578 | exists on a filesystem | |
579 | """ |
|
579 | """ | |
580 |
q = Session.query( |
|
580 | q = Session.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == | |
581 |
Repository.url_sep() |
|
581 | Repository.url_sep()) | |
582 |
q.options( |
|
582 | q.options(FromCache("sql_cache_short", "repository_repo_path")) | |
583 | return q.one().ui_value |
|
583 | return q.one().ui_value | |
584 |
|
584 | |||
585 | @property |
|
585 | @property | |
586 |
def repo_full_path( |
|
586 | def repo_full_path(self): | |
587 | p = [self.repo_path] |
|
587 | p = [self.repo_path] | |
588 | # we need to split the name by / since this is how we store the |
|
588 | # we need to split the name by / since this is how we store the | |
589 | # names in the database, but that eventually needs to be converted |
|
589 | # names in the database, but that eventually needs to be converted | |
590 | # into a valid system path |
|
590 | # into a valid system path | |
591 |
p += self.repo_name.split( |
|
591 | p += self.repo_name.split(Repository.url_sep()) | |
592 |
return os.path.join( |
|
592 | return os.path.join(*p) | |
593 |
|
593 | |||
594 |
def get_new_name( |
|
594 | def get_new_name(self, repo_name): | |
595 | """ |
|
595 | """ | |
596 | returns new full repository name based on assigned group and new new |
|
596 | returns new full repository name based on assigned group and new new | |
597 |
|
597 | |||
598 | :param group_name: |
|
598 | :param group_name: | |
599 | """ |
|
599 | """ | |
600 | path_prefix = self.group.full_path_splitted if self.group else [] |
|
600 | path_prefix = self.group.full_path_splitted if self.group else [] | |
601 |
return Repository.url_sep().join( |
|
601 | return Repository.url_sep().join(path_prefix + [repo_name]) | |
602 |
|
602 | |||
603 | @property |
|
603 | @property | |
604 |
def _ui( |
|
604 | def _ui(self): | |
605 | """ |
|
605 | """ | |
606 | Creates an db based ui object for this repository |
|
606 | Creates an db based ui object for this repository | |
607 | """ |
|
607 | """ | |
@@ -616,19 +616,19 b' class Repository( Base, BaseModel ):' | |||||
616 |
|
616 | |||
617 |
|
617 | |||
618 | ret = RhodeCodeUi.query()\ |
|
618 | ret = RhodeCodeUi.query()\ | |
619 |
.options( |
|
619 | .options(FromCache("sql_cache_short", "repository_repo_ui")).all() | |
620 |
|
620 | |||
621 | hg_ui = ret |
|
621 | hg_ui = ret | |
622 | for ui_ in hg_ui: |
|
622 | for ui_ in hg_ui: | |
623 | if ui_.ui_active: |
|
623 | if ui_.ui_active: | |
624 |
log.debug( |
|
624 | log.debug('settings ui from db[%s]%s:%s', ui_.ui_section, | |
625 |
ui_.ui_key, ui_.ui_value |
|
625 | ui_.ui_key, ui_.ui_value) | |
626 |
baseui.setconfig( |
|
626 | baseui.setconfig(ui_.ui_section, ui_.ui_key, ui_.ui_value) | |
627 |
|
627 | |||
628 | return baseui |
|
628 | return baseui | |
629 |
|
629 | |||
630 | @classmethod |
|
630 | @classmethod | |
631 |
def is_valid( |
|
631 | def is_valid(cls, repo_name): | |
632 | """ |
|
632 | """ | |
633 | returns True if given repo name is a valid filesystem repository |
|
633 | returns True if given repo name is a valid filesystem repository | |
634 |
|
634 | |||
@@ -637,26 +637,26 b' class Repository( Base, BaseModel ):' | |||||
637 | """ |
|
637 | """ | |
638 | from rhodecode.lib.utils import is_valid_repo |
|
638 | from rhodecode.lib.utils import is_valid_repo | |
639 |
|
639 | |||
640 |
return is_valid_repo( |
|
640 | return is_valid_repo(repo_name, cls.base_path()) | |
641 |
|
641 | |||
642 |
|
642 | |||
643 | #========================================================================== |
|
643 | #========================================================================== | |
644 | # SCM PROPERTIES |
|
644 | # SCM PROPERTIES | |
645 | #========================================================================== |
|
645 | #========================================================================== | |
646 |
|
646 | |||
647 |
def get_changeset( |
|
647 | def get_changeset(self, rev): | |
648 |
return get_changeset_safe( |
|
648 | return get_changeset_safe(self.scm_instance, rev) | |
649 |
|
649 | |||
650 | @property |
|
650 | @property | |
651 |
def tip( |
|
651 | def tip(self): | |
652 |
return self.get_changeset( |
|
652 | return self.get_changeset('tip') | |
653 |
|
653 | |||
654 | @property |
|
654 | @property | |
655 |
def author( |
|
655 | def author(self): | |
656 | return self.tip.author |
|
656 | return self.tip.author | |
657 |
|
657 | |||
658 | @property |
|
658 | @property | |
659 |
def last_change( |
|
659 | def last_change(self): | |
660 | return self.scm_instance.last_change |
|
660 | return self.scm_instance.last_change | |
661 |
|
661 | |||
662 | #========================================================================== |
|
662 | #========================================================================== | |
@@ -664,407 +664,407 b' class Repository( Base, BaseModel ):' | |||||
664 | #========================================================================== |
|
664 | #========================================================================== | |
665 |
|
665 | |||
666 | @property |
|
666 | @property | |
667 |
def invalidate( |
|
667 | def invalidate(self): | |
668 | """ |
|
668 | """ | |
669 | Returns Invalidation object if this repo should be invalidated |
|
669 | Returns Invalidation object if this repo should be invalidated | |
670 | None otherwise. `cache_active = False` means that this cache |
|
670 | None otherwise. `cache_active = False` means that this cache | |
671 | state is not valid and needs to be invalidated |
|
671 | state is not valid and needs to be invalidated | |
672 | """ |
|
672 | """ | |
673 | return CacheInvalidation.query()\ |
|
673 | return CacheInvalidation.query()\ | |
674 |
.filter( |
|
674 | .filter(CacheInvalidation.cache_key == self.repo_name)\ | |
675 |
.filter( |
|
675 | .filter(CacheInvalidation.cache_active == False)\ | |
676 | .scalar() |
|
676 | .scalar() | |
677 |
|
677 | |||
678 |
def set_invalidate( |
|
678 | def set_invalidate(self): | |
679 | """ |
|
679 | """ | |
680 | set a cache for invalidation for this instance |
|
680 | set a cache for invalidation for this instance | |
681 | """ |
|
681 | """ | |
682 | inv = CacheInvalidation.query()\ |
|
682 | inv = CacheInvalidation.query()\ | |
683 |
.filter( |
|
683 | .filter(CacheInvalidation.cache_key == self.repo_name)\ | |
684 | .scalar() |
|
684 | .scalar() | |
685 |
|
685 | |||
686 | if inv is None: |
|
686 | if inv is None: | |
687 |
inv = CacheInvalidation( |
|
687 | inv = CacheInvalidation(self.repo_name) | |
688 | inv.cache_active = True |
|
688 | inv.cache_active = True | |
689 |
Session.add( |
|
689 | Session.add(inv) | |
690 | Session.commit() |
|
690 | Session.commit() | |
691 |
|
691 | |||
692 | @LazyProperty |
|
692 | @LazyProperty | |
693 |
def scm_instance( |
|
693 | def scm_instance(self): | |
694 | return self.__get_instance() |
|
694 | return self.__get_instance() | |
695 |
|
695 | |||
696 | @property |
|
696 | @property | |
697 |
def scm_instance_cached( |
|
697 | def scm_instance_cached(self): | |
698 |
@cache_region( |
|
698 | @cache_region('long_term') | |
699 |
def _c( |
|
699 | def _c(repo_name): | |
700 | return self.__get_instance() |
|
700 | return self.__get_instance() | |
701 |
|
701 | |||
702 | # TODO: remove this trick when beaker 1.6 is released |
|
702 | # TODO: remove this trick when beaker 1.6 is released | |
703 | # and have fixed this issue with not supporting unicode keys |
|
703 | # and have fixed this issue with not supporting unicode keys | |
704 |
rn = safe_str( |
|
704 | rn = safe_str(self.repo_name) | |
705 |
|
705 | |||
706 | inv = self.invalidate |
|
706 | inv = self.invalidate | |
707 | if inv is not None: |
|
707 | if inv is not None: | |
708 |
region_invalidate( |
|
708 | region_invalidate(_c, None, rn) | |
709 | # update our cache |
|
709 | # update our cache | |
710 | inv.cache_active = True |
|
710 | inv.cache_active = True | |
711 |
Session.add( |
|
711 | Session.add(inv) | |
712 | Session.commit() |
|
712 | Session.commit() | |
713 |
|
713 | |||
714 |
return _c( |
|
714 | return _c(rn) | |
715 |
|
715 | |||
716 |
def __get_instance( |
|
716 | def __get_instance(self): | |
717 |
|
717 | |||
718 | repo_full_path = self.repo_full_path |
|
718 | repo_full_path = self.repo_full_path | |
719 |
|
719 | |||
720 | try: |
|
720 | try: | |
721 |
alias = get_scm( |
|
721 | alias = get_scm(repo_full_path)[0] | |
722 |
log.debug( |
|
722 | log.debug('Creating instance of %s repository', alias) | |
723 |
backend = get_backend( |
|
723 | backend = get_backend(alias) | |
724 | except VCSError: |
|
724 | except VCSError: | |
725 |
log.error( |
|
725 | log.error(traceback.format_exc()) | |
726 |
log.error( |
|
726 | log.error('Perhaps this repository is in db and not in ' | |
727 | 'filesystem run rescan repositories with ' |
|
727 | 'filesystem run rescan repositories with ' | |
728 |
'"destroy old data " option from admin panel' |
|
728 | '"destroy old data " option from admin panel') | |
729 | return |
|
729 | return | |
730 |
|
730 | |||
731 | if alias == 'hg': |
|
731 | if alias == 'hg': | |
732 |
|
732 | |||
733 |
repo = backend( |
|
733 | repo = backend(safe_str(repo_full_path), create = False, | |
734 |
baseui = self._ui |
|
734 | baseui = self._ui) | |
735 | #skip hidden web repository |
|
735 | #skip hidden web repository | |
736 | if repo._get_hidden(): |
|
736 | if repo._get_hidden(): | |
737 | return |
|
737 | return | |
738 | else: |
|
738 | else: | |
739 |
repo = backend( |
|
739 | repo = backend(repo_full_path, create = False) | |
740 |
|
740 | |||
741 | return repo |
|
741 | return repo | |
742 |
|
742 | |||
743 |
|
743 | |||
744 |
class Group( |
|
744 | class Group(Base, BaseModel): | |
745 | __tablename__ = 'groups' |
|
745 | __tablename__ = 'groups' | |
746 |
__table_args__ = ( |
|
746 | __table_args__ = (UniqueConstraint('group_name', 'group_parent_id'), | |
747 |
CheckConstraint( |
|
747 | CheckConstraint('group_id != group_parent_id'), {'extend_existing':True},) | |
748 | __mapper_args__ = {'order_by':'group_name'} |
|
748 | __mapper_args__ = {'order_by':'group_name'} | |
749 |
|
749 | |||
750 |
group_id = Column( |
|
750 | group_id = Column("group_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
751 |
group_name = Column( |
|
751 | group_name = Column("group_name", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = False, unique = True, default = None) | |
752 |
group_parent_id = Column( |
|
752 | group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable = True, unique = None, default = None) | |
753 |
group_description = Column( |
|
753 | group_description = Column("group_description", String(length = 10000, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
754 |
|
754 | |||
755 |
parent_group = relationship( |
|
755 | parent_group = relationship('Group', remote_side = group_id) | |
756 |
|
756 | |||
757 |
|
757 | |||
758 |
def __init__( |
|
758 | def __init__(self, group_name = '', parent_group = None): | |
759 | self.group_name = group_name |
|
759 | self.group_name = group_name | |
760 | self.parent_group = parent_group |
|
760 | self.parent_group = parent_group | |
761 |
|
761 | |||
762 |
def __repr__( |
|
762 | def __repr__(self): | |
763 |
return "<%s('%s:%s')>" % ( |
|
763 | return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, | |
764 |
self.group_name |
|
764 | self.group_name) | |
765 |
|
765 | |||
766 | @classmethod |
|
766 | @classmethod | |
767 |
def groups_choices( |
|
767 | def groups_choices(cls): | |
768 | from webhelpers.html import literal as _literal |
|
768 | from webhelpers.html import literal as _literal | |
769 |
repo_groups = [( |
|
769 | repo_groups = [('', '')] | |
770 | sep = ' » ' |
|
770 | sep = ' » ' | |
771 |
_name = lambda k: _literal( |
|
771 | _name = lambda k: _literal(sep.join(k)) | |
772 |
|
772 | |||
773 |
repo_groups.extend( |
|
773 | repo_groups.extend([(x.group_id, _name(x.full_path_splitted)) | |
774 |
for x in cls.query().all()] |
|
774 | for x in cls.query().all()]) | |
775 |
|
775 | |||
776 |
repo_groups = sorted( |
|
776 | repo_groups = sorted(repo_groups, key = lambda t: t[1].split(sep)[0]) | |
777 | return repo_groups |
|
777 | return repo_groups | |
778 |
|
778 | |||
779 | @classmethod |
|
779 | @classmethod | |
780 |
def url_sep( |
|
780 | def url_sep(cls): | |
781 | return '/' |
|
781 | return '/' | |
782 |
|
782 | |||
783 | @classmethod |
|
783 | @classmethod | |
784 |
def get_by_group_name( |
|
784 | def get_by_group_name(cls, group_name, cache = False, case_insensitive = False): | |
785 | if case_insensitive: |
|
785 | if case_insensitive: | |
786 | gr = cls.query()\ |
|
786 | gr = cls.query()\ | |
787 |
.filter( |
|
787 | .filter(cls.group_name.ilike(group_name)) | |
788 | else: |
|
788 | else: | |
789 | gr = cls.query()\ |
|
789 | gr = cls.query()\ | |
790 |
.filter( |
|
790 | .filter(cls.group_name == group_name) | |
791 | if cache: |
|
791 | if cache: | |
792 |
gr = gr.options( |
|
792 | gr = gr.options(FromCache("sql_cache_short", | |
793 |
"get_group_%s" % group_name |
|
793 | "get_group_%s" % group_name)) | |
794 | return gr.scalar() |
|
794 | return gr.scalar() | |
795 |
|
795 | |||
796 | @property |
|
796 | @property | |
797 |
def parents( |
|
797 | def parents(self): | |
798 | parents_recursion_limit = 5 |
|
798 | parents_recursion_limit = 5 | |
799 | groups = [] |
|
799 | groups = [] | |
800 | if self.parent_group is None: |
|
800 | if self.parent_group is None: | |
801 | return groups |
|
801 | return groups | |
802 | cur_gr = self.parent_group |
|
802 | cur_gr = self.parent_group | |
803 |
groups.insert( |
|
803 | groups.insert(0, cur_gr) | |
804 | cnt = 0 |
|
804 | cnt = 0 | |
805 | while 1: |
|
805 | while 1: | |
806 | cnt += 1 |
|
806 | cnt += 1 | |
807 |
gr = getattr( |
|
807 | gr = getattr(cur_gr, 'parent_group', None) | |
808 | cur_gr = cur_gr.parent_group |
|
808 | cur_gr = cur_gr.parent_group | |
809 | if gr is None: |
|
809 | if gr is None: | |
810 | break |
|
810 | break | |
811 | if cnt == parents_recursion_limit: |
|
811 | if cnt == parents_recursion_limit: | |
812 | # this will prevent accidental infinit loops |
|
812 | # this will prevent accidental infinit loops | |
813 |
log.error( |
|
813 | log.error('group nested more than %s' % | |
814 |
parents_recursion_limit |
|
814 | parents_recursion_limit) | |
815 | break |
|
815 | break | |
816 |
|
816 | |||
817 |
groups.insert( |
|
817 | groups.insert(0, gr) | |
818 | return groups |
|
818 | return groups | |
819 |
|
819 | |||
820 | @property |
|
820 | @property | |
821 |
def children( |
|
821 | def children(self): | |
822 |
return Group.query().filter( |
|
822 | return Group.query().filter(Group.parent_group == self) | |
823 |
|
823 | |||
824 | @property |
|
824 | @property | |
825 |
def name( |
|
825 | def name(self): | |
826 |
return self.group_name.split( |
|
826 | return self.group_name.split(Group.url_sep())[-1] | |
827 |
|
827 | |||
828 | @property |
|
828 | @property | |
829 |
def full_path( |
|
829 | def full_path(self): | |
830 |
return Group.url_sep().join( |
|
830 | return Group.url_sep().join([g.group_name for g in self.parents] + | |
831 |
[self.group_name] |
|
831 | [self.group_name]) | |
832 |
|
832 | |||
833 | @property |
|
833 | @property | |
834 |
def full_path_splitted( |
|
834 | def full_path_splitted(self): | |
835 |
return self.group_name.split( |
|
835 | return self.group_name.split(Group.url_sep()) | |
836 |
|
836 | |||
837 | @property |
|
837 | @property | |
838 |
def repositories( |
|
838 | def repositories(self): | |
839 |
return Repository.query().filter( |
|
839 | return Repository.query().filter(Repository.group == self) | |
840 |
|
840 | |||
841 | @property |
|
841 | @property | |
842 |
def repositories_recursive_count( |
|
842 | def repositories_recursive_count(self): | |
843 | cnt = self.repositories.count() |
|
843 | cnt = self.repositories.count() | |
844 |
|
844 | |||
845 |
def children_count( |
|
845 | def children_count(group): | |
846 | cnt = 0 |
|
846 | cnt = 0 | |
847 | for child in group.children: |
|
847 | for child in group.children: | |
848 | cnt += child.repositories.count() |
|
848 | cnt += child.repositories.count() | |
849 |
cnt += children_count( |
|
849 | cnt += children_count(child) | |
850 | return cnt |
|
850 | return cnt | |
851 |
|
851 | |||
852 |
return cnt + children_count( |
|
852 | return cnt + children_count(self) | |
853 |
|
853 | |||
854 |
|
854 | |||
855 |
def get_new_name( |
|
855 | def get_new_name(self, group_name): | |
856 | """ |
|
856 | """ | |
857 | returns new full group name based on parent and new name |
|
857 | returns new full group name based on parent and new name | |
858 |
|
858 | |||
859 | :param group_name: |
|
859 | :param group_name: | |
860 | """ |
|
860 | """ | |
861 | path_prefix = self.parent_group.full_path_splitted if self.parent_group else [] |
|
861 | path_prefix = self.parent_group.full_path_splitted if self.parent_group else [] | |
862 |
return Group.url_sep().join( |
|
862 | return Group.url_sep().join(path_prefix + [group_name]) | |
863 |
|
863 | |||
864 |
|
864 | |||
865 |
class Permission( |
|
865 | class Permission(Base, BaseModel): | |
866 | __tablename__ = 'permissions' |
|
866 | __tablename__ = 'permissions' | |
867 | __table_args__ = {'extend_existing':True} |
|
867 | __table_args__ = {'extend_existing':True} | |
868 |
permission_id = Column( |
|
868 | permission_id = Column("permission_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
869 |
permission_name = Column( |
|
869 | permission_name = Column("permission_name", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
870 |
permission_longname = Column( |
|
870 | permission_longname = Column("permission_longname", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
871 |
|
871 | |||
872 |
def __repr__( |
|
872 | def __repr__(self): | |
873 |
return "<%s('%s:%s')>" % ( |
|
873 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
874 |
self.permission_id, self.permission_name |
|
874 | self.permission_id, self.permission_name) | |
875 |
|
875 | |||
876 | @classmethod |
|
876 | @classmethod | |
877 |
def get_by_key( |
|
877 | def get_by_key(cls, key): | |
878 |
return cls.query().filter( |
|
878 | return cls.query().filter(cls.permission_name == key).scalar() | |
879 |
|
879 | |||
880 |
class RepoToPerm( |
|
880 | class RepoToPerm(Base, BaseModel): | |
881 | __tablename__ = 'repo_to_perm' |
|
881 | __tablename__ = 'repo_to_perm' | |
882 |
__table_args__ = ( |
|
882 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'extend_existing':True}) | |
883 |
repo_to_perm_id = Column( |
|
883 | repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
884 |
user_id = Column( |
|
884 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable = False, unique = None, default = None) | |
885 |
permission_id = Column( |
|
885 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable = False, unique = None, default = None) | |
886 |
repository_id = Column( |
|
886 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable = False, unique = None, default = None) | |
887 |
|
887 | |||
888 |
user = relationship( |
|
888 | user = relationship('User') | |
889 |
permission = relationship( |
|
889 | permission = relationship('Permission') | |
890 |
repository = relationship( |
|
890 | repository = relationship('Repository') | |
891 |
|
891 | |||
892 |
class UserToPerm( |
|
892 | class UserToPerm(Base, BaseModel): | |
893 | __tablename__ = 'user_to_perm' |
|
893 | __tablename__ = 'user_to_perm' | |
894 |
__table_args__ = ( |
|
894 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'extend_existing':True}) | |
895 |
user_to_perm_id = Column( |
|
895 | user_to_perm_id = Column("user_to_perm_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
896 |
user_id = Column( |
|
896 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable = False, unique = None, default = None) | |
897 |
permission_id = Column( |
|
897 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable = False, unique = None, default = None) | |
898 |
|
898 | |||
899 |
user = relationship( |
|
899 | user = relationship('User') | |
900 |
permission = relationship( |
|
900 | permission = relationship('Permission') | |
901 |
|
901 | |||
902 | @classmethod |
|
902 | @classmethod | |
903 |
def has_perm( |
|
903 | def has_perm(cls, user_id, perm): | |
904 |
if not isinstance( |
|
904 | if not isinstance(perm, Permission): | |
905 |
raise Exception( |
|
905 | raise Exception('perm needs to be an instance of Permission class') | |
906 |
|
906 | |||
907 |
return cls.query().filter( |
|
907 | return cls.query().filter(cls.user_id == user_id)\ | |
908 |
.filter( |
|
908 | .filter(cls.permission == perm).scalar() is not None | |
909 |
|
909 | |||
910 | @classmethod |
|
910 | @classmethod | |
911 |
def grant_perm( |
|
911 | def grant_perm(cls, user_id, perm): | |
912 |
if not isinstance( |
|
912 | if not isinstance(perm, Permission): | |
913 |
raise Exception( |
|
913 | raise Exception('perm needs to be an instance of Permission class') | |
914 |
|
914 | |||
915 | new = cls() |
|
915 | new = cls() | |
916 | new.user_id = user_id |
|
916 | new.user_id = user_id | |
917 | new.permission = perm |
|
917 | new.permission = perm | |
918 | try: |
|
918 | try: | |
919 |
Session.add( |
|
919 | Session.add(new) | |
920 | Session.commit() |
|
920 | Session.commit() | |
921 | except: |
|
921 | except: | |
922 | Session.rollback() |
|
922 | Session.rollback() | |
923 |
|
923 | |||
924 |
|
924 | |||
925 | @classmethod |
|
925 | @classmethod | |
926 |
def revoke_perm( |
|
926 | def revoke_perm(cls, user_id, perm): | |
927 |
if not isinstance( |
|
927 | if not isinstance(perm, Permission): | |
928 |
raise Exception( |
|
928 | raise Exception('perm needs to be an instance of Permission class') | |
929 |
|
929 | |||
930 | try: |
|
930 | try: | |
931 |
cls.query().filter( |
|
931 | cls.query().filter(cls.user_id == user_id)\ | |
932 |
.filter( |
|
932 | .filter(cls.permission == perm).delete() | |
933 | Session.commit() |
|
933 | Session.commit() | |
934 | except: |
|
934 | except: | |
935 | Session.rollback() |
|
935 | Session.rollback() | |
936 |
|
936 | |||
937 |
class UsersGroupRepoToPerm( |
|
937 | class UsersGroupRepoToPerm(Base, BaseModel): | |
938 | __tablename__ = 'users_group_repo_to_perm' |
|
938 | __tablename__ = 'users_group_repo_to_perm' | |
939 |
__table_args__ = ( |
|
939 | __table_args__ = (UniqueConstraint('repository_id', 'users_group_id', 'permission_id'), {'extend_existing':True}) | |
940 |
users_group_to_perm_id = Column( |
|
940 | users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
941 |
users_group_id = Column( |
|
941 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable = False, unique = None, default = None) | |
942 |
permission_id = Column( |
|
942 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable = False, unique = None, default = None) | |
943 |
repository_id = Column( |
|
943 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable = False, unique = None, default = None) | |
944 |
|
944 | |||
945 |
users_group = relationship( |
|
945 | users_group = relationship('UsersGroup') | |
946 |
permission = relationship( |
|
946 | permission = relationship('Permission') | |
947 |
repository = relationship( |
|
947 | repository = relationship('Repository') | |
948 |
|
948 | |||
949 |
def __repr__( |
|
949 | def __repr__(self): | |
950 |
return '<userGroup:%s => %s >' % ( |
|
950 | return '<userGroup:%s => %s >' % (self.users_group, self.repository) | |
951 |
|
951 | |||
952 |
class UsersGroupToPerm( |
|
952 | class UsersGroupToPerm(Base, BaseModel): | |
953 | __tablename__ = 'users_group_to_perm' |
|
953 | __tablename__ = 'users_group_to_perm' | |
954 |
users_group_to_perm_id = Column( |
|
954 | users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
955 |
users_group_id = Column( |
|
955 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable = False, unique = None, default = None) | |
956 |
permission_id = Column( |
|
956 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable = False, unique = None, default = None) | |
957 |
|
957 | |||
958 |
users_group = relationship( |
|
958 | users_group = relationship('UsersGroup') | |
959 |
permission = relationship( |
|
959 | permission = relationship('Permission') | |
960 |
|
960 | |||
961 |
|
961 | |||
962 | @classmethod |
|
962 | @classmethod | |
963 |
def has_perm( |
|
963 | def has_perm(cls, users_group_id, perm): | |
964 |
if not isinstance( |
|
964 | if not isinstance(perm, Permission): | |
965 |
raise Exception( |
|
965 | raise Exception('perm needs to be an instance of Permission class') | |
966 |
|
966 | |||
967 |
return cls.query().filter( |
|
967 | return cls.query().filter(cls.users_group_id == | |
968 |
users_group_id |
|
968 | users_group_id)\ | |
969 |
.filter( |
|
969 | .filter(cls.permission == perm)\ | |
970 | .scalar() is not None |
|
970 | .scalar() is not None | |
971 |
|
971 | |||
972 | @classmethod |
|
972 | @classmethod | |
973 |
def grant_perm( |
|
973 | def grant_perm(cls, users_group_id, perm): | |
974 |
if not isinstance( |
|
974 | if not isinstance(perm, Permission): | |
975 |
raise Exception( |
|
975 | raise Exception('perm needs to be an instance of Permission class') | |
976 |
|
976 | |||
977 | new = cls() |
|
977 | new = cls() | |
978 | new.users_group_id = users_group_id |
|
978 | new.users_group_id = users_group_id | |
979 | new.permission = perm |
|
979 | new.permission = perm | |
980 | try: |
|
980 | try: | |
981 |
Session.add( |
|
981 | Session.add(new) | |
982 | Session.commit() |
|
982 | Session.commit() | |
983 | except: |
|
983 | except: | |
984 | Session.rollback() |
|
984 | Session.rollback() | |
985 |
|
985 | |||
986 |
|
986 | |||
987 | @classmethod |
|
987 | @classmethod | |
988 |
def revoke_perm( |
|
988 | def revoke_perm(cls, users_group_id, perm): | |
989 |
if not isinstance( |
|
989 | if not isinstance(perm, Permission): | |
990 |
raise Exception( |
|
990 | raise Exception('perm needs to be an instance of Permission class') | |
991 |
|
991 | |||
992 | try: |
|
992 | try: | |
993 |
cls.query().filter( |
|
993 | cls.query().filter(cls.users_group_id == users_group_id)\ | |
994 |
.filter( |
|
994 | .filter(cls.permission == perm).delete() | |
995 | Session.commit() |
|
995 | Session.commit() | |
996 | except: |
|
996 | except: | |
997 | Session.rollback() |
|
997 | Session.rollback() | |
998 |
|
998 | |||
999 |
|
999 | |||
1000 |
class GroupToPerm( |
|
1000 | class GroupToPerm(Base, BaseModel): | |
1001 | __tablename__ = 'group_to_perm' |
|
1001 | __tablename__ = 'group_to_perm' | |
1002 |
__table_args__ = ( |
|
1002 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'extend_existing':True}) | |
1003 |
|
1003 | |||
1004 |
group_to_perm_id = Column( |
|
1004 | group_to_perm_id = Column("group_to_perm_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
1005 |
user_id = Column( |
|
1005 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable = False, unique = None, default = None) | |
1006 |
permission_id = Column( |
|
1006 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable = False, unique = None, default = None) | |
1007 |
group_id = Column( |
|
1007 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable = False, unique = None, default = None) | |
1008 |
|
1008 | |||
1009 |
user = relationship( |
|
1009 | user = relationship('User') | |
1010 |
permission = relationship( |
|
1010 | permission = relationship('Permission') | |
1011 |
group = relationship( |
|
1011 | group = relationship('Group') | |
1012 |
|
1012 | |||
1013 |
class Statistics( |
|
1013 | class Statistics(Base, BaseModel): | |
1014 | __tablename__ = 'statistics' |
|
1014 | __tablename__ = 'statistics' | |
1015 |
__table_args__ = ( |
|
1015 | __table_args__ = (UniqueConstraint('repository_id'), {'extend_existing':True}) | |
1016 |
stat_id = Column( |
|
1016 | stat_id = Column("stat_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
1017 |
repository_id = Column( |
|
1017 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable = False, unique = True, default = None) | |
1018 |
stat_on_revision = Column( |
|
1018 | stat_on_revision = Column("stat_on_revision", Integer(), nullable = False) | |
1019 |
commit_activity = Column( |
|
1019 | commit_activity = Column("commit_activity", LargeBinary(1000000), nullable = False)#JSON data | |
1020 |
commit_activity_combined = Column( |
|
1020 | commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable = False)#JSON data | |
1021 |
languages = Column( |
|
1021 | languages = Column("languages", LargeBinary(1000000), nullable = False)#JSON data | |
1022 |
|
1022 | |||
1023 |
repository = relationship( |
|
1023 | repository = relationship('Repository', single_parent = True) | |
1024 |
|
1024 | |||
1025 |
class UserFollowing( |
|
1025 | class UserFollowing(Base, BaseModel): | |
1026 | __tablename__ = 'user_followings' |
|
1026 | __tablename__ = 'user_followings' | |
1027 |
__table_args__ = ( |
|
1027 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), | |
1028 |
UniqueConstraint( |
|
1028 | UniqueConstraint('user_id', 'follows_user_id') | |
1029 |
, {'extend_existing':True} |
|
1029 | , {'extend_existing':True}) | |
1030 |
|
1030 | |||
1031 |
user_following_id = Column( |
|
1031 | user_following_id = Column("user_following_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
1032 |
user_id = Column( |
|
1032 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable = False, unique = None, default = None) | |
1033 |
follows_repo_id = Column( |
|
1033 | follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable = True, unique = None, default = None) | |
1034 |
follows_user_id = Column( |
|
1034 | follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable = True, unique = None, default = None) | |
1035 |
follows_from = Column( |
|
1035 | follows_from = Column('follows_from', DateTime(timezone = False), nullable = True, unique = None, default = datetime.datetime.now) | |
1036 |
|
1036 | |||
1037 |
user = relationship( |
|
1037 | user = relationship('User', primaryjoin = 'User.user_id==UserFollowing.user_id') | |
1038 |
|
1038 | |||
1039 |
follows_user = relationship( |
|
1039 | follows_user = relationship('User', primaryjoin = 'User.user_id==UserFollowing.follows_user_id') | |
1040 |
follows_repository = relationship( |
|
1040 | follows_repository = relationship('Repository', order_by = 'Repository.repo_name') | |
1041 |
|
1041 | |||
1042 |
|
1042 | |||
1043 | @classmethod |
|
1043 | @classmethod | |
1044 |
def get_repo_followers( |
|
1044 | def get_repo_followers(cls, repo_id): | |
1045 |
return cls.query().filter( |
|
1045 | return cls.query().filter(cls.follows_repo_id == repo_id) | |
1046 |
|
1046 | |||
1047 |
class CacheInvalidation( |
|
1047 | class CacheInvalidation(Base, BaseModel): | |
1048 | __tablename__ = 'cache_invalidation' |
|
1048 | __tablename__ = 'cache_invalidation' | |
1049 |
__table_args__ = ( |
|
1049 | __table_args__ = (UniqueConstraint('cache_key'), {'extend_existing':True}) | |
1050 |
cache_id = Column( |
|
1050 | cache_id = Column("cache_id", Integer(), nullable = False, unique = True, default = None, primary_key = True) | |
1051 |
cache_key = Column( |
|
1051 | cache_key = Column("cache_key", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
1052 |
cache_args = Column( |
|
1052 | cache_args = Column("cache_args", String(length = 255, convert_unicode = False, assert_unicode = None), nullable = True, unique = None, default = None) | |
1053 |
cache_active = Column( |
|
1053 | cache_active = Column("cache_active", Boolean(), nullable = True, unique = None, default = False) | |
1054 |
|
1054 | |||
1055 |
|
1055 | |||
1056 |
def __init__( |
|
1056 | def __init__(self, cache_key, cache_args = ''): | |
1057 | self.cache_key = cache_key |
|
1057 | self.cache_key = cache_key | |
1058 | self.cache_args = cache_args |
|
1058 | self.cache_args = cache_args | |
1059 | self.cache_active = False |
|
1059 | self.cache_active = False | |
1060 |
|
1060 | |||
1061 |
def __repr__( |
|
1061 | def __repr__(self): | |
1062 |
return "<%s('%s:%s')>" % ( |
|
1062 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
1063 |
self.cache_id, self.cache_key |
|
1063 | self.cache_id, self.cache_key) | |
1064 |
|
1064 | |||
1065 |
class DbMigrateVersion( |
|
1065 | class DbMigrateVersion(Base, BaseModel): | |
1066 | __tablename__ = 'db_migrate_version' |
|
1066 | __tablename__ = 'db_migrate_version' | |
1067 | __table_args__ = {'extend_existing':True} |
|
1067 | __table_args__ = {'extend_existing':True} | |
1068 |
repository_id = Column( |
|
1068 | repository_id = Column('repository_id', String(250), primary_key = True) | |
1069 |
repository_path = Column( |
|
1069 | repository_path = Column('repository_path', Text) | |
1070 |
version = Column( |
|
1070 | version = Column('version', Integer) |
@@ -27,18 +27,18 b' import logging' | |||||
27 | from rhodecode.model.db import BaseModel, RepoToPerm, Permission |
|
27 | from rhodecode.model.db import BaseModel, RepoToPerm, Permission | |
28 | from rhodecode.model.meta import Session |
|
28 | from rhodecode.model.meta import Session | |
29 |
|
29 | |||
30 |
log = logging.getLogger( |
|
30 | log = logging.getLogger(__name__) | |
31 |
|
31 | |||
32 |
class RepositoryPermissionModel( |
|
32 | class RepositoryPermissionModel(BaseModel): | |
33 |
def get |
|
33 | def get_user_permission(self, repository, user): | |
34 | return RepoToPerm.query() \ |
|
34 | return RepoToPerm.query() \ | |
35 |
.filter( |
|
35 | .filter(RepoToPerm.user == user) \ | |
36 |
.filter( |
|
36 | .filter(RepoToPerm.repository == repository) \ | |
37 | .scalar() |
|
37 | .scalar() | |
38 |
|
38 | |||
39 |
def update |
|
39 | def update_user_permission(self, repository, user, permission): | |
40 |
permission = Permission.get_by_key( |
|
40 | permission = Permission.get_by_key(permission) | |
41 |
current = self.get |
|
41 | current = self.get_user_permission(repository, user) | |
42 | if current: |
|
42 | if current: | |
43 | if not current.permission is permission: |
|
43 | if not current.permission is permission: | |
44 | current.permission = permission |
|
44 | current.permission = permission | |
@@ -47,17 +47,17 b' class RepositoryPermissionModel( BaseMod' | |||||
47 | p.user = user |
|
47 | p.user = user | |
48 | p.repository = repository |
|
48 | p.repository = repository | |
49 | p.permission = permission |
|
49 | p.permission = permission | |
50 |
Session.add( |
|
50 | Session.add(p) | |
51 | Session.commit() |
|
51 | Session.commit() | |
52 |
|
52 | |||
53 |
def delete |
|
53 | def delete_user_permission(self, repository, user): | |
54 |
current = self.get |
|
54 | current = self.get_user_permission(repository, user) | |
55 | if current: |
|
55 | if current: | |
56 |
Session.delete( |
|
56 | Session.delete(current) | |
57 | Session.commit() |
|
57 | Session.commit() | |
58 |
|
58 | |||
59 |
def update |
|
59 | def update_or_delete_user_permission(self, repository, user, permission): | |
60 | if permission: |
|
60 | if permission: | |
61 |
self.update |
|
61 | self.update_user_permission(repository, user, permission) | |
62 | else: |
|
62 | else: | |
63 |
self.delete |
|
63 | self.delete_user_permission(repository, user) |
@@ -36,21 +36,21 b' from rhodecode.model import BaseModel' | |||||
36 | from rhodecode.model.caching_query import FromCache |
|
36 | from rhodecode.model.caching_query import FromCache | |
37 | from rhodecode.model.db import Group, RhodeCodeUi |
|
37 | from rhodecode.model.db import Group, RhodeCodeUi | |
38 |
|
38 | |||
39 |
log = logging.getLogger( |
|
39 | log = logging.getLogger(__name__) | |
40 |
|
40 | |||
41 |
|
41 | |||
42 |
class ReposGroupModel( |
|
42 | class ReposGroupModel(BaseModel): | |
43 |
|
43 | |||
44 | @LazyProperty |
|
44 | @LazyProperty | |
45 |
def repos_path( |
|
45 | def repos_path(self): | |
46 | """ |
|
46 | """ | |
47 | Get's the repositories root path from database |
|
47 | Get's the repositories root path from database | |
48 | """ |
|
48 | """ | |
49 |
|
49 | |||
50 |
q = RhodeCodeUi.get_by_key( |
|
50 | q = RhodeCodeUi.get_by_key('/').one() | |
51 | return q.ui_value |
|
51 | return q.ui_value | |
52 |
|
52 | |||
53 |
def __create_group( |
|
53 | def __create_group(self, group_name, parent_id): | |
54 | """ |
|
54 | """ | |
55 | makes repositories group on filesystem |
|
55 | makes repositories group on filesystem | |
56 |
|
56 | |||
@@ -59,64 +59,64 b' class ReposGroupModel( BaseModel ):' | |||||
59 | """ |
|
59 | """ | |
60 |
|
60 | |||
61 | if parent_id: |
|
61 | if parent_id: | |
62 |
paths = Group.get( |
|
62 | paths = Group.get(parent_id).full_path.split(Group.url_sep()) | |
63 |
parent_path = os.sep.join( |
|
63 | parent_path = os.sep.join(paths) | |
64 | else: |
|
64 | else: | |
65 | parent_path = '' |
|
65 | parent_path = '' | |
66 |
|
66 | |||
67 |
create_path = os.path.join( |
|
67 | create_path = os.path.join(self.repos_path, parent_path, group_name) | |
68 |
log.debug( |
|
68 | log.debug('creating new group in %s', create_path) | |
69 |
|
69 | |||
70 |
if os.path.isdir( |
|
70 | if os.path.isdir(create_path): | |
71 |
raise Exception( |
|
71 | raise Exception('That directory already exists !') | |
72 |
|
72 | |||
73 |
|
73 | |||
74 |
os.makedirs( |
|
74 | os.makedirs(create_path) | |
75 |
|
75 | |||
76 |
|
76 | |||
77 |
def __rename_group( |
|
77 | def __rename_group(self, old, old_parent_id, new, new_parent_id): | |
78 | """ |
|
78 | """ | |
79 | Renames a group on filesystem |
|
79 | Renames a group on filesystem | |
80 |
|
80 | |||
81 | :param group_name: |
|
81 | :param group_name: | |
82 | """ |
|
82 | """ | |
83 |
log.debug( |
|
83 | log.debug('renaming repos group from %s to %s', old, new) | |
84 |
|
84 | |||
85 | if new_parent_id: |
|
85 | if new_parent_id: | |
86 |
paths = Group.get( |
|
86 | paths = Group.get(new_parent_id).full_path.split(Group.url_sep()) | |
87 |
new_parent_path = os.sep.join( |
|
87 | new_parent_path = os.sep.join(paths) | |
88 | else: |
|
88 | else: | |
89 | new_parent_path = '' |
|
89 | new_parent_path = '' | |
90 |
|
90 | |||
91 | if old_parent_id: |
|
91 | if old_parent_id: | |
92 |
paths = Group.get( |
|
92 | paths = Group.get(old_parent_id).full_path.split(Group.url_sep()) | |
93 |
old_parent_path = os.sep.join( |
|
93 | old_parent_path = os.sep.join(paths) | |
94 | else: |
|
94 | else: | |
95 | old_parent_path = '' |
|
95 | old_parent_path = '' | |
96 |
|
96 | |||
97 |
old_path = os.path.join( |
|
97 | old_path = os.path.join(self.repos_path, old_parent_path, old) | |
98 |
new_path = os.path.join( |
|
98 | new_path = os.path.join(self.repos_path, new_parent_path, new) | |
99 |
|
99 | |||
100 |
log.debug( |
|
100 | log.debug('renaming repos paths from %s to %s', old_path, new_path) | |
101 |
|
101 | |||
102 |
if os.path.isdir( |
|
102 | if os.path.isdir(new_path): | |
103 |
raise Exception( |
|
103 | raise Exception('Was trying to rename to already ' | |
104 |
'existing dir %s' % new_path |
|
104 | 'existing dir %s' % new_path) | |
105 |
shutil.move( |
|
105 | shutil.move(old_path, new_path) | |
106 |
|
106 | |||
107 |
def __delete_group( |
|
107 | def __delete_group(self, group): | |
108 | """ |
|
108 | """ | |
109 | Deletes a group from a filesystem |
|
109 | Deletes a group from a filesystem | |
110 |
|
110 | |||
111 | :param group: instance of group from database |
|
111 | :param group: instance of group from database | |
112 | """ |
|
112 | """ | |
113 |
paths = group.full_path.split( |
|
113 | paths = group.full_path.split(Group.url_sep()) | |
114 |
paths = os.sep.join( |
|
114 | paths = os.sep.join(paths) | |
115 |
|
115 | |||
116 |
rm_path = os.path.join( |
|
116 | rm_path = os.path.join(self.repos_path, paths) | |
117 |
os.rmdir( |
|
117 | os.rmdir(rm_path) | |
118 |
|
118 | |||
119 |
def create( |
|
119 | def create(self, form_data): | |
120 | try: |
|
120 | try: | |
121 | new_repos_group = Group() |
|
121 | new_repos_group = Group() | |
122 | new_repos_group.group_name = form_data['group_name'] |
|
122 | new_repos_group.group_name = form_data['group_name'] | |
@@ -124,22 +124,22 b' class ReposGroupModel( BaseModel ):' | |||||
124 | form_data['group_description'] |
|
124 | form_data['group_description'] | |
125 | new_repos_group.group_parent_id = form_data['group_parent_id'] |
|
125 | new_repos_group.group_parent_id = form_data['group_parent_id'] | |
126 |
|
126 | |||
127 |
self.sa.add( |
|
127 | self.sa.add(new_repos_group) | |
128 |
|
128 | |||
129 |
self.__create_group( |
|
129 | self.__create_group(form_data['group_name'], | |
130 |
form_data['group_parent_id'] |
|
130 | form_data['group_parent_id']) | |
131 |
|
131 | |||
132 | self.sa.commit() |
|
132 | self.sa.commit() | |
133 | return new_repos_group |
|
133 | return new_repos_group | |
134 | except: |
|
134 | except: | |
135 |
log.error( |
|
135 | log.error(traceback.format_exc()) | |
136 | self.sa.rollback() |
|
136 | self.sa.rollback() | |
137 | raise |
|
137 | raise | |
138 |
|
138 | |||
139 |
def update( |
|
139 | def update(self, repos_group_id, form_data): | |
140 |
|
140 | |||
141 | try: |
|
141 | try: | |
142 |
repos_group = Group.get( |
|
142 | repos_group = Group.get(repos_group_id) | |
143 | old_name = repos_group.group_name |
|
143 | old_name = repos_group.group_name | |
144 | old_parent_id = repos_group.group_parent_id |
|
144 | old_parent_id = repos_group.group_parent_id | |
145 |
|
145 | |||
@@ -148,27 +148,27 b' class ReposGroupModel( BaseModel ):' | |||||
148 | form_data['group_description'] |
|
148 | form_data['group_description'] | |
149 | repos_group.group_parent_id = form_data['group_parent_id'] |
|
149 | repos_group.group_parent_id = form_data['group_parent_id'] | |
150 |
|
150 | |||
151 |
self.sa.add( |
|
151 | self.sa.add(repos_group) | |
152 |
|
152 | |||
153 |
if old_name != form_data['group_name'] or ( |
|
153 | if old_name != form_data['group_name'] or (old_parent_id != | |
154 |
form_data['group_parent_id'] |
|
154 | form_data['group_parent_id']): | |
155 |
self.__rename_group( |
|
155 | self.__rename_group(old = old_name, old_parent_id = old_parent_id, | |
156 | new = form_data['group_name'], |
|
156 | new = form_data['group_name'], | |
157 |
new_parent_id = form_data['group_parent_id'] |
|
157 | new_parent_id = form_data['group_parent_id']) | |
158 |
|
158 | |||
159 | self.sa.commit() |
|
159 | self.sa.commit() | |
160 | except: |
|
160 | except: | |
161 |
log.error( |
|
161 | log.error(traceback.format_exc()) | |
162 | self.sa.rollback() |
|
162 | self.sa.rollback() | |
163 | raise |
|
163 | raise | |
164 |
|
164 | |||
165 |
def delete( |
|
165 | def delete(self, users_group_id): | |
166 | try: |
|
166 | try: | |
167 |
users_group = Group.get( |
|
167 | users_group = Group.get(users_group_id) | |
168 |
self.sa.delete( |
|
168 | self.sa.delete(users_group) | |
169 |
self.__delete_group( |
|
169 | self.__delete_group(users_group) | |
170 | self.sa.commit() |
|
170 | self.sa.commit() | |
171 | except: |
|
171 | except: | |
172 |
log.error( |
|
172 | log.error(traceback.format_exc()) | |
173 | self.sa.rollback() |
|
173 | self.sa.rollback() | |
174 | raise |
|
174 | raise |
@@ -49,14 +49,14 b" PERM_WEIGHTS = {'repository.none': 0," | |||||
49 |
|
49 | |||
50 |
|
50 | |||
51 | class UserModel(BaseModel): |
|
51 | class UserModel(BaseModel): | |
52 | def get(self, user_id, cache=False): |
|
52 | def get(self, user_id, cache = False): | |
53 | user = self.sa.query(User) |
|
53 | user = self.sa.query(User) | |
54 | if cache: |
|
54 | if cache: | |
55 | user = user.options(FromCache("sql_cache_short", |
|
55 | user = user.options(FromCache("sql_cache_short", | |
56 | "get_user_%s" % user_id)) |
|
56 | "get_user_%s" % user_id)) | |
57 | return user.get(user_id) |
|
57 | return user.get(user_id) | |
58 |
|
58 | |||
59 | def get_by_username(self, username, cache=False, case_insensitive=False): |
|
59 | def get_by_username(self, username, cache = False, case_insensitive = False): | |
60 |
|
60 | |||
61 | if case_insensitive: |
|
61 | if case_insensitive: | |
62 | user = self.sa.query(User).filter(User.username.ilike(username)) |
|
62 | user = self.sa.query(User).filter(User.username.ilike(username)) | |
@@ -68,7 +68,7 b' class UserModel(BaseModel):' | |||||
68 | "get_user_%s" % username)) |
|
68 | "get_user_%s" % username)) | |
69 | return user.scalar() |
|
69 | return user.scalar() | |
70 |
|
70 | |||
71 | def get_by_api_key(self, api_key, cache=False): |
|
71 | def get_by_api_key(self, api_key, cache = False): | |
72 |
|
72 | |||
73 | user = self.sa.query(User)\ |
|
73 | user = self.sa.query(User)\ | |
74 | .filter(User.api_key == api_key) |
|
74 | .filter(User.api_key == api_key) | |
@@ -103,7 +103,7 b' class UserModel(BaseModel):' | |||||
103 | """ |
|
103 | """ | |
104 | from rhodecode.lib.auth import get_crypt_password |
|
104 | from rhodecode.lib.auth import get_crypt_password | |
105 | log.debug('Checking for such ldap account in RhodeCode database') |
|
105 | log.debug('Checking for such ldap account in RhodeCode database') | |
106 | if self.get_by_username(username, case_insensitive=True) is None: |
|
106 | if self.get_by_username(username, case_insensitive = True) is None: | |
107 | try: |
|
107 | try: | |
108 | new_user = User() |
|
108 | new_user = User() | |
109 | # add ldap account always lowercase |
|
109 | # add ldap account always lowercase | |
@@ -152,7 +152,7 b' class UserModel(BaseModel):' | |||||
152 |
|
152 | |||
153 | def update(self, user_id, form_data): |
|
153 | def update(self, user_id, form_data): | |
154 | try: |
|
154 | try: | |
155 | user = self.get(user_id, cache=False) |
|
155 | user = self.get(user_id, cache = False) | |
156 | if user.username == 'default': |
|
156 | if user.username == 'default': | |
157 | raise DefaultUserException( |
|
157 | raise DefaultUserException( | |
158 | _("You can't Edit this user since it's" |
|
158 | _("You can't Edit this user since it's" | |
@@ -174,7 +174,7 b' class UserModel(BaseModel):' | |||||
174 |
|
174 | |||
175 | def update_my_account(self, user_id, form_data): |
|
175 | def update_my_account(self, user_id, form_data): | |
176 | try: |
|
176 | try: | |
177 | user = self.get(user_id, cache=False) |
|
177 | user = self.get(user_id, cache = False) | |
178 | if user.username == 'default': |
|
178 | if user.username == 'default': | |
179 | raise DefaultUserException( |
|
179 | raise DefaultUserException( | |
180 | _("You can't Edit this user since it's" |
|
180 | _("You can't Edit this user since it's" | |
@@ -196,7 +196,7 b' class UserModel(BaseModel):' | |||||
196 |
|
196 | |||
197 | def delete(self, user_id): |
|
197 | def delete(self, user_id): | |
198 | try: |
|
198 | try: | |
199 | user = self.get(user_id, cache=False) |
|
199 | user = self.get(user_id, cache = False) | |
200 | if user.username == 'default': |
|
200 | if user.username == 'default': | |
201 | raise DefaultUserException( |
|
201 | raise DefaultUserException( | |
202 | _("You can't remove this user since it's" |
|
202 | _("You can't remove this user since it's" | |
@@ -222,7 +222,7 b' class UserModel(BaseModel):' | |||||
222 | from rhodecode.lib.celerylib import tasks, run_task |
|
222 | from rhodecode.lib.celerylib import tasks, run_task | |
223 | run_task(tasks.reset_user_password, data['email']) |
|
223 | run_task(tasks.reset_user_password, data['email']) | |
224 |
|
224 | |||
225 | def fill_data(self, auth_user, user_id=None, api_key=None): |
|
225 | def fill_data(self, auth_user, user_id = None, api_key = None): | |
226 | """ |
|
226 | """ | |
227 | Fetches auth_user by user_id,or api_key if present. |
|
227 | Fetches auth_user by user_id,or api_key if present. | |
228 | Fills auth_user attributes with those taken from database. |
|
228 | Fills auth_user attributes with those taken from database. | |
@@ -268,7 +268,7 b' class UserModel(BaseModel):' | |||||
268 | #====================================================================== |
|
268 | #====================================================================== | |
269 | # fetch default permissions |
|
269 | # fetch default permissions | |
270 | #====================================================================== |
|
270 | #====================================================================== | |
271 | default_user = self.get_by_username('default', cache=True) |
|
271 | default_user = self.get_by_username('default', cache = True) | |
272 |
|
272 | |||
273 | default_perms = self.sa.query(RepoToPerm, Repository, Permission)\ |
|
273 | default_perms = self.sa.query(RepoToPerm, Repository, Permission)\ | |
274 | .join((Repository, RepoToPerm.repository_id == |
|
274 | .join((Repository, RepoToPerm.repository_id == |
@@ -30,43 +30,43 b' from rhodecode.model import BaseModel' | |||||
30 | from rhodecode.model.caching_query import FromCache |
|
30 | from rhodecode.model.caching_query import FromCache | |
31 | from rhodecode.model.db import UsersGroupMember, UsersGroup |
|
31 | from rhodecode.model.db import UsersGroupMember, UsersGroup | |
32 |
|
32 | |||
33 |
log = logging.getLogger( |
|
33 | log = logging.getLogger(__name__) | |
34 |
|
34 | |||
35 |
class UsersGroupModel( |
|
35 | class UsersGroupModel(BaseModel): | |
36 |
|
36 | |||
37 |
def get( |
|
37 | def get(self, users_group_id, cache = False): | |
38 | users_group = UsersGroup.query() |
|
38 | users_group = UsersGroup.query() | |
39 | if cache: |
|
39 | if cache: | |
40 |
users_group = users_group.options( |
|
40 | users_group = users_group.options(FromCache("sql_cache_short", | |
41 |
"get_users_group_%s" % users_group_id |
|
41 | "get_users_group_%s" % users_group_id)) | |
42 |
return users_group.get( |
|
42 | return users_group.get(users_group_id) | |
43 |
|
43 | |||
44 |
def get_by_name( |
|
44 | def get_by_name(self, name, cache = False, case_insensitive = False): | |
45 | users_group = UsersGroup.query() |
|
45 | users_group = UsersGroup.query() | |
46 | if case_insensitive: |
|
46 | if case_insensitive: | |
47 |
users_group = users_group.filter( |
|
47 | users_group = users_group.filter(UsersGroup.users_group_name.ilike(name)) | |
48 | else: |
|
48 | else: | |
49 |
users_group = users_group.filter( |
|
49 | users_group = users_group.filter(UsersGroup.users_group_name == name) | |
50 | if cache: |
|
50 | if cache: | |
51 |
users_group = users_group.options( |
|
51 | users_group = users_group.options(FromCache("sql_cache_short", | |
52 |
"get_users_group_%s" % name |
|
52 | "get_users_group_%s" % name)) | |
53 | return users_group.scalar() |
|
53 | return users_group.scalar() | |
54 |
|
54 | |||
55 |
def create( |
|
55 | def create(self, form_data): | |
56 | try: |
|
56 | try: | |
57 | new_users_group = UsersGroup() |
|
57 | new_users_group = UsersGroup() | |
58 | for k, v in form_data.items(): |
|
58 | for k, v in form_data.items(): | |
59 |
setattr( |
|
59 | setattr(new_users_group, k, v) | |
60 |
|
60 | |||
61 |
self.sa.add( |
|
61 | self.sa.add(new_users_group) | |
62 | self.sa.commit() |
|
62 | self.sa.commit() | |
63 | return new_users_group |
|
63 | return new_users_group | |
64 | except: |
|
64 | except: | |
65 |
log.error( |
|
65 | log.error(traceback.format_exc()) | |
66 | self.sa.rollback() |
|
66 | self.sa.rollback() | |
67 | raise |
|
67 | raise | |
68 |
|
68 | |||
69 |
def add_user_to_group( |
|
69 | def add_user_to_group(self, users_group, user): | |
70 | for m in users_group.members: |
|
70 | for m in users_group.members: | |
71 | u = m.user |
|
71 | u = m.user | |
72 | if u.user_id == user.user_id: |
|
72 | if u.user_id == user.user_id: | |
@@ -77,13 +77,13 b' class UsersGroupModel( BaseModel ):' | |||||
77 | users_group_member.user = user |
|
77 | users_group_member.user = user | |
78 | users_group_member.users_group = users_group |
|
78 | users_group_member.users_group = users_group | |
79 |
|
79 | |||
80 |
users_group.members.append( |
|
80 | users_group.members.append(users_group_member) | |
81 |
user.group_member.append( |
|
81 | user.group_member.append(users_group_member) | |
82 |
|
82 | |||
83 |
self.sa.add( |
|
83 | self.sa.add(users_group_member) | |
84 | self.sa.commit() |
|
84 | self.sa.commit() | |
85 | return users_group_member |
|
85 | return users_group_member | |
86 | except: |
|
86 | except: | |
87 |
log.error( |
|
87 | log.error(traceback.format_exc()) | |
88 | self.sa.rollback() |
|
88 | self.sa.rollback() | |
89 | raise |
|
89 | raise |
General Comments 0
You need to be logged in to leave comments.
Login now