diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-02-04 17:05:09 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-02-04 17:05:09 +0000 |
commit | fe49ba2f8c962cf56a58b0e4e4a8547e36c11e4f (patch) | |
tree | 775c090454abea2698be4b9491506f405f44d3ce /lib/sqlalchemy/attributes.py | |
parent | 5fcc683e16ad3a27634065e269fcee2620a8ca32 (diff) | |
download | sqlalchemy-fe49ba2f8c962cf56a58b0e4e4a8547e36c11e4f.tar.gz |
one-to-one support:
rolled the BackrefExtensions into a single GenericBackrefExtension to handle
all combinations of list/nonlist properties (such as one-to-one)
tweak to properties.py which may receive "None" as "added_items()", in the case of a scalar property
instead of a list
PropHistory masquerades as a List on the setattr/append delattr/remove side to make one-to-one's automatically
work
Diffstat (limited to 'lib/sqlalchemy/attributes.py')
-rw-r--r-- | lib/sqlalchemy/attributes.py | 42 |
1 files changed, 17 insertions, 25 deletions
diff --git a/lib/sqlalchemy/attributes.py b/lib/sqlalchemy/attributes.py index 87b09507f..f6abe6b31 100644 --- a/lib/sqlalchemy/attributes.py +++ b/lib/sqlalchemy/attributes.py @@ -24,7 +24,7 @@ AttributeManager can also assign a "callable" history container to an object's a which is invoked when first accessed, to provide the object's "committed" value. The package includes functions for managing "bi-directional" object relationships as well -via the ListBackrefExtension, OTMBackrefExtension, and MTOBackrefExtension objects. +via the GenericBackrefExtension object. """ import sqlalchemy.util as util @@ -80,15 +80,18 @@ class PropHistory(object): orig = self.obj.__dict__.get(self.key, None) if orig is value: return - self.orig = orig + if self.orig is PropHistory.NONE: + self.orig = orig self.obj.__dict__[self.key] = value - if self.extension is not None and self.orig is not value: - self.extension.set(self.obj, value, self.orig) + if self.extension is not None: + self.extension.set(self.obj, value, orig) def delattr(self): - self.orig = self.obj.__dict__.get(self.key, None) + orig = self.obj.__dict__.get(self.key, None) + if self.orig is PropHistory.NONE: + self.orig = orig self.obj.__dict__[self.key] = None if self.extension is not None: - self.extension.set(self.obj, None, self.orig) + self.extension.set(self.obj, None, orig) def append(self, obj): self.setattr(obj) def remove(self, obj): @@ -223,25 +226,7 @@ class AttributeExtension(object): def set(self, obj, child, oldchild): pass -class ListBackrefExtension(AttributeExtension): - def __init__(self, key): - self.key = key - def append(self, obj, child): - getattr(child, self.key).append(obj) - def delete(self, obj, child): - getattr(child, self.key).remove(obj) -class OTMBackrefExtension(AttributeExtension): - def __init__(self, key): - self.key = key - def append(self, obj, child): - prop = child.__class__._attribute_manager.get_history(child, self.key) - prop.setattr(obj) -# prop.setattr(obj) - def delete(self, obj, child): - prop = child.__class__._attribute_manager.get_history(child, self.key) - prop.delattr() - -class MTOBackrefExtension(AttributeExtension): +class GenericBackrefExtension(AttributeExtension): def __init__(self, key): self.key = key def set(self, obj, child, oldchild): @@ -251,6 +236,13 @@ class MTOBackrefExtension(AttributeExtension): if child is not None: prop = child.__class__._attribute_manager.get_history(child, self.key) prop.append(obj) + def append(self, obj, child): + prop = child.__class__._attribute_manager.get_history(child, self.key) + prop.append(obj) + def delete(self, obj, child): + prop = child.__class__._attribute_manager.get_history(child, self.key) + prop.remove(obj) + class AttributeManager(object): """maintains a set of per-attribute history container objects for a set of objects.""" |