def map_column(self, mode: EditMode, request: Request, node: colander.SchemaNode, model: type, name: str, column: Column, column_type: TypeEngine) -> t.Tuple[colander.SchemaType, dict]:
"""Map non-relationship SQLAlchemy column to Colander SchemaNode.
:return: Tuple(constructed colander.SchemaType, dict of addtional colander.SchemaNode construction arguments)
"""
logger.debug("Mapping field %s, mode %s, node %s, column %s, column type %s", name, mode, node, column, column_type)
# Check for autogenerated columns (updated_at)
if column.onupdate:
if mode in (EditMode.edit, EditMode.add):
return TypeOverridesHandling.drop, {}
# Don't fill default values when added, as they are automatically populated
if column.default:
if mode == EditMode.add:
return TypeOverridesHandling.drop, {}
# Never add primary keys
# NOTE: TODO: We need to preserve ids because of nesting mechanism and groupedit widget wants it id
if column.primary_key:
# TODO: Looks like column.autoincrement is set True by default, so we cannot use it here
if mode in (EditMode.edit, EditMode.add):
return TypeOverridesHandling.drop, {}
if column.foreign_keys:
# Handled by relationship mapper
return TypeOverridesHandling.drop, {}
elif isinstance(column_type, (PostgreSQLUUID, columns.UUID)):
# UUID's cannot be22 edited
if mode in (EditMode.add, EditMode.edit):
return TypeOverridesHandling.drop, {}
# But let's show them
return fields.UUID(), dict(missing=colander.drop, widget=FriendlyUUIDWidget(readonly=True))
elif isinstance(column_type, Text):
return colander.String(), dict(widget=deform.widget.TextAreaWidget())
elif isinstance(column_type, JSONB):
return JSONValue(), dict(widget=JSONWidget())
elif isinstance(column_type, (JSONB, columns.JSONB)):
# Can't edit JSON
if mode in (EditMode.add, EditMode.edit):
return TypeOverridesHandling.drop, {}
return colander.String(), {}
elif isinstance(column_type, LargeBinary):
# Can't edit binary
return TypeOverridesHandling.drop, {}
elif isinstance(column_type, Geometry):
# Can't edit geometry
return TypeOverridesHandling.drop, {}
elif isinstance(column_type, (INET, columns.INET)):
return colander.String(), {}
else:
# Default mapping / unknown, let the parent handle
return TypeOverridesHandling.unknown, {}
评论列表
文章目录