21 from cubicweb import ValidationError, RepositoryError |
21 from cubicweb import ValidationError, RepositoryError |
22 from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES, CONSTRAINTS |
22 from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES, CONSTRAINTS |
23 from cubicweb.server import schemaserial as ss |
23 from cubicweb.server import schemaserial as ss |
24 from cubicweb.server.sqlutils import SQL_PREFIX |
24 from cubicweb.server.sqlutils import SQL_PREFIX |
25 from cubicweb.server.pool import Operation, SingleLastOperation, PreCommitOperation |
25 from cubicweb.server.pool import Operation, SingleLastOperation, PreCommitOperation |
26 from cubicweb.server.hookhelper import (entity_attr, entity_name, |
26 from cubicweb.server.hookhelper import entity_oldnewvalue, check_internal_entity |
27 check_internal_entity) |
|
28 |
27 |
29 |
28 |
30 TYPE_CONVERTER = { # XXX |
29 TYPE_CONVERTER = { # XXX |
31 'Boolean': bool, |
30 'Boolean': bool, |
32 'Int': int, |
31 'Int': int, |
180 class MemSchemaPermissionOperation(MemSchemaOperation): |
179 class MemSchemaPermissionOperation(MemSchemaOperation): |
181 """base class to synchronize schema permission definitions""" |
180 """base class to synchronize schema permission definitions""" |
182 def __init__(self, session, perm, etype_eid): |
181 def __init__(self, session, perm, etype_eid): |
183 self.perm = perm |
182 self.perm = perm |
184 try: |
183 try: |
185 self.name = entity_name(session, etype_eid) |
184 self.name = session.entity_from_eid(etype_eid).name |
186 except IndexError: |
185 except IndexError: |
187 self.error('changing permission of a no more existant type #%s', |
186 self.error('changing permission of a no more existant type #%s', |
188 etype_eid) |
187 etype_eid) |
189 else: |
188 else: |
190 Operation.__init__(self, session) |
189 Operation.__init__(self, session) |
663 |
662 |
664 class MemSchemaPermissionCWGroupAdd(MemSchemaPermissionOperation): |
663 class MemSchemaPermissionCWGroupAdd(MemSchemaPermissionOperation): |
665 """synchronize schema when a *_permission relation has been added on a group |
664 """synchronize schema when a *_permission relation has been added on a group |
666 """ |
665 """ |
667 def __init__(self, session, perm, etype_eid, group_eid): |
666 def __init__(self, session, perm, etype_eid, group_eid): |
668 self.group = entity_name(session, group_eid) |
667 self.group = session.entity_from_eid(group_eid).name |
669 super(MemSchemaPermissionCWGroupAdd, self).__init__( |
668 super(MemSchemaPermissionCWGroupAdd, self).__init__( |
670 session, perm, etype_eid) |
669 session, perm, etype_eid) |
671 |
670 |
672 def commit_event(self): |
671 def commit_event(self): |
673 """the observed connections pool has been commited""" |
672 """the observed connections pool has been commited""" |
957 |
956 |
958 def check_valid_changes(session, entity, ro_attrs=('name', 'final')): |
957 def check_valid_changes(session, entity, ro_attrs=('name', 'final')): |
959 errors = {} |
958 errors = {} |
960 # don't use getattr(entity, attr), we would get the modified value if any |
959 # don't use getattr(entity, attr), we would get the modified value if any |
961 for attr in ro_attrs: |
960 for attr in ro_attrs: |
962 origval = entity_attr(session, entity.eid, attr) |
961 if attr in entity.edited_attributes: |
963 if entity.get(attr, origval) != origval: |
962 orival, newval = entity_oldnewvalue(entity, attr) |
964 errors[attr] = session._("can't change the %s attribute") % \ |
963 if newval != origval: |
965 display_name(session, attr) |
964 errors[attr] = session._("can't change the %s attribute") % \ |
|
965 display_name(session, attr) |
966 if errors: |
966 if errors: |
967 raise ValidationError(entity.eid, errors) |
967 raise ValidationError(entity.eid, errors) |
968 |
968 |
969 def before_update_eetype(session, entity): |
969 def before_update_eetype(session, entity): |
970 """check name change, handle final""" |
970 """check name change, handle final""" |
971 check_valid_changes(session, entity, ro_attrs=('final',)) |
971 check_valid_changes(session, entity, ro_attrs=('final',)) |
972 # don't use getattr(entity, attr), we would get the modified value if any |
972 # don't use getattr(entity, attr), we would get the modified value if any |
973 oldname = entity_attr(session, entity.eid, 'name') |
973 oldname, newname = entity_oldnewvalue(entity, 'name') |
974 newname = entity.get('name', oldname) |
|
975 if newname.lower() != oldname.lower(): |
974 if newname.lower() != oldname.lower(): |
976 SourceDbCWETypeRename(session, oldname=oldname, newname=newname) |
975 SourceDbCWETypeRename(session, oldname=oldname, newname=newname) |
977 MemSchemaCWETypeRename(session, oldname=oldname, newname=newname) |
976 MemSchemaCWETypeRename(session, oldname=oldname, newname=newname) |
978 |
977 |
979 def before_update_ertype(session, entity): |
978 def before_update_ertype(session, entity): |