Repository: incubator-ariatosca
Updated Branches:
  refs/heads/Relationship-ordering-significance-support d683352e8 -> f96047dd3 
(forced update)


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/f96047dd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/f96047dd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/f96047dd

Branch: refs/heads/Relationship-ordering-significance-support
Commit: f96047dd3445ee7fa93ce732872a2a23ed97a535
Parents: c9ecc54
Author: mxmrlv <mxm...@gmail.com>
Authored: Tue Dec 27 16:42:57 2016 +0200
Committer: mxmrlv <mxm...@gmail.com>
Committed: Sun Jan 1 16:24:59 2017 +0200

----------------------------------------------------------------------
 aria/storage/base_model.py | 68 +++++++++++++++++++++++++++++++++--------
 aria/storage/structure.py  | 17 +++++++++--
 2 files changed, 70 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/f96047dd/aria/storage/base_model.py
----------------------------------------------------------------------
diff --git a/aria/storage/base_model.py b/aria/storage/base_model.py
index c7eb27c..454bc95 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.

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/f96047dd/aria/storage/structure.py
----------------------------------------------------------------------
diff --git a/aria/storage/structure.py b/aria/storage/structure.py
index d222c94..4b179f2 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
@@ -142,7 +146,14 @@ class ModelMixin(object):
         else:
             # Can't simply call here `self.to_response()` because inheriting
             # class might override it, but we always need the same code here
-            res = dict((f, getattr(self, f)) for f in self.fields())
+            res = dict()
+
+            # Recursive to_dict support
+            for field in self.fields():
+                if isinstance(field, ModelMixin):
+                    res[field] = field.to_dict()
+                else:
+                    res[field] = getattr(self, field)
         return res
 
     @classmethod

Reply via email to