hooks/syncschema.py
changeset 8556 bbe0d6985e59
parent 8534 6ed331fd4347
child 8695 358d8bed9626
equal deleted inserted replaced
8555:c747242d22a6 8556:bbe0d6985e59
     1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    22 
    22 
    23 checking for schema consistency is done in hooks.py
    23 checking for schema consistency is done in hooks.py
    24 """
    24 """
    25 
    25 
    26 __docformat__ = "restructuredtext en"
    26 __docformat__ = "restructuredtext en"
       
    27 _ = unicode
    27 
    28 
    28 from copy import copy
    29 from copy import copy
    29 from yams.schema import BASE_TYPES, RelationSchema, RelationDefinitionSchema
    30 from yams.schema import BASE_TYPES, RelationSchema, RelationDefinitionSchema
    30 from yams import buildobjs as ybo, schema2sql as y2sql
    31 from yams import buildobjs as ybo, schema2sql as y2sql
    31 
    32 
    32 from logilab.common.decorators import clear_cache
    33 from logilab.common.decorators import clear_cache
    33 
    34 
    34 from cubicweb import ValidationError
    35 from cubicweb import validation_error
    35 from cubicweb.predicates import is_instance
    36 from cubicweb.predicates import is_instance
    36 from cubicweb.schema import (SCHEMA_TYPES, META_RTYPES, VIRTUAL_RTYPES,
    37 from cubicweb.schema import (SCHEMA_TYPES, META_RTYPES, VIRTUAL_RTYPES,
    37                              CONSTRAINTS, ETYPE_NAME_MAP, display_name)
    38                              CONSTRAINTS, ETYPE_NAME_MAP, display_name)
    38 from cubicweb.server import hook, schemaserial as ss
    39 from cubicweb.server import hook, schemaserial as ss
    39 from cubicweb.server.sqlutils import SQL_PREFIX
    40 from cubicweb.server.sqlutils import SQL_PREFIX
   125     # don't use getattr(entity, attr), we would get the modified value if any
   126     # don't use getattr(entity, attr), we would get the modified value if any
   126     for attr in entity.cw_edited:
   127     for attr in entity.cw_edited:
   127         if attr in ro_attrs:
   128         if attr in ro_attrs:
   128             origval, newval = entity.cw_edited.oldnewvalue(attr)
   129             origval, newval = entity.cw_edited.oldnewvalue(attr)
   129             if newval != origval:
   130             if newval != origval:
   130                 errors[attr] = session._("can't change the %s attribute") % \
   131                 errors[attr] = _("can't change this attribute")
   131                                display_name(session, attr)
       
   132     if errors:
   132     if errors:
   133         raise ValidationError(entity.eid, errors)
   133         raise validation_error(entity, errors)
   134 
   134 
   135 
   135 
   136 class _MockEntity(object): # XXX use a named tuple with python 2.6
   136 class _MockEntity(object): # XXX use a named tuple with python 2.6
   137     def __init__(self, eid):
   137     def __init__(self, eid):
   138         self.eid = eid
   138         self.eid = eid
   911 
   911 
   912     def __call__(self):
   912     def __call__(self):
   913         # final entities can't be deleted, don't care about that
   913         # final entities can't be deleted, don't care about that
   914         name = self.entity.name
   914         name = self.entity.name
   915         if name in CORE_TYPES:
   915         if name in CORE_TYPES:
   916             raise ValidationError(self.entity.eid, {None: self._cw._('can\'t be deleted')})
   916             raise validation_error(self.entity, {None: _("can't be deleted")})
   917         # delete every entities of this type
   917         # delete every entities of this type
   918         if name not in ETYPE_NAME_MAP:
   918         if name not in ETYPE_NAME_MAP:
   919             self._cw.execute('DELETE %s X' % name)
   919             self._cw.execute('DELETE %s X' % name)
   920             MemSchemaCWETypeDel(self._cw, etype=name)
   920             MemSchemaCWETypeDel(self._cw, etype=name)
   921         DropTable(self._cw, table=SQL_PREFIX + name)
   921         DropTable(self._cw, table=SQL_PREFIX + name)
   981     events = ('before_delete_entity',)
   981     events = ('before_delete_entity',)
   982 
   982 
   983     def __call__(self):
   983     def __call__(self):
   984         name = self.entity.name
   984         name = self.entity.name
   985         if name in CORE_TYPES:
   985         if name in CORE_TYPES:
   986             raise ValidationError(self.entity.eid, {None: self._cw._('can\'t be deleted')})
   986             raise validation_error(self.entity, {None: _("can't be deleted")})
   987         # delete relation definitions using this relation type
   987         # delete relation definitions using this relation type
   988         self._cw.execute('DELETE CWAttribute X WHERE X relation_type Y, Y eid %(x)s',
   988         self._cw.execute('DELETE CWAttribute X WHERE X relation_type Y, Y eid %(x)s',
   989                         {'x': self.entity.eid})
   989                         {'x': self.entity.eid})
   990         self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
   990         self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
   991                         {'x': self.entity.eid})
   991                         {'x': self.entity.eid})