##// END OF EJS Templates
fixes issue #16 reimplementation of database repository, for using generic pk instead of repo naming as pk. Which caused to many problems....
marcink -
r367:a26f48ad default
parent child Browse files
Show More
@@ -17,6 +17,11 b''
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 # MA 02110-1301, USA.
19 # MA 02110-1301, USA.
20 """
21 Created on June 30, 2010
22 settings controller for pylons
23 @author: marcink
24 """
20 from formencode import htmlfill
25 from formencode import htmlfill
21 from pylons import tmpl_context as c, request, url
26 from pylons import tmpl_context as c, request, url
22 from pylons.controllers.util import redirect
27 from pylons.controllers.util import redirect
@@ -30,11 +35,7 b' import formencode'
30 import logging
35 import logging
31 import pylons_app.lib.helpers as h
36 import pylons_app.lib.helpers as h
32 import traceback
37 import traceback
33 """
38
34 Created on June 30, 2010
35 settings controller for pylons
36 @author: marcink
37 """
38 log = logging.getLogger(__name__)
39 log = logging.getLogger(__name__)
39
40
40 class SettingsController(BaseController):
41 class SettingsController(BaseController):
@@ -72,6 +73,7 b' class SettingsController(BaseController)'
72
73
73 def update(self, repo_name):
74 def update(self, repo_name):
74 repo_model = RepoModel()
75 repo_model = RepoModel()
76 changed_name = repo_name
75 _form = RepoSettingsForm(edit=True, old_data={'repo_name':repo_name})()
77 _form = RepoSettingsForm(edit=True, old_data={'repo_name':repo_name})()
76 try:
78 try:
77 form_result = _form.to_python(dict(request.POST))
79 form_result = _form.to_python(dict(request.POST))
@@ -79,7 +81,7 b' class SettingsController(BaseController)'
79 invalidate_cache('cached_repo_list')
81 invalidate_cache('cached_repo_list')
80 h.flash(_('Repository %s updated succesfully' % repo_name),
82 h.flash(_('Repository %s updated succesfully' % repo_name),
81 category='success')
83 category='success')
82
84 changed_name = form_result['repo_name']
83 except formencode.Invalid as errors:
85 except formencode.Invalid as errors:
84 c.repo_info = repo_model.get(repo_name)
86 c.repo_info = repo_model.get(repo_name)
85 c.users_array = repo_model.get_users_js()
87 c.users_array = repo_model.get_users_js()
@@ -93,6 +95,6 b' class SettingsController(BaseController)'
93 except Exception:
95 except Exception:
94 log.error(traceback.format_exc())
96 log.error(traceback.format_exc())
95 h.flash(_('error occured during update of repository %s') \
97 h.flash(_('error occured during update of repository %s') \
96 % form_result['repo_name'], category='error')
98 % repo_name, category='error')
97
99
98 return redirect(url('repo_settings_home', repo_name=form_result['repo_name']))
100 return redirect(url('repo_settings_home', repo_name=changed_name))
@@ -106,12 +106,17 b' def set_base_path(config):'
106 config['base_path'] = config['pylons.app_globals'].base_path
106 config['base_path'] = config['pylons.app_globals'].base_path
107
107
108 def fill_perms(user):
108 def fill_perms(user):
109 """
110 Fills user permission attribute with permissions taken from database
111 @param user:
112 """
113
109 sa = meta.Session
114 sa = meta.Session
110 user.permissions['repositories'] = {}
115 user.permissions['repositories'] = {}
111
116
112 #first fetch default permissions
117 #first fetch default permissions
113 default_perms = sa.query(Repo2Perm, Repository, Permission)\
118 default_perms = sa.query(Repo2Perm, Repository, Permission)\
114 .join((Repository, Repo2Perm.repository == Repository.repo_name))\
119 .join((Repository, Repo2Perm.repository_id == Repository.repo_id))\
115 .join((Permission, Repo2Perm.permission_id == Permission.permission_id))\
120 .join((Permission, Repo2Perm.permission_id == Permission.permission_id))\
116 .filter(Repo2Perm.user_id == sa.query(User).filter(User.username ==
121 .filter(Repo2Perm.user_id == sa.query(User).filter(User.username ==
117 'default').one().user_id).all()
122 'default').one().user_id).all()
@@ -121,7 +126,7 b' def fill_perms(user):'
121 #admin have all rights full
126 #admin have all rights full
122 for perm in default_perms:
127 for perm in default_perms:
123 p = 'repository.admin'
128 p = 'repository.admin'
124 user.permissions['repositories'][perm.Repo2Perm.repository] = p
129 user.permissions['repositories'][perm.Repo2Perm.repository.repo_name] = p
125
130
126 else:
131 else:
127 user.permissions['global'] = set()
132 user.permissions['global'] = set()
@@ -135,11 +140,11 b' def fill_perms(user):'
135 else:
140 else:
136 p = perm.Permission.permission_name
141 p = perm.Permission.permission_name
137
142
138 user.permissions['repositories'][perm.Repo2Perm.repository] = p
143 user.permissions['repositories'][perm.Repo2Perm.repository.repo_name] = p
139
144
140
145
141 user_perms = sa.query(Repo2Perm, Permission, Repository)\
146 user_perms = sa.query(Repo2Perm, Permission, Repository)\
142 .join((Repository, Repo2Perm.repository == Repository.repo_name))\
147 .join((Repository, Repo2Perm.repository_id == Repository.repo_id))\
143 .join((Permission, Repo2Perm.permission_id == Permission.permission_id))\
148 .join((Permission, Repo2Perm.permission_id == Permission.permission_id))\
144 .filter(Repo2Perm.user_id == user.user_id).all()
149 .filter(Repo2Perm.user_id == user.user_id).all()
145 #overwrite userpermissions with defaults
150 #overwrite userpermissions with defaults
@@ -149,7 +154,7 b' def fill_perms(user):'
149 p = 'repository.write'
154 p = 'repository.write'
150 else:
155 else:
151 p = perm.Permission.permission_name
156 p = perm.Permission.permission_name
152 user.permissions['repositories'][perm.Repo2Perm.repository] = p
157 user.permissions['repositories'][perm.Repo2Perm.repository.repo_name] = p
153 meta.Session.remove()
158 meta.Session.remove()
154 return user
159 return user
155
160
@@ -159,10 +164,9 b' def get_user(session):'
159 @param session:
164 @param session:
160 """
165 """
161 user = session.get('hg_app_user', AuthUser())
166 user = session.get('hg_app_user', AuthUser())
162
167
163 if user.is_authenticated:
168 if user.is_authenticated:
164 user = fill_perms(user)
169 user = fill_perms(user)
165
166 session['hg_app_user'] = user
170 session['hg_app_user'] = user
167 session.save()
171 session.save()
168 return user
172 return user
@@ -126,7 +126,7 b' class DbManage(object):'
126 hgsettings.app_title = 'hg-app'
126 hgsettings.app_title = 'hg-app'
127
127
128 try:
128 try:
129 self.sa.add(hooks)
129 #self.sa.add(hooks)
130 self.sa.add(web1)
130 self.sa.add(web1)
131 self.sa.add(web2)
131 self.sa.add(web2)
132 self.sa.add(web3)
132 self.sa.add(web3)
@@ -205,7 +205,7 b' def repo2db_mapper(initial_repo_list, re'
205 rm = RepoModel()
205 rm = RepoModel()
206
206
207 for name, repo in initial_repo_list.items():
207 for name, repo in initial_repo_list.items():
208 if not sa.query(Repository).get(name):
208 if not sa.query(Repository).filter(Repository.repo_name == name).scalar():
209 log.info('repository %s not found creating default', name)
209 log.info('repository %s not found creating default', name)
210
210
211 form_data = {
211 form_data = {
@@ -54,8 +54,9 b' class UserLog(Base):'
54
54
55 class Repository(Base):
55 class Repository(Base):
56 __tablename__ = 'repositories'
56 __tablename__ = 'repositories'
57 __table_args__ = {'useexisting':True}
57 __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
58 repo_name = Column("repo_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None, primary_key=True)
58 repo_id = Column("repo_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
59 repo_name = Column("repo_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
59 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None)
60 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None)
60 private = Column("private", BOOLEAN(), nullable=True, unique=None, default=None)
61 private = Column("private", BOOLEAN(), nullable=True, unique=None, default=None)
61 description = Column("description", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
62 description = Column("description", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
@@ -75,12 +76,12 b' class Permission(Base):'
75
76
76 class Repo2Perm(Base):
77 class Repo2Perm(Base):
77 __tablename__ = 'repo_to_perm'
78 __tablename__ = 'repo_to_perm'
78 __table_args__ = (UniqueConstraint('user_id', 'repository'), {'useexisting':True})
79 __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True})
79 repo2perm_id = Column("repo2perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
80 repo2perm_id = Column("repo2perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
80 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
81 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
81 permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
82 permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
82 repository = Column("repository", TEXT(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_name'), nullable=False, unique=None, default=None)
83 repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
83
84
84 user = relation('User')
85 user = relation('User')
85 permission = relation('Permission')
86 permission = relation('Permission')
86
87 repository = relation('Repository')
@@ -164,10 +164,9 b' def ValidRepoName(edit, old_data):'
164 if slug in ['_admin']:
164 if slug in ['_admin']:
165 raise formencode.Invalid(_('This repository name is disallowed'),
165 raise formencode.Invalid(_('This repository name is disallowed'),
166 value, state)
166 value, state)
167
168 if old_data.get('repo_name') != value or not edit:
167 if old_data.get('repo_name') != value or not edit:
169 sa = meta.Session
168 sa = meta.Session
170 if sa.query(Repository).get(slug):
169 if sa.query(Repository).filter(Repository.repo_name == slug).scalar():
171 raise formencode.Invalid(_('This repository already exists') ,
170 raise formencode.Invalid(_('This repository already exists') ,
172 value, state)
171 value, state)
173 meta.Session.remove()
172 meta.Session.remove()
@@ -16,21 +16,20 b''
16 # along with this program; if not, write to the Free Software
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA 02110-1301, USA.
18 # MA 02110-1301, USA.
19
20 """
19 """
21 Created on Jun 5, 2010
20 Created on Jun 5, 2010
22 model for handling repositories actions
21 model for handling repositories actions
23 @author: marcink
22 @author: marcink
24 """
23 """
25 from pylons_app.model.meta import Session
24 from datetime import datetime
25 from pylons import app_globals as g
26 from pylons_app.lib.utils import check_repo
26 from pylons_app.model.db import Repository, Repo2Perm, User, Permission
27 from pylons_app.model.db import Repository, Repo2Perm, User, Permission
27 import shutil
28 from pylons_app.model.meta import Session
29 import logging
28 import os
30 import os
29 from datetime import datetime
31 import shutil
30 from pylons_app.lib.utils import check_repo
31 from pylons import app_globals as g
32 import traceback
32 import traceback
33 import logging
34 log = logging.getLogger(__name__)
33 log = logging.getLogger(__name__)
35
34
36 class RepoModel(object):
35 class RepoModel(object):
@@ -39,7 +38,7 b' class RepoModel(object):'
39 self.sa = Session()
38 self.sa = Session()
40
39
41 def get(self, id):
40 def get(self, id):
42 return self.sa.query(Repository).get(id)
41 return self.sa.query(Repository).filter(Repository.repo_name == id).scalar()
43
42
44 def get_users_js(self):
43 def get_users_js(self):
45
44
@@ -51,42 +50,49 b' class RepoModel(object):'
51 return users_array
50 return users_array
52
51
53
52
54 def update(self, repo_id, form_data):
53 def update(self, repo_name, form_data):
55 try:
54 try:
56 if repo_id != form_data['repo_name']:
55
57 #rename our data
58 self.__rename_repo(repo_id, form_data['repo_name'])
59 cur_repo = self.sa.query(Repository).get(repo_id)
60 for k, v in form_data.items():
61 if k == 'user':
62 cur_repo.user_id = v
63 else:
64 setattr(cur_repo, k, v)
65
66 #update permissions
56 #update permissions
67 for username, perm in form_data['perms_updates']:
57 for username, perm in form_data['perms_updates']:
68 r2p = self.sa.query(Repo2Perm)\
58 r2p = self.sa.query(Repo2Perm)\
69 .filter(Repo2Perm.user == self.sa.query(User)\
59 .filter(Repo2Perm.user == self.sa.query(User)\
70 .filter(User.username == username).one())\
60 .filter(User.username == username).one())\
71 .filter(Repo2Perm.repository == repo_id).one()
61 .filter(Repo2Perm.repository == self.get(repo_name))\
62 .one()
72
63
73 r2p.permission_id = self.sa.query(Permission).filter(
64 r2p.permission_id = self.sa.query(Permission).filter(
74 Permission.permission_name ==
65 Permission.permission_name ==
75 perm).one().permission_id
66 perm).one().permission_id
76 self.sa.add(r2p)
67 self.sa.add(r2p)
77
68
69 #set new permissions
78 for username, perm in form_data['perms_new']:
70 for username, perm in form_data['perms_new']:
79 r2p = Repo2Perm()
71 r2p = Repo2Perm()
80 r2p.repository = repo_id
72 r2p.repository = self.get(repo_name)
81 r2p.user = self.sa.query(User)\
73 r2p.user = self.sa.query(User)\
82 .filter(User.username == username).one()
74 .filter(User.username == username).one()
83
75
84 r2p.permission_id = self.sa.query(Permission).filter(
76 r2p.permission_id = self.sa.query(Permission).filter(
85 Permission.permission_name ==
77 Permission.permission_name == perm)\
86 perm).one().permission_id
78 .one().permission_id
87 self.sa.add(r2p)
79 self.sa.add(r2p)
88
80
81 #update current repo
82 cur_repo = self.get(repo_name)
83
84 for k, v in form_data.items():
85 if k == 'user':
86 cur_repo.user_id = v
87 else:
88 setattr(cur_repo, k, v)
89
89 self.sa.add(cur_repo)
90 self.sa.add(cur_repo)
91
92 if repo_name != form_data['repo_name']:
93 #rename our data
94 self.__rename_repo(repo_name, form_data['repo_name'])
95
90 self.sa.commit()
96 self.sa.commit()
91 except:
97 except:
92 log.error(traceback.format_exc())
98 log.error(traceback.format_exc())
@@ -102,7 +108,7 b' class RepoModel(object):'
102
108
103 new_repo.user_id = cur_user.user_id
109 new_repo.user_id = cur_user.user_id
104 self.sa.add(new_repo)
110 self.sa.add(new_repo)
105
111
106 #create default permission
112 #create default permission
107 repo2perm = Repo2Perm()
113 repo2perm = Repo2Perm()
108 default_perm = 'repository.none' if form_data['private'] \
114 default_perm = 'repository.none' if form_data['private'] \
@@ -111,7 +117,7 b' class RepoModel(object):'
111 .filter(Permission.permission_name == default_perm)\
117 .filter(Permission.permission_name == default_perm)\
112 .one().permission_id
118 .one().permission_id
113
119
114 repo2perm.repository = repo_name
120 repo2perm.repository_id = new_repo.repo_id
115 repo2perm.user_id = self.sa.query(User)\
121 repo2perm.user_id = self.sa.query(User)\
116 .filter(User.username == 'default').one().user_id
122 .filter(User.username == 'default').one().user_id
117
123
@@ -133,10 +139,12 b' class RepoModel(object):'
133 log.error(traceback.format_exc())
139 log.error(traceback.format_exc())
134 self.sa.rollback()
140 self.sa.rollback()
135 raise
141 raise
142
136 def delete_perm_user(self, form_data, repo_name):
143 def delete_perm_user(self, form_data, repo_name):
137 try:
144 try:
138 r2p = self.sa.query(Repo2Perm).filter(Repo2Perm.repository == repo_name)\
145 self.sa.query(Repo2Perm)\
139 .filter(Repo2Perm.user_id == form_data['user_id']).delete()
146 .filter(Repo2Perm.repository == self.get(repo_name))\
147 .filter(Repo2Perm.user_id == form_data['user_id']).delete()
140 self.sa.commit()
148 self.sa.commit()
141 except:
149 except:
142 log.error(traceback.format_exc())
150 log.error(traceback.format_exc())
General Comments 0
You need to be logged in to leave comments. Login now