--- a/server/hookhelper.py Mon Nov 16 19:42:27 2009 +0100
+++ b/server/hookhelper.py Tue Nov 17 11:48:36 2009 +0100
@@ -10,14 +10,18 @@
from cubicweb import RepositoryError
from cubicweb.server.pool import SingleLastOperation
+def entity_oldnewvalue(entity, attr):
+ """returns the couple (old attr value, new attr value)
-def entity_name(session, eid):
- """return the "name" attribute of the entity with the given eid"""
- return entity_attr(session, eid, 'name')
-
-def entity_attr(session, eid, attr):
- """return an arbitrary attribute of the entity with the given eid"""
- return getattr(session.entity_from_eid(eid), attr)
+ NOTE: will only work in a before_update_entity hook
+ """
+ # get new value and remove from local dict to force a db query to
+ # fetch old value
+ newvalue = entity.pop(attr, None)
+ oldvalue = getattr(entity, attr)
+ if newvalue is not None:
+ entity[attr] = newvalue
+ return oldvalue, newvalue
def rproperty(session, rtype, eidfrom, eidto, rprop):
rschema = session.repo.schema[rtype]
@@ -29,7 +33,7 @@
"""check that the entity's name is not in the internal_names list.
raise a RepositoryError if so, else return the entity's name
"""
- name = entity_name(session, eid)
+ name = session.entity_from_eid(eid).name
if name in internal_names:
raise RepositoryError('%s entity can\'t be deleted' % name)
return name
--- a/server/schemahooks.py Mon Nov 16 19:42:27 2009 +0100
+++ b/server/schemahooks.py Tue Nov 17 11:48:36 2009 +0100
@@ -23,8 +23,7 @@
from cubicweb.server import schemaserial as ss
from cubicweb.server.sqlutils import SQL_PREFIX
from cubicweb.server.pool import Operation, SingleLastOperation, PreCommitOperation
-from cubicweb.server.hookhelper import (entity_attr, entity_name,
- check_internal_entity)
+from cubicweb.server.hookhelper import entity_oldnewvalue, check_internal_entity
TYPE_CONVERTER = { # XXX
@@ -143,6 +142,10 @@
def commit_event(self):
rebuildinfered = self.session.data.get('rebuild-infered', True)
self.repo.set_schema(self.repo.schema, rebuildinfered=rebuildinfered)
+ # CWUser class might have changed, update current session users
+ cwuser_cls = self.session.vreg['etypes'].etype_class('CWUser')
+ for session in self.repo._sessions.values():
+ session.user.__class__ = cwuser_cls
def rollback_event(self):
self.precommit_event()
@@ -182,7 +185,7 @@
def __init__(self, session, perm, etype_eid):
self.perm = perm
try:
- self.name = entity_name(session, etype_eid)
+ self.name = session.entity_from_eid(etype_eid).name
except IndexError:
self.error('changing permission of a no more existant type #%s',
etype_eid)
@@ -665,7 +668,7 @@
"""synchronize schema when a *_permission relation has been added on a group
"""
def __init__(self, session, perm, etype_eid, group_eid):
- self.group = entity_name(session, group_eid)
+ self.group = session.entity_from_eid(group_eid).name
super(MemSchemaPermissionCWGroupAdd, self).__init__(
session, perm, etype_eid)
@@ -959,10 +962,11 @@
errors = {}
# don't use getattr(entity, attr), we would get the modified value if any
for attr in ro_attrs:
- origval = entity_attr(session, entity.eid, attr)
- if entity.get(attr, origval) != origval:
- errors[attr] = session._("can't change the %s attribute") % \
- display_name(session, attr)
+ if attr in entity.edited_attributes:
+ orival, newval = entity_oldnewvalue(entity, attr)
+ if newval != origval:
+ errors[attr] = session._("can't change the %s attribute") % \
+ display_name(session, attr)
if errors:
raise ValidationError(entity.eid, errors)
@@ -970,8 +974,7 @@
"""check name change, handle final"""
check_valid_changes(session, entity, ro_attrs=('final',))
# don't use getattr(entity, attr), we would get the modified value if any
- oldname = entity_attr(session, entity.eid, 'name')
- newname = entity.get('name', oldname)
+ oldname, newname = entity_oldnewvalue(entity, 'name')
if newname.lower() != oldname.lower():
SourceDbCWETypeRename(session, oldname=oldname, newname=newname)
MemSchemaCWETypeRename(session, oldname=oldname, newname=newname)