Refactor property observer

This commit is contained in:
Konsta Vesterinen
2014-02-19 16:12:20 +02:00
parent 334823638e
commit ecf4fcdd27

View File

@@ -88,12 +88,20 @@ class AttributeValueGenerator(object):
property_key, property_key,
getdotattr(value, str(path[1:])) getdotattr(value, str(path[1:]))
) )
elif index == len(path) - 1: else:
inversed_path = ~path[0:-1] inversed_path = ~path[0:-1]
entities = getdotattr( if index == len(path) - 1:
target, entities = getdotattr(
str(inversed_path) target,
) str(inversed_path)
)
assigned_value = value
else:
entities = getdotattr(
target,
str(inversed_path[index:])
)
assigned_value = getdotattr(value, str(path[(index + 1):]))
if entities: if entities:
if not isinstance(entities, list): if not isinstance(entities, list):
entities = [entities] entities = [entities]
@@ -103,30 +111,14 @@ class AttributeValueGenerator(object):
setattr( setattr(
e, e,
property_key, property_key,
value assigned_value
) )
else: else:
setattr( setattr(
entity, entity,
property_key, property_key,
value assigned_value
) )
else:
inversed_path = ~path[0:-1]
entities = getdotattr(
target,
str(inversed_path[index:])
)
if entities:
if not isinstance(entities, list):
entities = [entities]
for entity in entities:
setattr(
entity,
property_key,
getdotattr(value, str(path[(index + 1):]))
)
def update_generated_properties(self, session, ctx, instances): def update_generated_properties(self, session, ctx, instances):
for obj in itertools.chain(session.new, session.dirty): for obj in itertools.chain(session.new, session.dirty):
@@ -143,6 +135,12 @@ class AttributeValueGenerator(object):
setattr(obj, attr, func(obj, getdotattr(obj, source))) setattr(obj, attr, func(obj, getdotattr(obj, source)))
class PropertyObserver(object):
def __init__(property, observed_property_path):
pass
generator = AttributeValueGenerator() generator = AttributeValueGenerator()