Refactor employees view to use master3

This commit is contained in:
Lance Edgar 2017-12-04 13:48:31 -06:00
parent 7a777964a7
commit 84ebf5d929
4 changed files with 181 additions and 117 deletions

View file

@ -75,6 +75,38 @@ class CustomSchemaNode(SQLAlchemySchemaNode):
"""
return get_association_proxy(self.inspector, field)
def association_proxy_target(self, field):
"""
Returns the property on the main class, which represents the "target"
for the given association proxy field name. Typically this will refer
to the "extension" model class.
"""
proxy = self.association_proxy(field)
if proxy:
proxy_target = self.inspector.get_property(proxy.target_collection)
if isinstance(proxy_target, orm.RelationshipProperty) and not proxy_target.uselist:
return proxy_target
def association_proxy_column(self, field):
"""
Returns the property on the proxy target class, for the column which is
reflected by the proxy.
"""
proxy_target = self.association_proxy_target(field)
if proxy_target:
prop = proxy_target.mapper.get_property(field)
if isinstance(prop, orm.ColumnProperty) and isinstance(prop.columns[0], sa.Column):
return prop
def supported_association_proxy(self, field):
"""
Returns boolean indicating whether the association proxy corresponding
to the given field name, is "supported" with typical logic.
"""
if not self.association_proxy_column(field):
return False
return True
def add_nodes(self, includes, excludes, overrides):
"""
Add all automatic nodes to the schema.
@ -117,13 +149,9 @@ class CustomSchemaNode(SQLAlchemySchemaNode):
else:
# magic for association proxy fields
proxy = self.association_proxy(name)
if proxy:
proxy_prop = self.inspector.get_property(proxy.target_collection)
if isinstance(proxy_prop, orm.RelationshipProperty):
prop = proxy_prop.mapper.get_property(name)
if isinstance(prop, orm.ColumnProperty) and isinstance(prop.columns[0], sa.Column):
node = self.get_schema_from_column(prop, name_overrides_copy)
column = self.association_proxy_column(name)
if column:
node = self.get_schema_from_column(column, name_overrides_copy)
else:
log.debug(
@ -167,7 +195,8 @@ class CustomSchemaNode(SQLAlchemySchemaNode):
name = node.name
if name not in dict_:
if not self.association_proxy(name):
# we're only processing association proxy fields here
if not self.supported_association_proxy(name):
continue
value = getattr(obj, name)
@ -229,7 +258,7 @@ class CustomSchemaNode(SQLAlchemySchemaNode):
else:
# try to process association proxy field
if self.association_proxy(attr):
if self.supported_association_proxy(attr):
value = dict_[attr]
if value is colander.null:
# `colander.null` is never an appropriate
@ -306,6 +335,10 @@ class Form(object):
if key in self.fields:
self.fields.remove(key)
def remove_fields(self, *args):
for arg in args:
self.remove_field(arg)
def make_schema(self):
if not self.model_class:
# TODO
@ -566,10 +599,11 @@ class Form(object):
return HTML.tag('pre', value)
def obtain_value(self, record, field_name):
try:
return record[field_name]
except TypeError:
return getattr(record, field_name)
if record:
try:
return record[field_name]
except TypeError:
return getattr(record, field_name)
def validate(self, *args, **kwargs):
form = self.make_deform_form()