hooks/syncsession.py
changeset 6142 8bc6eac1fac1
parent 5877 0c7b7b76a84f
child 6628 7cf5d55cbb8a
equal deleted inserted replaced
6141:b8287e54b528 6142:8bc6eac1fac1
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # details.
    14 # details.
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    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/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """Core hooks: synchronize living session on persistent data changes
    18 """Core hooks: synchronize living session on persistent data changes"""
    19 
    19 
    20 """
       
    21 __docformat__ = "restructuredtext en"
    20 __docformat__ = "restructuredtext en"
    22 
    21 
    23 from yams.schema import role_name
    22 from yams.schema import role_name
    24 from cubicweb import UnknownProperty, ValidationError, BadConnectionId
    23 from cubicweb import UnknownProperty, ValidationError, BadConnectionId
    25 from cubicweb.selectors import is_instance
    24 from cubicweb.selectors import is_instance
    54         self.group = result[0][0]
    53         self.group = result[0][0]
    55 
    54 
    56 
    55 
    57 class _DeleteGroupOp(_GroupOperation):
    56 class _DeleteGroupOp(_GroupOperation):
    58     """synchronize user when a in_group relation has been deleted"""
    57     """synchronize user when a in_group relation has been deleted"""
    59     def commit_event(self):
    58     def postcommit_event(self):
    60         """the observed connections pool has been commited"""
    59         """the observed connections pool has been commited"""
    61         groups = self.cnxuser.groups
    60         groups = self.cnxuser.groups
    62         try:
    61         try:
    63             groups.remove(self.group)
    62             groups.remove(self.group)
    64         except KeyError:
    63         except KeyError:
    65             self.error('user %s not in group %s',  self.cnxuser, self.group)
    64             self.error('user %s not in group %s',  self.cnxuser, self.group)
    66             return
       
    67 
    65 
    68 
    66 
    69 class _AddGroupOp(_GroupOperation):
    67 class _AddGroupOp(_GroupOperation):
    70     """synchronize user when a in_group relation has been added"""
    68     """synchronize user when a in_group relation has been added"""
    71     def commit_event(self):
    69     def postcommit_event(self):
    72         """the observed connections pool has been commited"""
    70         """the observed connections pool has been commited"""
    73         groups = self.cnxuser.groups
    71         groups = self.cnxuser.groups
    74         if self.group in groups:
    72         if self.group in groups:
    75             self.warning('user %s already in group %s', self.cnxuser,
    73             self.warning('user %s already in group %s', self.cnxuser,
    76                          self.group)
    74                          self.group)
    77             return
    75         else:
    78         groups.add(self.group)
    76             groups.add(self.group)
    79 
    77 
    80 
    78 
    81 class SyncInGroupHook(SyncSessionHook):
    79 class SyncInGroupHook(SyncSessionHook):
    82     __regid__ = 'syncingroup'
    80     __regid__ = 'syncingroup'
    83     __select__ = SyncSessionHook.__select__ & hook.match_rtype('in_group')
    81     __select__ = SyncSessionHook.__select__ & hook.match_rtype('in_group')
    96     """close associated user's session when it is deleted"""
    94     """close associated user's session when it is deleted"""
    97     def __init__(self, session, cnxid):
    95     def __init__(self, session, cnxid):
    98         self.cnxid = cnxid
    96         self.cnxid = cnxid
    99         hook.Operation.__init__(self, session)
    97         hook.Operation.__init__(self, session)
   100 
    98 
   101     def commit_event(self):
    99     def postcommit_event(self):
   102         """the observed connections pool has been commited"""
   100         """the observed connections pool has been commited"""
   103         try:
   101         try:
   104             self.session.repo.close(self.cnxid)
   102             self.session.repo.close(self.cnxid)
   105         except BadConnectionId:
   103         except BadConnectionId:
   106             pass # already closed
   104             pass # already closed
   121 
   119 
   122 
   120 
   123 class _DelCWPropertyOp(hook.Operation):
   121 class _DelCWPropertyOp(hook.Operation):
   124     """a user's custom properties has been deleted"""
   122     """a user's custom properties has been deleted"""
   125 
   123 
   126     def commit_event(self):
   124     def postcommit_event(self):
   127         """the observed connections pool has been commited"""
   125         """the observed connections pool has been commited"""
   128         try:
   126         try:
   129             del self.cwpropdict[self.key]
   127             del self.cwpropdict[self.key]
   130         except KeyError:
   128         except KeyError:
   131             self.error('%s has no associated value', self.key)
   129             self.error('%s has no associated value', self.key)
   132 
   130 
   133 
   131 
   134 class _ChangeCWPropertyOp(hook.Operation):
   132 class _ChangeCWPropertyOp(hook.Operation):
   135     """a user's custom properties has been added/changed"""
   133     """a user's custom properties has been added/changed"""
   136 
   134 
   137     def commit_event(self):
   135     def postcommit_event(self):
   138         """the observed connections pool has been commited"""
   136         """the observed connections pool has been commited"""
   139         self.cwpropdict[self.key] = self.value
   137         self.cwpropdict[self.key] = self.value
   140 
   138 
   141 
   139 
   142 class _AddCWPropertyOp(hook.Operation):
   140 class _AddCWPropertyOp(hook.Operation):
   143     """a user's custom properties has been added/changed"""
   141     """a user's custom properties has been added/changed"""
   144 
   142 
   145     def commit_event(self):
   143     def postcommit_event(self):
   146         """the observed connections pool has been commited"""
   144         """the observed connections pool has been commited"""
   147         cwprop = self.cwprop
   145         cwprop = self.cwprop
   148         if not cwprop.for_user:
   146         if not cwprop.for_user:
   149             self.session.vreg['propertyvalues'][cwprop.pkey] = cwprop.value
   147             self.session.vreg['propertyvalues'][cwprop.pkey] = cwprop.value
   150         # if for_user is set, update is handled by a ChangeCWPropertyOp operation
   148         # if for_user is set, update is handled by a ChangeCWPropertyOp operation
   178     __regid__ = 'updatecwprop'
   176     __regid__ = 'updatecwprop'
   179     events = ('after_update_entity',)
   177     events = ('after_update_entity',)
   180 
   178 
   181     def __call__(self):
   179     def __call__(self):
   182         entity = self.entity
   180         entity = self.entity
   183         if not ('pkey' in entity.edited_attributes or
   181         if not ('pkey' in entity.cw_edited or
   184                 'value' in entity.edited_attributes):
   182                 'value' in entity.cw_edited):
   185             return
   183             return
   186         key, value = entity.pkey, entity.value
   184         key, value = entity.pkey, entity.value
   187         session = self._cw
   185         session = self._cw
   188         try:
   186         try:
   189             value = session.vreg.typed_value(key, value)
   187             value = session.vreg.typed_value(key, value)