ariatosca-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mxm...@apache.org
Subject incubator-ariatosca git commit: wip
Date Tue, 27 Dec 2016 14:43:10 GMT
Repository: incubator-ariatosca
Updated Branches:
  refs/heads/Relationship-ordering-significance-support [created] d683352e8


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

Branch: refs/heads/Relationship-ordering-significance-support
Commit: d683352e809f806c632c85caed07e3e9294bc891
Parents: c9ecc54
Author: mxmrlv <mxmrlv@gmail.com>
Authored: Tue Dec 27 16:42:57 2016 +0200
Committer: mxmrlv <mxmrlv@gmail.com>
Committed: Tue Dec 27 16:42:57 2016 +0200

----------------------------------------------------------------------
 aria/storage/base_model.py | 63 ++++++++++++++++++++++++++++++++++-------
 aria/storage/structure.py  | 17 +++++++++--
 2 files changed, 66 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/d683352e/aria/storage/base_model.py
----------------------------------------------------------------------
diff --git a/aria/storage/base_model.py b/aria/storage/base_model.py
index c7eb27c..9a3a5fb 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,10 @@ 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 source_node_fk(cls):
@@ -423,8 +427,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 +442,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,7 +520,10 @@ 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):
@@ -511,11 +531,21 @@ class RelationshipInstanceBase(ModelMixin):
 
     @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
@@ -524,16 +554,26 @@ class RelationshipInstanceBase(ModelMixin):
 
     @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)
+        return cls.foreign_key(RelationshipBase, nullable=True)
 
     @declared_attr
     def relationship(cls):
@@ -544,6 +584,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/d683352e/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


Mime
View raw message