110 """a user's custom properties has been deleted""" |
110 """a user's custom properties has been deleted""" |
111 |
111 |
112 def commit_event(self): |
112 def commit_event(self): |
113 """the observed connections pool has been commited""" |
113 """the observed connections pool has been commited""" |
114 try: |
114 try: |
115 del self.epropdict[self.key] |
115 del self.cwpropdict[self.key] |
116 except KeyError: |
116 except KeyError: |
117 self.error('%s has no associated value', self.key) |
117 self.error('%s has no associated value', self.key) |
118 |
118 |
119 |
119 |
120 class _ChangeCWPropertyOp(hook.Operation): |
120 class _ChangeCWPropertyOp(hook.Operation): |
121 """a user's custom properties has been added/changed""" |
121 """a user's custom properties has been added/changed""" |
122 |
122 |
123 def commit_event(self): |
123 def commit_event(self): |
124 """the observed connections pool has been commited""" |
124 """the observed connections pool has been commited""" |
125 self.epropdict[self.key] = self.value |
125 self.cwpropdict[self.key] = self.value |
126 |
126 |
127 |
127 |
128 class _AddCWPropertyOp(hook.Operation): |
128 class _AddCWPropertyOp(hook.Operation): |
129 """a user's custom properties has been added/changed""" |
129 """a user's custom properties has been added/changed""" |
130 |
130 |
131 def commit_event(self): |
131 def commit_event(self): |
132 """the observed connections pool has been commited""" |
132 """the observed connections pool has been commited""" |
133 eprop = self.eprop |
133 cwprop = self.cwprop |
134 if not eprop.for_user: |
134 if not cwprop.for_user: |
135 self.repo.vreg.eprop_values[eprop.pkey] = eprop.value |
135 self.repo.vreg['propertyvalues'][cwprop.pkey] = cwprop.value |
136 # if for_user is set, update is handled by a ChangeCWPropertyOp operation |
136 # if for_user is set, update is handled by a ChangeCWPropertyOp operation |
137 |
137 |
138 |
138 |
139 class AddCWPropertyHook(SyncSessionHook): |
139 class AddCWPropertyHook(SyncSessionHook): |
140 __id__ = 'addcwprop' |
140 __id__ = 'addcwprop' |
153 raise ValidationError(self.entity.eid, |
153 raise ValidationError(self.entity.eid, |
154 {'value': session._(str(ex))}) |
154 {'value': session._(str(ex))}) |
155 if not session.user.matching_groups('managers'): |
155 if not session.user.matching_groups('managers'): |
156 session.add_relation(entity.eid, 'for_user', session.user.eid) |
156 session.add_relation(entity.eid, 'for_user', session.user.eid) |
157 else: |
157 else: |
158 _AddCWPropertyOp(session, eprop=entity) |
158 _AddCWPropertyOp(session, cwprop=self.entity) |
159 |
159 |
160 |
160 |
161 class UpdateCWPropertyHook(AddCWPropertyHook): |
161 class UpdateCWPropertyHook(AddCWPropertyHook): |
162 __id__ = 'updatecwprop' |
162 __id__ = 'updatecwprop' |
163 events = ('after_update_entity',) |
163 events = ('after_update_entity',) |
175 return |
175 return |
176 except ValueError, ex: |
176 except ValueError, ex: |
177 raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
177 raise ValidationError(entity.eid, {'value': session._(str(ex))}) |
178 if entity.for_user: |
178 if entity.for_user: |
179 for session_ in get_user_sessions(session.repo, entity.for_user[0].eid): |
179 for session_ in get_user_sessions(session.repo, entity.for_user[0].eid): |
180 _ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
180 _ChangeCWPropertyOp(session, cwpropdict=session_.user.properties, |
181 key=key, value=value) |
181 key=key, value=value) |
182 else: |
182 else: |
183 # site wide properties |
183 # site wide properties |
184 _ChangeCWPropertyOp(session, epropdict=session.vreg.eprop_values, |
184 _ChangeCWPropertyOp(session, cwpropdict=session.vreg['propertyvalues'], |
185 key=key, value=value) |
185 key=key, value=value) |
186 |
186 |
187 |
187 |
188 class DeleteCWPropertyHook(AddCWPropertyHook): |
188 class DeleteCWPropertyHook(AddCWPropertyHook): |
189 __id__ = 'delcwprop' |
189 __id__ = 'delcwprop' |
195 for eidfrom, rtype, eidto in session.transaction_data.get('pendingrelations', ()): |
195 for eidfrom, rtype, eidto in session.transaction_data.get('pendingrelations', ()): |
196 if rtype == 'for_user' and eidfrom == self.entity.eid: |
196 if rtype == 'for_user' and eidfrom == self.entity.eid: |
197 # if for_user was set, delete has already been handled |
197 # if for_user was set, delete has already been handled |
198 break |
198 break |
199 else: |
199 else: |
200 _DelCWPropertyOp(session, epropdict=session.vreg.eprop_values, key=entity.pkey) |
200 _DelCWPropertyOp(session, cwpropdict=session.vreg['propertyvalues'], key=entity.pkey) |
201 |
201 |
202 |
202 |
203 class AddForUserRelationHook(SyncSessionHook): |
203 class AddForUserRelationHook(SyncSessionHook): |
204 __id__ = 'addcwpropforuser' |
204 __id__ = 'addcwpropforuser' |
205 __select__ = SyncSessionHook.__select__ & hook.match_rtype('for_user') |
205 __select__ = SyncSessionHook.__select__ & hook.match_rtype('for_user') |
214 {'x': eidfrom}, 'x')[0] |
214 {'x': eidfrom}, 'x')[0] |
215 if session.vreg.property_info(key)['sitewide']: |
215 if session.vreg.property_info(key)['sitewide']: |
216 raise ValidationError(eidfrom, |
216 raise ValidationError(eidfrom, |
217 {'for_user': session._("site-wide property can't be set for user")}) |
217 {'for_user': session._("site-wide property can't be set for user")}) |
218 for session_ in get_user_sessions(session.repo, self.eidto): |
218 for session_ in get_user_sessions(session.repo, self.eidto): |
219 _ChangeCWPropertyOp(session, epropdict=session_.user.properties, |
219 _ChangeCWPropertyOp(session, cwpropdict=session_.user.properties, |
220 key=key, value=value) |
220 key=key, value=value) |
221 |
221 |
222 |
222 |
223 class DelForUserRelationHook(AddForUserRelationHook): |
223 class DelForUserRelationHook(AddForUserRelationHook): |
224 __id__ = 'delcwpropforuser' |
224 __id__ = 'delcwpropforuser' |
229 key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
229 key = session.execute('Any K WHERE P eid %(x)s, P pkey K', |
230 {'x': self.eidfrom}, 'x')[0][0] |
230 {'x': self.eidfrom}, 'x')[0][0] |
231 session.transaction_data.setdefault('pendingrelations', []).append( |
231 session.transaction_data.setdefault('pendingrelations', []).append( |
232 (self.eidfrom, self.rtype, self.eidto)) |
232 (self.eidfrom, self.rtype, self.eidto)) |
233 for session_ in get_user_sessions(session.repo, self.eidto): |
233 for session_ in get_user_sessions(session.repo, self.eidto): |
234 _DelCWPropertyOp(session, epropdict=session_.user.properties, key=key) |
234 _DelCWPropertyOp(session, cwpropdict=session_.user.properties, key=key) |