wip
Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/958626a7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/958626a7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/958626a7 Branch: refs/heads/ARIA-52-Support-order-management-in-relationships Commit: 958626a7d50ea7d352b8c654f9f99a24d3650f44 Parents: 12e175b Author: mxmrlv <[email protected]> Authored: Tue Dec 27 16:42:57 2016 +0200 Committer: mxmrlv <[email protected]> Committed: Thu Jan 5 11:52:24 2017 +0200 ---------------------------------------------------------------------- aria/storage/base_model.py | 70 +++++++++++++++++++++++++++++++++-------- aria/storage/sql_mapi.py | 2 +- aria/storage/structure.py | 10 ++++-- 3 files changed, 66 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/958626a7/aria/storage/base_model.py ---------------------------------------------------------------------- diff --git a/aria/storage/base_model.py b/aria/storage/base_model.py index d1aebf2..c230fe3 100644 --- a/aria/storage/base_model.py +++ b/aria/storage/base_model.py @@ -51,6 +51,7 @@ from sqlalchemy import ( Float, orm, ) +from sqlalchemy.ext.orderinglist import ordering_list from .structure import ModelMixin @@ -415,7 +416,13 @@ class RelationshipBase(ModelMixin): """ __tablename__ = 'relationships' - _private_fields = ['source_node_fk', 'target_node_fk'] + _private_fields = ['source_node_fk', 'target_node_fk', 'position'] + + position = Column(Integer) + + @declared_attr + def deployment_id(self): + return association_proxy('source_node', 'deployment_id') @declared_attr def source_node_fk(cls): @@ -423,8 +430,14 @@ class RelationshipBase(ModelMixin): @declared_attr def source_node(cls): - return cls.one_to_many_relationship('source_node_fk', - backreference='outbound_relationships') + return cls.one_to_many_relationship( + 'source_node_fk', + backreference='outbound_relationships', + backref_kwargs=dict( + order_by=cls.position, + collection_class=ordering_list('position', count_from=0) + ) + ) @declared_attr def source_name(cls): @@ -432,11 +445,18 @@ class RelationshipBase(ModelMixin): @declared_attr def target_node_fk(cls): - return cls.foreign_key(NodeBase) + return cls.foreign_key(NodeBase, nullable=True) @declared_attr def target_node(cls): - return cls.one_to_many_relationship('target_node_fk', backreference='inbound_relationships') + return cls.one_to_many_relationship( + 'target_node_fk', + backreference='inbound_relationships', + backref_kwargs=dict( + order_by=cls.position, + collection_class=ordering_list('position', count_from=0) + ) + ) @declared_attr def target_name(cls): @@ -503,35 +523,58 @@ class RelationshipInstanceBase(ModelMixin): __tablename__ = 'relationship_instances' _private_fields = ['relationship_storage_fk', 'source_node_instance_fk', - 'target_node_instance_fk'] + 'target_node_instance_fk', + 'position'] + + position = Column(Integer) @declared_attr def source_node_instance_fk(cls): - return cls.foreign_key(NodeInstanceBase) + return cls.foreign_key(NodeInstanceBase, nullable=True) @declared_attr def source_node_instance(cls): - return cls.one_to_many_relationship('source_node_instance_fk', - backreference='outbound_relationship_instances') + return cls.one_to_many_relationship( + 'source_node_instance_fk', + backreference='outbound_relationship_instances', + backref_kwargs=dict( + order_by=cls.position, + collection_class=ordering_list('position', count_from=0) + ) + ) @declared_attr def source_node_instance_name(cls): + return association_proxy('source_node_instance', 'node_{0}'.format(cls.name_column_name())) + + @declared_attr + def source_node_name(cls): return association_proxy('source_node_instance', cls.name_column_name()) @declared_attr def target_node_instance_fk(cls): - return cls.foreign_key(NodeInstanceBase) + return cls.foreign_key(NodeInstanceBase, nullable=True) @declared_attr def target_node_instance(cls): - return cls.one_to_many_relationship('target_node_instance_fk', - backreference='inbound_relationship_instances') + return cls.one_to_many_relationship( + 'target_node_instance_fk', + backreference='inbound_relationship_instances', + backref_kwargs=dict( + order_by=cls.position, + collection_class=ordering_list('position', count_from=0) + ) + ) @declared_attr def target_node_instance_name(cls): return association_proxy('target_node_instance', cls.name_column_name()) @declared_attr + def target_node_name(cls): + return association_proxy('target_node_instance', 'node_{0}'.format(cls.name_column_name())) + + @declared_attr def relationship_fk(cls): return cls.foreign_key(RelationshipBase) @@ -544,6 +587,7 @@ class RelationshipInstanceBase(ModelMixin): return association_proxy('relationship', cls.name_column_name()) + class PluginBase(ModelMixin): """ Plugin model representation. @@ -646,7 +690,7 @@ class TaskBase(ModelMixin): INFINITE_RETRIES = -1 - status = Column(Enum(*STATES), name='status', default=PENDING) + status = Column(Enum(*STATES, name='status'), default=PENDING) due_at = Column(DateTime, default=datetime.utcnow) started_at = Column(DateTime, default=None) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/958626a7/aria/storage/sql_mapi.py ---------------------------------------------------------------------- diff --git a/aria/storage/sql_mapi.py b/aria/storage/sql_mapi.py index cde40c2..809f677 100644 --- a/aria/storage/sql_mapi.py +++ b/aria/storage/sql_mapi.py @@ -46,7 +46,7 @@ class SQLAlchemyModelAPI(api.ModelAPI): if not result: raise exceptions.StorageError( - 'Requested {0} with ID `{1}` was not found' + 'Requested `{0}` with ID `{1}` was not found' .format(self.model_cls.__name__, entry_id) ) return result http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/958626a7/aria/storage/structure.py ---------------------------------------------------------------------- diff --git a/aria/storage/structure.py b/aria/storage/structure.py index 431633b..fa592ac 100644 --- a/aria/storage/structure.py +++ b/aria/storage/structure.py @@ -80,7 +80,9 @@ class ModelMixin(object): @classmethod def one_to_many_relationship(cls, foreign_key_column, - backreference=None): + backreference=None, + backref_kwargs=None, + **kwargs): """Return a one-to-many SQL relationship object Meant to be used from inside the *child* object @@ -89,6 +91,7 @@ class ModelMixin(object): :param foreign_key_column: The column of the foreign key (from the child table) :param backreference: The name to give to the reference to the child (on the parent table) """ + backref_kwargs = backref_kwargs or {} parent_table = cls._get_cls_by_tablename( getattr(cls, foreign_key_column).__remote_table_name) primaryjoin_str = '{parent_class_name}.{parent_unique_id} == ' \ @@ -105,7 +108,8 @@ class ModelMixin(object): foreign_keys=[getattr(cls, foreign_key_column)], # The following line make sure that when the *parent* is # deleted, all its connected children are deleted as well - backref=backref(backreference or cls.__tablename__, cascade='all'), + backref=backref(backreference or cls.__tablename__, cascade='all', **backref_kwargs), + **kwargs ) @classmethod @@ -145,6 +149,8 @@ class ModelMixin(object): field_value = list(field_value) elif isinstance(field_value, dict): field_value = dict(field_value) + elif isinstance(field_value, ModelMixin): + field_value = field_value.to_dict() res[field] = field_value return res
