Author: phd
Date: 2004-11-30 13:08:08 +0000 (Tue, 30 Nov 2004)
New Revision: 424
Modified:
home/phd/SQLObject/inheritance/sqlobject/main.py
Log:
Merged changes from revision 423:
Removed separartion of attributes for plain setters from ._create()
because .set() does this itself. Minor optimization in kw.items().
Do not pass child-only attributes to the parent.
Modified: home/phd/SQLObject/inheritance/sqlobject/main.py
===================================================================
--- home/phd/SQLObject/inheritance/sqlobject/main.py 2004-11-30 13:04:38 UTC (rev 423)
+++ home/phd/SQLObject/inheritance/sqlobject/main.py 2004-11-30 13:08:08 UTC (rev 424)
@@ -887,8 +887,9 @@
is_column = self._SO_plainSetters.has_key
f_is_column = lambda item: is_column(item[0])
f_not_column = lambda item: not is_column(item[0])
- extra = dict(filter(f_not_column, kw.items()))
- kw = dict(filter(f_is_column, kw.items()))
+ items = kw.items()
+ extra = dict(filter(f_not_column, items))
+ kw = dict(filter(f_is_column, items))
# _SO_creating is special, see _SO_setValue
if self._SO_creating or self._lazyUpdate:
@@ -898,7 +899,15 @@
kw[name] = fromPy(value, self._SO_validatorState)
setattr(self, instanceName(name), value)
for name, value in extra.items():
- setattr(self, name, value)
+ try:
+ getattr(self.__class__, name)
+ except AttributeError:
+ raise TypeError, "%s.set() got an unexpected keyword argument %s" % (self.__class__.__name__, name)
+ try:
+ setattr(self, name, value)
+ except AttributeError, e:
+ raise AttributeError, '%s (with attribute %r)' % (e, name)
+
self._SO_createValues.update(kw)
self.dirty = True
return
@@ -925,7 +934,14 @@
if self._cacheValues:
setattr(self, instanceName(name), value)
for name, value in extra.items():
- setattr(self, name, value)
+ try:
+ getattr(self.__class__, name)
+ except AttributeError:
+ raise TypeError, "%s.set() got an unexpected keyword argument %s" % (self.__class__.__name__, name)
+ try:
+ setattr(self, name, value)
+ except AttributeError, e:
+ raise AttributeError, '%s (with attribute %r)' % (e, name)
if toUpdate:
args = [(self._SO_columnDict[name].dbName, value)
@@ -1002,8 +1018,13 @@
#DSM: If we are the children of an inheritable class,
#DSM: we must first create our parent
if self._parentClass:
- #kw['childName'] = inst._className
- self._parent = self._parentClass(kw=kw)
+ parentClass = self._parentClass
+ parent_kw = dict(
+ [(name, value) for (name, value) in kw.items()
+ if hasattr(parentClass, name)
+ ]
+ )
+ self._parent = parentClass(kw=parent_kw)
self._parent.childName = self._className
id = self._parent.id
@@ -1033,35 +1054,8 @@
# that keyword:
kw[column.name] = default
- # We sort out what columns go straight into the database,
- # and which ones need setattr() directly here:
- forDB = {}
- others = {}
- for name, value in kw.items():
- if name in self._SO_plainSetters:
- forDB[name] = value
- #DSM: If this is a call from the child,
- #DSM: we must remove the parameter for the database
- if fromChild: del kw[name]
- elif not fromChild:
- #DSM: Only use other items if this isn't a call from the child
- others[name] = value
+ self.set(**kw)
- # We take all the straight-to-DB values and use set() to
- # set them:
- self.set(**forDB)
-
- # The rest go through setattr():
- for name, value in others.items():
- try:
- getattr(self.__class__, name)
- except AttributeError:
- raise TypeError, "%s() got an unexpected keyword argument %s" % (self.__class__.__name__, name)
- try:
- setattr(self, name, value)
- except AttributeError, e:
- raise AttributeError, '%s (with attribute %r)' % (e, name)
-
# Then we finalize the process:
self._SO_finishCreate(id)
|