From: Mike Bayer Date: Wed, 22 Aug 2007 18:21:25 +0000 (+0000) Subject: - a "collection-holding" InstrumentedAttribute is now identified X-Git-Tag: rel_0_4beta4~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d6fcdb5da02206b50cf8296496aff4c7d903e9b9;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - a "collection-holding" InstrumentedAttribute is now identified by the presence of a "get_collection" method. - added "get_collection" to DynamicCollectionAttribute so its treated as a collection. --- diff --git a/CHANGES b/CHANGES index 732499d909..1ced053e7d 100644 --- a/CHANGES +++ b/CHANGES @@ -54,6 +54,8 @@ CHANGES - Connection.begin() no longer accepts nested=True, this logic is now all in begin_nested(). + +- fixes to new "dynamic" relation loader involving cascades 0.4.0beta3 ---------- diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py index 23af66242f..74e9eb333f 100644 --- a/lib/sqlalchemy/orm/attributes.py +++ b/lib/sqlalchemy/orm/attributes.py @@ -417,7 +417,7 @@ class InstrumentedCollectionAttribute(InstrumentedAttribute): obj._state['modified'] = True - collection = self._get_collection(obj) + collection = self.get_collection(obj) collection.clear_with_event() del obj.__dict__[self.key] @@ -431,13 +431,13 @@ class InstrumentedCollectionAttribute(InstrumentedAttribute): def append(self, obj, value, initiator): if initiator is self: return - collection = self._get_collection(obj) + collection = self.get_collection(obj) collection.append_with_event(value, initiator) def remove(self, obj, value, initiator): if initiator is self: return - collection = self._get_collection(obj) + collection = self.get_collection(obj) collection.remove_with_event(value, initiator) def set(self, obj, value, initiator): @@ -459,7 +459,7 @@ class InstrumentedCollectionAttribute(InstrumentedAttribute): trig() old = self.get(obj) - old_collection = self._get_collection(obj, old) + old_collection = self.get_collection(obj, old) new_collection, user_data = self._build_collection(obj) self._load_collection(obj, value or [], emit_events=True, @@ -497,7 +497,7 @@ class InstrumentedCollectionAttribute(InstrumentedAttribute): return collection, user_data def _load_collection(self, obj, values, emit_events=True, collection=None): - collection = collection or self._get_collection(obj) + collection = collection or self.get_collection(obj) if values is None: return elif emit_events: @@ -507,7 +507,7 @@ class InstrumentedCollectionAttribute(InstrumentedAttribute): for item in values: collection.append_without_event(item) - def _get_collection(self, obj, user_data=None): + def get_collection(self, obj, user_data=None): if user_data is None: user_data = self.get(obj) try: @@ -557,10 +557,10 @@ class CommittedState(object): def rollback(self, manager, obj): for attr in manager.managed_attributes(obj.__class__): if attr.key in self.data: - if not isinstance(attr, InstrumentedCollectionAttribute): + if not hasattr(attr, 'get_collection'): obj.__dict__[attr.key] = self.data[attr.key] else: - collection = attr._get_collection(obj) + collection = attr.get_collection(obj) collection.clear_without_event() for item in self.data[attr.key]: collection.append_without_event(item) @@ -589,14 +589,14 @@ class AttributeHistory(object): else: original = None - if isinstance(attr, InstrumentedCollectionAttribute): + if hasattr(attr, 'get_collection'): self._current = current s = util.Set(original or []) self._added_items = [] self._unchanged_items = [] self._deleted_items = [] if current: - collection = attr._get_collection(obj, current) + collection = attr.get_collection(obj, current) for a in collection: if a in s: self._unchanged_items.append(a) @@ -730,8 +730,8 @@ class AttributeManager(object): x = attr.get(obj, passive=passive) if x is PASSIVE_NORESULT: return [] - elif isinstance(attr, InstrumentedCollectionAttribute): - return list(attr._get_collection(obj, x)) + elif hasattr(attr, 'get_collection'): + return list(attr.get_collection(obj, x)) elif isinstance(x, list): return x else: @@ -853,7 +853,6 @@ class AttributeManager(object): def init_collection(self, instance, key): """Initialize a collection attribute and return the collection adapter.""" - attr = self.get_attribute(instance, key) user_data = attr.initialize(instance) - return attr._get_collection(instance, user_data) + return attr.get_collection(instance, user_data) diff --git a/lib/sqlalchemy/orm/dynamic.py b/lib/sqlalchemy/orm/dynamic.py index fe3628df2d..1d4b5f6c93 100644 --- a/lib/sqlalchemy/orm/dynamic.py +++ b/lib/sqlalchemy/orm/dynamic.py @@ -21,7 +21,10 @@ class DynamicCollectionAttribute(attributes.InstrumentedAttribute): # we have our own AttributeHistory therefore dont need CommittedState # instead, we reset the history stored on the attribute obj.__dict__[self.key] = CollectionHistory(self, obj) - + + def get_collection(self, obj, user_data=None): + return self.get_history(obj)._added_items + def set(self, obj, value, initiator): if initiator is self: return