Skip to content

Commit

Permalink
Relationships now allow you to keep parent properties
Browse files Browse the repository at this point in the history
  • Loading branch information
timmartin19 committed Nov 9, 2015
1 parent 335e9a2 commit 0ebcd02
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
15 changes: 11 additions & 4 deletions ripozo/resources/relationships/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ripozo.exceptions import RestException
from ripozo.resources.constructor import ResourceMetaClass
from ripozo.utilities import get_or_pop

import logging
import six
Expand All @@ -26,7 +27,8 @@ class Relationship(object):
_resource_meta_class = ResourceMetaClass

def __init__(self, name, property_map=None, relation=None, embedded=False,
required=False, no_pks=False, query_args=None, templated=False):
required=False, no_pks=False, query_args=None, templated=False,
remove_properties=True):
"""
:param unicode name:
:param dict property_map: A map of the parent's property name
Expand All @@ -53,6 +55,9 @@ def __init__(self, name, property_map=None, relation=None, embedded=False,
:param bool templated: If templated is True, then the resource
does not need to have all pks. However, embedded is negated
if templated = True to prevent infinite loops.
:param bool remove_properties: If True, then the properties in the
child relationship will be removed. Otherwise, the properties
will simply be copied to the relationship
"""
self.query_args = query_args or tuple()
self.property_map = property_map or {}
Expand All @@ -62,6 +67,7 @@ def __init__(self, name, property_map=None, relation=None, embedded=False,
self.name = name
self.no_pks = no_pks
self.templated = templated
self.remove_properties = remove_properties

@property
def relation(self):
Expand Down Expand Up @@ -135,6 +141,7 @@ def remove_child_resource_properties(self, properties):
properties = properties.copy()
for key in six.iterkeys(self.property_map):
properties.pop(key, None)
properties.pop(self.name, None)
return properties

def _map_pks(self, parent_properties):
Expand All @@ -159,10 +166,10 @@ def _map_pks(self, parent_properties):
"""
properties = {}
for parent_prop, prop in six.iteritems(self.property_map):
val = parent_properties.pop(parent_prop, None)
val = get_or_pop(parent_properties, parent_prop, pop=self.remove_properties)
if val is not None:
properties[prop] = val
name_values = parent_properties.pop(self.name, {})
if name_values is not None:
name_values = get_or_pop(parent_properties, self.name, pop=self.remove_properties)
if name_values:
properties.update(name_values)
return properties
31 changes: 31 additions & 0 deletions ripozo_tests/unit/resources/relationships/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,34 @@ class MyResource(ResourceBase):
self.assertTrue(rel._should_return_none(res))
rel.templated = True
self.assertFalse(rel._should_return_none(res))

def test_remove_properties(self):
"""Tests whether properties are appropriately
kept or removed according to the remove_properties
attribute"""
rel = Relationship('related')
x = dict(related=dict(name='name'))
ret = rel._map_pks(x)
self.assertDictEqual(ret, dict(name='name'))
self.assertDictEqual(x, dict())
rel.remove_properties = False
x = dict(related=dict(name='name'))
ret = rel._map_pks(x)
self.assertDictEqual(ret, dict(name='name'))
self.assertDictEqual(x, dict(related=dict(name='name')))

def test_remove_properties_property_map(self):
"""Tests whether removing properties according
to the property_map adheres to the remove_properties
attribute"""

rel = Relationship('related', property_map=dict(name='name'))
x = dict(name='name')
ret = rel._map_pks(x)
self.assertDictEqual(ret, dict(name='name'))
self.assertDictEqual(x, dict())
rel.remove_properties = False
x = dict(name='name')
ret = rel._map_pks(x)
self.assertDictEqual(ret, dict(name='name'))
self.assertDictEqual(x, dict(name='name'))

0 comments on commit 0ebcd02

Please sign in to comment.