[server] remove not-so-useful entity_name and entity_attr functions, introduce entity_oldnewvalue
entity_oldnewvalue(entity, attr) is usable in before_update_entity and
returns both old and new value of entity
--- a/server/hookhelper.py Mon Nov 16 17:20:03 2009 +0100
+++ b/server/hookhelper.py Tue Nov 17 11:46:07 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 17:20:03 2009 +0100
+++ b/server/schemahooks.py Tue Nov 17 11:46:07 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
@@ -182,7 +181,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 +664,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 +958,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 +970,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)