|
1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
1 """Core hooks: synchronize living session on persistent data changes |
18 """Core hooks: synchronize living session on persistent data changes |
2 |
19 |
3 :organization: Logilab |
|
4 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
|
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
|
7 """ |
20 """ |
8 __docformat__ = "restructuredtext en" |
21 __docformat__ = "restructuredtext en" |
9 |
22 |
|
23 from yams.schema import role_name |
10 from cubicweb import UnknownProperty, ValidationError, BadConnectionId |
24 from cubicweb import UnknownProperty, ValidationError, BadConnectionId |
11 from cubicweb.selectors import implements |
25 from cubicweb.selectors import implements |
12 from cubicweb.server import hook |
26 from cubicweb.server import hook |
13 |
27 |
14 |
28 |
145 key, value = self.entity.pkey, self.entity.value |
159 key, value = self.entity.pkey, self.entity.value |
146 session = self._cw |
160 session = self._cw |
147 try: |
161 try: |
148 value = session.vreg.typed_value(key, value) |
162 value = session.vreg.typed_value(key, value) |
149 except UnknownProperty: |
163 except UnknownProperty: |
|
164 qname = role_name('pkey', 'subject') |
150 raise ValidationError(self.entity.eid, |
165 raise ValidationError(self.entity.eid, |
151 {'pkey': session._('unknown property key')}) |
166 {qname: session._('unknown property key')}) |
152 except ValueError, ex: |
167 except ValueError, ex: |
|
168 qname = role_name('value', 'subject') |
153 raise ValidationError(self.entity.eid, |
169 raise ValidationError(self.entity.eid, |
154 {'value': session._(str(ex))}) |
170 {qname: session._(str(ex))}) |
155 if not session.user.matching_groups('managers'): |
171 if not session.user.matching_groups('managers'): |
156 session.add_relation(self.entity.eid, 'for_user', session.user.eid) |
172 session.add_relation(self.entity.eid, 'for_user', session.user.eid) |
157 else: |
173 else: |
158 _AddCWPropertyOp(session, cwprop=self.entity) |
174 _AddCWPropertyOp(session, cwprop=self.entity) |
159 |
175 |
172 try: |
188 try: |
173 value = session.vreg.typed_value(key, value) |
189 value = session.vreg.typed_value(key, value) |
174 except UnknownProperty: |
190 except UnknownProperty: |
175 return |
191 return |
176 except ValueError, ex: |
192 except ValueError, ex: |
177 raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
193 qname = role_name('value', 'subject') |
|
194 raise ValidationError(entity.eid, {qname: session._(str(ex))}) |
178 if entity.for_user: |
195 if entity.for_user: |
179 for session_ in get_user_sessions(session.repo, entity.for_user[0].eid): |
196 for session_ in get_user_sessions(session.repo, entity.for_user[0].eid): |
180 _ChangeCWPropertyOp(session, cwpropdict=session_.user.properties, |
197 _ChangeCWPropertyOp(session, cwpropdict=session_.user.properties, |
181 key=key, value=value) |
198 key=key, value=value) |
182 else: |
199 else: |
212 if not session.describe(eidfrom)[0] == 'CWProperty': |
229 if not session.describe(eidfrom)[0] == 'CWProperty': |
213 return |
230 return |
214 key, value = session.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V', |
231 key, value = session.execute('Any K,V WHERE P eid %(x)s,P pkey K,P value V', |
215 {'x': eidfrom}, 'x')[0] |
232 {'x': eidfrom}, 'x')[0] |
216 if session.vreg.property_info(key)['sitewide']: |
233 if session.vreg.property_info(key)['sitewide']: |
|
234 qname = role_name('for_user', 'subject') |
|
235 msg = session._("site-wide property can't be set for user") |
217 raise ValidationError(eidfrom, |
236 raise ValidationError(eidfrom, |
218 {'for_user': session._("site-wide property can't be set for user")}) |
237 {qname: msg}) |
219 for session_ in get_user_sessions(session.repo, self.eidto): |
238 for session_ in get_user_sessions(session.repo, self.eidto): |
220 _ChangeCWPropertyOp(session, cwpropdict=session_.user.properties, |
239 _ChangeCWPropertyOp(session, cwpropdict=session_.user.properties, |
221 key=key, value=value) |
240 key=key, value=value) |
222 |
241 |
223 |
242 |