hooks/syncschema.py
branchstable
changeset 4719 aaed3f813ef8
parent 4667 6c8eccb1b695
child 4721 8f63691ccb7f
equal deleted inserted replaced
4718:3dc3ad02d091 4719:aaed3f813ef8
    16 from yams.buildobjs import EntityType, RelationType, RelationDefinition
    16 from yams.buildobjs import EntityType, RelationType, RelationDefinition
    17 from yams.schema2sql import eschema2sql, rschema2sql, type_from_constraints
    17 from yams.schema2sql import eschema2sql, rschema2sql, type_from_constraints
    18 
    18 
    19 from logilab.common.decorators import clear_cache
    19 from logilab.common.decorators import clear_cache
    20 
    20 
    21 from cubicweb import ValidationError, RepositoryError
    21 from cubicweb import ValidationError
    22 from cubicweb.selectors import implements
    22 from cubicweb.selectors import implements
    23 from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES, CONSTRAINTS, display_name
    23 from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES, CONSTRAINTS, display_name
    24 from cubicweb.server import hook, schemaserial as ss
    24 from cubicweb.server import hook, schemaserial as ss
    25 from cubicweb.server.sqlutils import SQL_PREFIX
    25 from cubicweb.server.sqlutils import SQL_PREFIX
    26 
    26 
   937         if newvalues:
   937         if newvalues:
   938             MemSchemaCWRTypeUpdate(self._cw, rschema=rschema, values=newvalues)
   938             MemSchemaCWRTypeUpdate(self._cw, rschema=rschema, values=newvalues)
   939             SourceDbCWRTypeUpdate(self._cw, rschema=rschema, values=newvalues,
   939             SourceDbCWRTypeUpdate(self._cw, rschema=rschema, values=newvalues,
   940                                   entity=entity)
   940                                   entity=entity)
   941 
   941 
   942 def check_valid_changes(session, entity, ro_attrs=('name', 'final')):
       
   943     errors = {}
       
   944     # don't use getattr(entity, attr), we would get the modified value if any
       
   945     for attr in ro_attrs:
       
   946         if attr in entity.edited_attributes:
       
   947             origval, newval = hook.entity_oldnewvalue(entity, attr)
       
   948             if newval != origval:
       
   949                 errors[attr] = session._("can't change the %s attribute") % \
       
   950                                display_name(session, attr)
       
   951     if errors:
       
   952         raise ValidationError(entity.eid, errors)
       
   953 
       
   954 
   942 
   955 class AfterDelRelationTypeHook(SyncSchemaHook):
   943 class AfterDelRelationTypeHook(SyncSchemaHook):
   956     """before deleting a CWAttribute or CWRelation entity:
   944     """before deleting a CWAttribute or CWRelation entity:
   957     * if this is a final or inlined relation definition, instantiate an
   945     * if this is a final or inlined relation definition, instantiate an
   958       operation to drop necessary column, else if this is the last instance
   946       operation to drop necessary column, else if this is the last instance