hooks/syncschema.py
changeset 6142 8bc6eac1fac1
parent 6097 27e48fb7f113
child 6173 eb386e473044
equal deleted inserted replaced
6141:b8287e54b528 6142:8bc6eac1fac1
   126 
   126 
   127 
   127 
   128 def check_valid_changes(session, entity, ro_attrs=('name', 'final')):
   128 def check_valid_changes(session, entity, ro_attrs=('name', 'final')):
   129     errors = {}
   129     errors = {}
   130     # don't use getattr(entity, attr), we would get the modified value if any
   130     # don't use getattr(entity, attr), we would get the modified value if any
   131     for attr in entity.edited_attributes:
   131     for attr in entity.cw_edited:
   132         if attr in ro_attrs:
   132         if attr in ro_attrs:
   133             newval = entity.pop(attr)
   133             origval, newval = entity.cw_edited.oldnewvalue(attr)
   134             origval = getattr(entity, attr)
       
   135             if newval != origval:
   134             if newval != origval:
   136                 errors[attr] = session._("can't change the %s attribute") % \
   135                 errors[attr] = session._("can't change the %s attribute") % \
   137                                display_name(session, attr)
   136                                display_name(session, attr)
   138             entity[attr] = newval
       
   139     if errors:
   137     if errors:
   140         raise ValidationError(entity.eid, errors)
   138         raise ValidationError(entity.eid, errors)
   141 
   139 
   142 
   140 
   143 class SyncSchemaHook(hook.Hook):
   141 class SyncSchemaHook(hook.Hook):
   860     __regid__ = 'syncaddcwetype'
   858     __regid__ = 'syncaddcwetype'
   861     events = ('after_add_entity',)
   859     events = ('after_add_entity',)
   862 
   860 
   863     def __call__(self):
   861     def __call__(self):
   864         entity = self.entity
   862         entity = self.entity
   865         if entity.get('final'):
   863         if entity.cw_edited.get('final'):
   866             return
   864             return
   867         CWETypeAddOp(self._cw, entity=entity)
   865         CWETypeAddOp(self._cw, entity=entity)
   868 
   866 
   869 
   867 
   870 class BeforeUpdateCWETypeHook(DelCWETypeHook):
   868 class BeforeUpdateCWETypeHook(DelCWETypeHook):
   874 
   872 
   875     def __call__(self):
   873     def __call__(self):
   876         entity = self.entity
   874         entity = self.entity
   877         check_valid_changes(self._cw, entity, ro_attrs=('final',))
   875         check_valid_changes(self._cw, entity, ro_attrs=('final',))
   878         # don't use getattr(entity, attr), we would get the modified value if any
   876         # don't use getattr(entity, attr), we would get the modified value if any
   879         if 'name' in entity.edited_attributes:
   877         if 'name' in entity.cw_edited:
   880             oldname, newname = hook.entity_oldnewvalue(entity, 'name')
   878             oldname, newname = entity.cw_edited.oldnewvalue('name')
   881             if newname.lower() != oldname.lower():
   879             if newname.lower() != oldname.lower():
   882                 CWETypeRenameOp(self._cw, oldname=oldname, newname=newname)
   880                 CWETypeRenameOp(self._cw, oldname=oldname, newname=newname)
   883 
   881 
   884 
   882 
   885 # CWRType hooks ################################################################
   883 # CWRType hooks ################################################################
   918 
   916 
   919     def __call__(self):
   917     def __call__(self):
   920         entity = self.entity
   918         entity = self.entity
   921         rtypedef = ybo.RelationType(name=entity.name,
   919         rtypedef = ybo.RelationType(name=entity.name,
   922                                     description=entity.description,
   920                                     description=entity.description,
   923                                     inlined=entity.get('inlined', False),
   921                                     inlined=entity.cw_edited.get('inlined', False),
   924                                     symmetric=entity.get('symmetric', False),
   922                                     symmetric=entity.cw_edited.get('symmetric', False),
   925                                     eid=entity.eid)
   923                                     eid=entity.eid)
   926         MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef)
   924         MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef)
   927 
   925 
   928 
   926 
   929 class BeforeUpdateCWRTypeHook(DelCWRTypeHook):
   927 class BeforeUpdateCWRTypeHook(DelCWRTypeHook):
   934     def __call__(self):
   932     def __call__(self):
   935         entity = self.entity
   933         entity = self.entity
   936         check_valid_changes(self._cw, entity)
   934         check_valid_changes(self._cw, entity)
   937         newvalues = {}
   935         newvalues = {}
   938         for prop in ('symmetric', 'inlined', 'fulltext_container'):
   936         for prop in ('symmetric', 'inlined', 'fulltext_container'):
   939             if prop in entity.edited_attributes:
   937             if prop in entity.cw_edited:
   940                 old, new = hook.entity_oldnewvalue(entity, prop)
   938                 old, new = entity.cw_edited.oldnewvalue(prop)
   941                 if old != new:
   939                 if old != new:
   942                     newvalues[prop] = entity[prop]
   940                     newvalues[prop] = new
   943         if newvalues:
   941         if newvalues:
   944             rschema = self._cw.vreg.schema.rschema(entity.name)
   942             rschema = self._cw.vreg.schema.rschema(entity.name)
   945             CWRTypeUpdateOp(self._cw, rschema=rschema, entity=entity,
   943             CWRTypeUpdateOp(self._cw, rschema=rschema, entity=entity,
   946                             values=newvalues)
   944                             values=newvalues)
   947 
   945 
  1022                 continue
  1020                 continue
  1023             if prop == 'order':
  1021             if prop == 'order':
  1024                 attr = 'ordernum'
  1022                 attr = 'ordernum'
  1025             else:
  1023             else:
  1026                 attr = prop
  1024                 attr = prop
  1027             if attr in entity.edited_attributes:
  1025             if attr in entity.cw_edited:
  1028                 old, new = hook.entity_oldnewvalue(entity, attr)
  1026                 old, new = entity.cw_edited.oldnewvalue(attr)
  1029                 if old != new:
  1027                 if old != new:
  1030                     newvalues[prop] = new
  1028                     newvalues[prop] = new
  1031         if newvalues:
  1029         if newvalues:
  1032             RDefUpdateOp(self._cw, rschema=rschema, rdefkey=(subjtype, objtype),
  1030             RDefUpdateOp(self._cw, rschema=rschema, rdefkey=(subjtype, objtype),
  1033                          values=newvalues)
  1031                          values=newvalues)