##// END OF EJS Templates
another major refactoring with session management
marcink -
r1734:48d4fcf0 beta
parent child Browse files
Show More
@@ -38,6 +38,7 b' from rhodecode.lib.base import BaseContr'
38 38 from rhodecode.model.forms import DefaultPermissionsForm
39 39 from rhodecode.model.permission import PermissionModel
40 40 from rhodecode.model.db import User
41 from rhodecode.model.meta import Session
41 42
42 43 log = logging.getLogger(__name__)
43 44
@@ -101,6 +102,7 b' class PermissionsController(BaseControll'
101 102 form_result = _form.to_python(dict(request.POST))
102 103 form_result.update({'perm_user_name': id})
103 104 permission_model.update(form_result)
105 Session().commit()
104 106 h.flash(_('Default permissions updated successfully'),
105 107 category='success')
106 108
@@ -17,6 +17,7 b' from rhodecode.lib.base import BaseContr'
17 17 from rhodecode.model.db import RepoGroup
18 18 from rhodecode.model.repos_group import ReposGroupModel
19 19 from rhodecode.model.forms import ReposGroupForm
20 from rhodecode.model.meta import Session
20 21
21 22 log = logging.getLogger(__name__)
22 23
@@ -65,12 +66,12 b' class ReposGroupsController(BaseControll'
65 66 """POST /repos_groups: Create a new item"""
66 67 # url('repos_groups')
67 68 self.__load_defaults()
68 repos_group_model = ReposGroupModel()
69 69 repos_group_form = ReposGroupForm(available_groups=
70 70 c.repo_groups_choices)()
71 71 try:
72 72 form_result = repos_group_form.to_python(dict(request.POST))
73 repos_group_model.create(form_result)
73 ReposGroupModel().create(form_result)
74 Session().commit()
74 75 h.flash(_('created repos group %s') \
75 76 % form_result['group_name'], category='success')
76 77 #TODO: in futureaction_logger(, '', '', '', self.sa)
@@ -110,14 +111,14 b' class ReposGroupsController(BaseControll'
110 111 self.__load_defaults()
111 112 c.repos_group = RepoGroup.get(id)
112 113
113 repos_group_model = ReposGroupModel()
114 114 repos_group_form = ReposGroupForm(edit=True,
115 115 old_data=c.repos_group.get_dict(),
116 116 available_groups=
117 117 c.repo_groups_choices)()
118 118 try:
119 119 form_result = repos_group_form.to_python(dict(request.POST))
120 repos_group_model.update(id, form_result)
120 ReposGroupModel().update(id, form_result)
121 Session().commit()
121 122 h.flash(_('updated repos group %s') \
122 123 % form_result['group_name'], category='success')
123 124 #TODO: in futureaction_logger(, '', '', '', self.sa)
@@ -147,7 +148,6 b' class ReposGroupsController(BaseControll'
147 148 # method='delete')
148 149 # url('repos_group', id=ID)
149 150
150 repos_group_model = ReposGroupModel()
151 151 gr = RepoGroup.get(id)
152 152 repos = gr.repositories.all()
153 153 if repos:
@@ -157,7 +157,8 b' class ReposGroupsController(BaseControll'
157 157 return redirect(url('repos_groups'))
158 158
159 159 try:
160 repos_group_model.delete(id)
160 ReposGroupModel().delete(id)
161 Session().commit()
161 162 h.flash(_('removed repos group %s' % gr.group_name), category='success')
162 163 #TODO: in future action_logger(, '', '', '', self.sa)
163 164 except IntegrityError, e:
@@ -1,19 +1,21 b''
1 1 import traceback
2 2 import logging
3 3
4 from sqlalchemy.orm.exc import NoResultFound
5
4 6 from rhodecode.controllers.api import JSONRPCController, JSONRPCError
5 7 from rhodecode.lib.auth import HasPermissionAllDecorator, \
6 8 HasPermissionAnyDecorator
9
10 from rhodecode.model.meta import Session
7 11 from rhodecode.model.scm import ScmModel
8
9 12 from rhodecode.model.db import User, UsersGroup, RepoGroup, Repository
10 13 from rhodecode.model.repo import RepoModel
11 14 from rhodecode.model.user import UserModel
12 15 from rhodecode.model.repo_permission import RepositoryPermissionModel
13 16 from rhodecode.model.users_group import UsersGroupModel
14 from rhodecode.model import users_group
15 17 from rhodecode.model.repos_group import ReposGroupModel
16 from sqlalchemy.orm.exc import NoResultFound
18
17 19
18 20 log = logging.getLogger(__name__)
19 21
@@ -116,8 +118,9 b' class ApiController(JSONRPCController):'
116 118 raise JSONRPCError("user %s already exist" % username)
117 119
118 120 try:
119 UserModel().create_or_update(username, password, email, firstname,
121 UserModel().create_or_update(username, password, email, firstname,
120 122 lastname, active, admin, ldap_dn)
123 Session().commit()
121 124 return dict(msg='created new user %s' % username)
122 125 except Exception:
123 126 log.error(traceback.format_exc())
@@ -194,9 +197,8 b' class ApiController(JSONRPCController):'
194 197 raise JSONRPCError("users group %s already exist" % name)
195 198
196 199 try:
197 form_data = dict(users_group_name=name,
198 users_group_active=active)
199 ug = UsersGroup.create(form_data)
200 ug = UsersGroupModel().create(name=name, active=active)
201 Session().commit()
200 202 return dict(id=ug.users_group_id,
201 203 msg='created new users group %s' % name)
202 204 except Exception:
@@ -224,7 +226,7 b' class ApiController(JSONRPCController):'
224 226 raise JSONRPCError('unknown user %s' % user_name)
225 227
226 228 ugm = UsersGroupModel().add_user_to_group(users_group, user)
227
229 Session().commit()
228 230 return dict(id=ugm.users_group_member_id,
229 231 msg='created new users group member')
230 232 except Exception:
@@ -291,7 +293,7 b' class ApiController(JSONRPCController):'
291 293 return result
292 294
293 295 @HasPermissionAnyDecorator('hg.admin', 'hg.create.repository')
294 def create_repo(self, apiuser, name, owner_name, description='',
296 def create_repo(self, apiuser, name, owner_name, description='',
295 297 repo_type='hg', private=False):
296 298 """
297 299 Create a repository
@@ -332,6 +334,7 b' class ApiController(JSONRPCController):'
332 334 repo_type=repo_type,
333 335 repo_group=parent_id,
334 336 clone_uri=None), owner)
337 Session().commit()
335 338 except Exception:
336 339 log.error(traceback.format_exc())
337 340 raise JSONRPCError('failed to create repository %s' % name)
@@ -360,6 +363,7 b' class ApiController(JSONRPCController):'
360 363
361 364 RepositoryPermissionModel()\
362 365 .update_or_delete_user_permission(repo, user, perm)
366 Session().commit()
363 367 except Exception:
364 368 log.error(traceback.format_exc())
365 369 raise JSONRPCError('failed to edit permission %(repo)s for %(user)s'
@@ -78,16 +78,11 b' class DbManage(object):'
78 78 log.info('Created tables for %s', self.dbname)
79 79
80 80 def set_db_version(self):
81 try:
82 ver = DbMigrateVersion()
83 ver.version = __dbversion__
84 ver.repository_id = 'rhodecode_db_migrations'
85 ver.repository_path = 'versions'
86 self.sa.add(ver)
87 self.sa.commit()
88 except:
89 self.sa.rollback()
90 raise
81 ver = DbMigrateVersion()
82 ver.version = __dbversion__
83 ver.repository_id = 'rhodecode_db_migrations'
84 ver.repository_path = 'versions'
85 self.sa.add(ver)
91 86 log.info('db version set to: %s', __dbversion__)
92 87
93 88 def upgrade(self):
@@ -162,7 +157,7 b' class DbManage(object):'
162 157 self.klass.fix_settings()
163 158 print ('Adding ldap defaults')
164 159 self.klass.create_ldap_options(skip_existing=True)
165
160
166 161 upgrade_steps = [0] + range(curr_version + 1, __dbversion__ + 1)
167 162
168 163 #CALL THE PROPER ORDER OF STEPS TO PERFORM FULL UPGRADE
@@ -299,43 +294,33 b' class DbManage(object):'
299 294 dotencode_disable.ui_value = 'false'
300 295
301 296 # enable largefiles
302 dotencode_disable = RhodeCodeUi()
303 dotencode_disable.ui_section = 'extensions'
304 dotencode_disable.ui_key = 'largefiles'
305 dotencode_disable.ui_value = '1'
297 largefiles = RhodeCodeUi()
298 largefiles.ui_section = 'extensions'
299 largefiles.ui_key = 'largefiles'
300 largefiles.ui_value = '1'
306 301
307 try:
308 self.sa.add(hooks1)
309 self.sa.add(hooks2)
310 self.sa.add(hooks3)
311 self.sa.add(hooks4)
312 self.sa.add(dotencode_disable)
313 self.sa.commit()
314 except:
315 self.sa.rollback()
316 raise
302 self.sa.add(hooks1)
303 self.sa.add(hooks2)
304 self.sa.add(hooks3)
305 self.sa.add(hooks4)
306 self.sa.add(largefiles)
317 307
318 def create_ldap_options(self,skip_existing=False):
308 def create_ldap_options(self, skip_existing=False):
319 309 """Creates ldap settings"""
320 310
321 try:
322 for k, v in [('ldap_active', 'false'), ('ldap_host', ''),
323 ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'),
324 ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''),
325 ('ldap_dn_pass', ''), ('ldap_base_dn', ''),
326 ('ldap_filter', ''), ('ldap_search_scope', ''),
327 ('ldap_attr_login', ''), ('ldap_attr_firstname', ''),
328 ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]:
311 for k, v in [('ldap_active', 'false'), ('ldap_host', ''),
312 ('ldap_port', '389'), ('ldap_tls_kind', 'PLAIN'),
313 ('ldap_tls_reqcert', ''), ('ldap_dn_user', ''),
314 ('ldap_dn_pass', ''), ('ldap_base_dn', ''),
315 ('ldap_filter', ''), ('ldap_search_scope', ''),
316 ('ldap_attr_login', ''), ('ldap_attr_firstname', ''),
317 ('ldap_attr_lastname', ''), ('ldap_attr_email', '')]:
329 318
330 if skip_existing and RhodeCodeSetting.get_by_name(k) != None:
331 log.debug('Skipping option %s' % k)
332 continue
333 setting = RhodeCodeSetting(k, v)
334 self.sa.add(setting)
335 self.sa.commit()
336 except:
337 self.sa.rollback()
338 raise
319 if skip_existing and RhodeCodeSetting.get_by_name(k) != None:
320 log.debug('Skipping option %s' % k)
321 continue
322 setting = RhodeCodeSetting(k, v)
323 self.sa.add(setting)
339 324
340 325 def config_prompt(self, test_repo_path='', retries=3):
341 326 if retries == 3:
@@ -401,20 +386,14 b' class DbManage(object):'
401 386 hgsettings2 = RhodeCodeSetting('title', 'RhodeCode')
402 387 hgsettings3 = RhodeCodeSetting('ga_code', '')
403 388
404 try:
405 self.sa.add(web1)
406 self.sa.add(web2)
407 self.sa.add(web3)
408 self.sa.add(web4)
409 self.sa.add(paths)
410 self.sa.add(hgsettings1)
411 self.sa.add(hgsettings2)
412 self.sa.add(hgsettings3)
413
414 self.sa.commit()
415 except:
416 self.sa.rollback()
417 raise
389 self.sa.add(web1)
390 self.sa.add(web2)
391 self.sa.add(web3)
392 self.sa.add(web4)
393 self.sa.add(paths)
394 self.sa.add(hgsettings1)
395 self.sa.add(hgsettings2)
396 self.sa.add(hgsettings3)
418 397
419 398 self.create_ldap_options()
420 399
@@ -422,18 +401,18 b' class DbManage(object):'
422 401
423 402 def create_user(self, username, password, email='', admin=False):
424 403 log.info('creating user %s', username)
425 UserModel().create_or_update(username, password, email,
426 name='RhodeCode', lastname='Admin',
404 UserModel().create_or_update(username, password, email,
405 name='RhodeCode', lastname='Admin',
427 406 active=True, admin=admin)
428 407
429 408 def create_default_user(self):
430 409 log.info('creating default user')
431 410 # create default user for handling default permissions.
432 UserModel().create_or_update(username='default',
433 password=str(uuid.uuid1())[:8],
434 email='anonymous@rhodecode.org',
411 UserModel().create_or_update(username='default',
412 password=str(uuid.uuid1())[:8],
413 email='anonymous@rhodecode.org',
435 414 name='Anonymous', lastname='User')
436
415
437 416 def create_permissions(self):
438 417 #module.(access|create|change|delete)_[name]
439 418 #module.(read|write|owner)
@@ -458,12 +437,7 b' class DbManage(object):'
458 437 new_perm = Permission()
459 438 new_perm.permission_name = p[0]
460 439 new_perm.permission_longname = p[1]
461 try:
462 self.sa.add(new_perm)
463 self.sa.commit()
464 except:
465 self.sa.rollback()
466 raise
440 self.sa.add(new_perm)
467 441
468 442 def populate_default_permissions(self):
469 443 log.info('creating default user permissions')
@@ -489,11 +463,6 b' class DbManage(object):'
489 463 .filter(Permission.permission_name == 'repository.read')\
490 464 .scalar()
491 465
492 try:
493 self.sa.add(reg_perm)
494 self.sa.add(create_repo_perm)
495 self.sa.add(default_repo_perm)
496 self.sa.commit()
497 except:
498 self.sa.rollback()
499 raise
466 self.sa.add(reg_perm)
467 self.sa.add(create_repo_perm)
468 self.sa.add(default_repo_perm)
@@ -51,6 +51,7 b' from rhodecode.lib.caching_query import '
51 51 from rhodecode.model import meta
52 52 from rhodecode.model.db import Repository, User, RhodeCodeUi, \
53 53 UserLog, RepoGroup, RhodeCodeSetting
54 from rhodecode.model.meta import Session
54 55
55 56 log = logging.getLogger(__name__)
56 57
@@ -509,7 +510,7 b' def create_test_env(repos_test_path, con'
509 510 dbmanage.admin_prompt()
510 511 dbmanage.create_permissions()
511 512 dbmanage.populate_default_permissions()
512
513 Session().commit()
513 514 # PART TWO make test repo
514 515 log.debug('making test vcs repositories')
515 516
@@ -109,7 +109,6 b' class ChangesetCommentsModel(BaseModel):'
109 109 recipients=mention_recipients,
110 110 type_=Notification.TYPE_CHANGESET_COMMENT)
111 111
112 self.sa.commit()
113 112 return comment
114 113
115 114 def delete(self, comment):
@@ -70,8 +70,8 b' class PermissionModel(BaseModel):'
70 70
71 71 def update(self, form_result):
72 72 perm_user = self.sa.query(User)\
73 .filter(User.username ==
74 form_result['perm_user_name']).scalar()
73 .filter(User.username ==
74 form_result['perm_user_name']).scalar()
75 75 u2p = self.sa.query(UserToPerm).filter(UserToPerm.user ==
76 76 perm_user).all()
77 77 if len(u2p) != 3:
@@ -110,8 +110,6 b' class PermissionModel(BaseModel):'
110 110 perm_user.active = bool(form_result['anonymous'])
111 111 self.sa.add(perm_user)
112 112
113 self.sa.commit()
114 113 except (DatabaseError,):
115 114 log.error(traceback.format_exc())
116 self.sa.rollback()
117 115 raise
@@ -50,13 +50,11 b' class RepositoryPermissionModel(BaseMode'
50 50 p.repository = repository
51 51 p.permission = permission
52 52 self.sa.add(p)
53 self.sa.commit()
54 53
55 54 def delete_user_permission(self, repository, user):
56 55 current = self.get_user_permission(repository, user)
57 56 if current:
58 57 self.sa.delete(current)
59 self.sa.commit()
60 58
61 59 def update_or_delete_user_permission(self, repository, user, permission):
62 60 if permission:
@@ -111,14 +111,12 b' class ReposGroupModel(BaseModel):'
111 111 new_repos_group.group_name = new_repos_group.get_new_name(form_data['group_name'])
112 112
113 113 self.sa.add(new_repos_group)
114
114 self.sa.flush()
115 115 self.__create_group(new_repos_group.group_name)
116 116
117 self.sa.commit()
118 117 return new_repos_group
119 118 except:
120 119 log.error(traceback.format_exc())
121 self.sa.rollback()
122 120 raise
123 121
124 122 def update(self, repos_group_id, form_data):
@@ -126,7 +124,7 b' class ReposGroupModel(BaseModel):'
126 124 try:
127 125 repos_group = RepoGroup.get(repos_group_id)
128 126 old_path = repos_group.full_path
129
127
130 128 # change properties
131 129 repos_group.group_description = form_data['group_description']
132 130 repos_group.parent_group = RepoGroup.get(form_data['group_parent_id'])
@@ -144,11 +142,9 b' class ReposGroupModel(BaseModel):'
144 142 r.repo_name = r.get_new_name(r.just_name)
145 143 self.sa.add(r)
146 144
147 self.sa.commit()
148 145 return repos_group
149 146 except:
150 147 log.error(traceback.format_exc())
151 self.sa.rollback()
152 148 raise
153 149
154 150 def delete(self, users_group_id):
@@ -156,8 +152,6 b' class ReposGroupModel(BaseModel):'
156 152 users_group = RepoGroup.get(users_group_id)
157 153 self.sa.delete(users_group)
158 154 self.__delete_group(users_group)
159 self.sa.commit()
160 155 except:
161 156 log.error(traceback.format_exc())
162 self.sa.rollback()
163 157 raise
@@ -129,13 +129,10 b' class UserModel(BaseModel):'
129 129 new_user.ldap_dn = safe_unicode(ldap_dn) if ldap_dn else None
130 130 new_user.name = name
131 131 new_user.lastname = lastname
132
133 132 self.sa.add(new_user)
134 self.sa.commit()
135 133 return new_user
136 134 except (DatabaseError,):
137 135 log.error(traceback.format_exc())
138 self.sa.rollback()
139 136 raise
140 137
141 138
@@ -43,22 +43,7 b' class UsersGroupModel(BaseModel):'
43 43 def get_by_name(self, name, cache=False, case_insensitive=False):
44 44 return UsersGroup.get_by_group_name(name, cache, case_insensitive)
45 45
46 def create(self, form_data):
47 try:
48 new_users_group = UsersGroup()
49 for k, v in form_data.items():
50 setattr(new_users_group, k, v)
51
52 self.sa.add(new_users_group)
53 self.sa.commit()
54 return new_users_group
55 except:
56 log.error(traceback.format_exc())
57 self.sa.rollback()
58 raise
59
60
61 def create_(self, name, active=True):
46 def create(self, name, active=True):
62 47 new = UsersGroup()
63 48 new.users_group_name = name
64 49 new.users_group_active = active
@@ -84,9 +69,7 b' class UsersGroupModel(BaseModel):'
84 69 user.group_member.append(users_group_member)
85 70
86 71 self.sa.add(users_group_member)
87 self.sa.commit()
88 72 return users_group_member
89 73 except:
90 74 log.error(traceback.format_exc())
91 self.sa.rollback()
92 75 raise
@@ -53,7 +53,7 b' add_cache(conf)'
53 53
54 54 USER = 'test_admin'
55 55 PASS = 'test12'
56 HOST = '127.0.0.1:5000'
56 HOST = 'hg.local'
57 57 METHOD = 'pull'
58 58 DEBUG = True
59 59 log = logging.getLogger(__name__)
@@ -42,6 +42,7 b' class TestReposGroups(unittest.TestCase)'
42 42 group_description=desc,
43 43 group_parent_id=parent_id)
44 44 gr = ReposGroupModel().create(form_data)
45 Session.commit()
45 46 return gr
46 47
47 48 def __delete_group(self, id_):
@@ -65,7 +66,7 b' class TestReposGroups(unittest.TestCase)'
65 66
66 67 def test_create_same_name_group(self):
67 68 self.assertRaises(IntegrityError, lambda:self.__make_group('newGroup'))
68
69 Session().rollback()
69 70
70 71 def test_same_subgroup(self):
71 72 sg1 = self.__make_group('sub1', parent_id=self.g1.group_id)
@@ -162,16 +163,20 b' class TestUser(unittest.TestCase):'
162 163 usr = UserModel().create_or_update(username=u'test_user', password=u'qweqwe',
163 164 email=u'u232@rhodecode.org',
164 165 name=u'u1', lastname=u'u1')
166 Session().commit()
165 167 self.assertEqual(User.get_by_username(u'test_user'), usr)
166 168
167 169 # make users group
168 users_group = UsersGroupModel().create_('some_example_group')
170 users_group = UsersGroupModel().create('some_example_group')
169 171 Session().commit()
172
170 173 UsersGroupModel().add_user_to_group(users_group, usr)
174 Session().commit()
171 175
172 176 self.assertEqual(UsersGroup.get(users_group.users_group_id), users_group)
173 177 self.assertEqual(UsersGroupMember.query().count(), 1)
174 178 UserModel().delete(usr.user_id)
179 Session().commit()
175 180
176 181 self.assertEqual(UsersGroupMember.query().all(), [])
177 182
@@ -182,15 +187,24 b' class TestNotifications(unittest.TestCas'
182 187 self.u1 = UserModel().create_or_update(username=u'u1',
183 188 password=u'qweqwe',
184 189 email=u'u1@rhodecode.org',
185 name=u'u1', lastname=u'u1').user_id
190 name=u'u1', lastname=u'u1')
191 Session.commit()
192 self.u1 = self.u1.user_id
193
186 194 self.u2 = UserModel().create_or_update(username=u'u2',
187 195 password=u'qweqwe',
188 196 email=u'u2@rhodecode.org',
189 name=u'u2', lastname=u'u3').user_id
197 name=u'u2', lastname=u'u3')
198 Session.commit()
199 self.u2 = self.u2.user_id
200
190 201 self.u3 = UserModel().create_or_update(username=u'u3',
191 202 password=u'qweqwe',
192 203 email=u'u3@rhodecode.org',
193 name=u'u3', lastname=u'u3').user_id
204 name=u'u3', lastname=u'u3')
205 Session.commit()
206 self.u3 = self.u3.user_id
207
194 208 super(TestNotifications, self).__init__(methodName=methodName)
195 209
196 210 def _clean_notifications(self):
@@ -28,6 +28,7 b' import logging'
28 28
29 29 from rhodecode.config.environment import load_environment
30 30 from rhodecode.lib.db_manage import DbManage
31 from rhodecode.model.meta import Session
31 32
32 33
33 34 log = logging.getLogger(__name__)
@@ -45,5 +46,5 b' def setup_app(command, conf, vars):'
45 46 dbmanage.admin_prompt()
46 47 dbmanage.create_permissions()
47 48 dbmanage.populate_default_permissions()
48
49 Session().commit()
49 50 load_environment(conf.global_conf, conf.local_conf, initial=True)
General Comments 0
You need to be logged in to leave comments. Login now