Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 49 additions & 34 deletions colanderalchemy/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
String,
Numeric,
Time)
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.schema import (FetchedValue, ColumnDefault, Column)
from sqlalchemy.orm import (ColumnProperty, RelationshipProperty)

Expand Down Expand Up @@ -298,42 +299,16 @@ def get_schema_from_column(self, prop, overrides):
log.debug('Column %s: type overridden via TypeDecorator: %s.',
name, type_)

elif isinstance(column_type, Boolean):
type_ = colander.Boolean()

elif isinstance(column_type, Date):
type_ = colander.Date()

elif isinstance(column_type, DateTime):
type_ = colander.DateTime(default_tzinfo=None)

elif isinstance(column_type, Enum):
type_ = colander.String()
kwargs["validator"] = colander.OneOf(column.type.enums)

elif isinstance(column_type, Float):
type_ = colander.Float()

elif isinstance(column_type, Integer):
type_ = colander.Integer()

elif isinstance(column_type, String):
type_ = colander.String()
kwargs["validator"] = colander.Length(0, column.type.length)

elif isinstance(column_type, Numeric):
type_ = colander.Decimal()

elif isinstance(column_type, Time):
type_ = colander.Time()
elif isinstance(column_type, ARRAY):
name_arr = name + '_array_typ'
kwargs_arr = dict(name=name_arr)
node = colander.SchemaNode(
self.get_type(name_arr, column_type.item_type, kwargs_arr))
children = [node]
type_ = Sequence()

else:
raise NotImplementedError(
'Not able to derive a colander type from sqlalchemy '
'type: %s Please explicitly provide a colander '
'`typ` for the "%s" Column.'
% (repr(column_type), name)
)
type_ = self.get_type(name, column_type, kwargs)

"""
Add default values
Expand Down Expand Up @@ -400,6 +375,46 @@ def get_schema_from_column(self, prop, overrides):

return colander.SchemaNode(type_, *children, **kwargs)

def get_type(self, name, column_type, kwargs):
if isinstance(column_type, Boolean):
type_ = colander.Boolean()

elif isinstance(column_type, Date):
type_ = colander.Date()

elif isinstance(column_type, DateTime):
type_ = colander.DateTime(default_tzinfo=None)

elif isinstance(column_type, Enum):
type_ = colander.String()
kwargs["validator"] = colander.OneOf(column_type.enums)

elif isinstance(column_type, Float):
type_ = colander.Float()

elif isinstance(column_type, Integer):
type_ = colander.Integer()

elif isinstance(column_type, String):
type_ = colander.String()
kwargs["validator"] = colander.Length(0, column_type.length)

elif isinstance(column_type, Numeric):
type_ = colander.Decimal()

elif isinstance(column_type, Time):
type_ = colander.Time()

else:
raise NotImplementedError(
'Not able to derive a colander type from sqlalchemy '
'type: %s Please explicitly provide a colander '
'`typ` for the "%s" Column.'
% (repr(column_type), name)
)

return type_

def check_overrides(self, name, arg, typedecorator_overrides,
declarative_overrides, overrides):
msg = None
Expand Down
2 changes: 2 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Unicode,
event
)
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import (relationship, mapper)

Expand Down Expand Up @@ -64,6 +65,7 @@ class Person(Base):
gender = Column(Enum('M', 'F'), nullable=False)
birthday = Column(Date, nullable=True)
age = Column(Integer, nullable=True)
phone_numbers = Column(ARRAY(Unicode(32)))
addresses = relationship(
'Address',
info={
Expand Down
10 changes: 7 additions & 3 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ class UseListOverrides(Base):
def _prep_schema(self):
overrides = {
'person': {
'includes': ['name', 'surname', 'gender', 'addresses'],
'includes': [
'name', 'surname', 'gender', 'addresses', 'phone_numbers'
],
'overrides': {
'addresses': {
'includes': ['street', 'city'],
Expand Down Expand Up @@ -403,7 +405,8 @@ def test_dictify(self):
address = Address(**address_args)

person_args = dict(name='My Name', surname='My Surname',
gender='M', addresses=[address])
gender='M', addresses=[address],
phone_numbers=['+12345', '+234567'])
person = Person(**person_args)

account_args = dict(email='mailbox@domain.tld',
Expand Down Expand Up @@ -476,7 +479,8 @@ def test_objectify(self):
'city': 'My City',
'street': 'My Street'
}],
'name': 'My Name'
'name': 'My Name',
'phone_numbers': ['+123', '+234']
},
'enabled': True,
'email': 'mailbox@domain.tld',
Expand Down