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