Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/linestringfield support #1

Merged
merged 2 commits into from
Aug 31, 2018
Merged
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
10 changes: 5 additions & 5 deletions marshmallow_mongoengine/conversion/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def marshmallow_field_cls(self):


class ListBuilder(MetaFieldBuilder):
AVAILABLE_PARAMS = (params.LenghtParam,)
AVAILABLE_PARAMS = (params.LengthParam,)
MARSHMALLOW_FIELD_CLS = ma_fields.List

def _get_marshmallow_field_cls(self):
Expand Down Expand Up @@ -165,7 +165,7 @@ class Builder(MetaFieldBuilder):
register_field(me.fields.DictField, ma_fields.Raw)
register_field(me.fields.DynamicField, ma_fields.Raw)
register_field(me.fields.EmailField, ma_fields.Email,
available_params=(params.LenghtParam,))
available_params=(params.LengthParam,))
register_field(me.fields.FloatField, ma_fields.Float,
available_params=(params.SizeParam,))
register_field(me.fields.GenericEmbeddedDocumentField,
Expand All @@ -187,19 +187,19 @@ class Builder(MetaFieldBuilder):
register_field(me.fields.ObjectIdField, ma_fields.ObjectId)
register_field(me.fields.UUIDField, ma_fields.UUID)
register_field(me.fields.PointField, ma_fields.Point)
register_field(me.fields.LineStringField, ma_fields.LineString)
register_field(me.fields.SequenceField, ma_fields.Integer,
available_params=(params.SizeParam,)) # TODO: handle value_decorator
register_field(me.fields.StringField, ma_fields.String,
available_params=(params.LenghtParam, params.RegexParam))
available_params=(params.LengthParam, params.RegexParam))
register_field(me.fields.URLField, ma_fields.URL,
available_params=(params.LenghtParam,))
available_params=(params.LengthParam,))
register_field_builder(me.fields.EmbeddedDocumentField, EmbeddedDocumentBuilder)
register_field_builder(me.fields.ListField, ListBuilder)
register_field_builder(me.fields.MapField, MapBuilder)
register_field_builder(me.fields.SortedListField, ListBuilder)
# TODO: finish fields...
# me.fields.GeoPointField: ma_fields.GeoPoint,
# me.fields.LineStringField: ma_fields.LineString,
# me.fields.PolygonField: ma_fields.Polygon,
# me.fields.MultiPointField: ma_fields.MultiPoint,
# me.fields.MultiLineStringField: ma_fields.MultiLineString,
Expand Down
4 changes: 2 additions & 2 deletions marshmallow_mongoengine/conversion/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def __init__(self, field_me):
self.field_kwargs['required'] = required


class LenghtParam(MetaParam):
class LengthParam(MetaParam):
def __init__(self, field_me):
super(LenghtParam, self).__init__()
super(LengthParam, self).__init__()
# Add a length validator for max_length/min_length
maxmin_args = {}
if hasattr(field_me, 'max_length'):
Expand Down
27 changes: 27 additions & 0 deletions marshmallow_mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,33 @@ def _serialize(self, value, attr, obj):
)


class LineString(fields.Field):

"""
Marshmallow custom field to map with :class Mongoengine.LineStringField:
"""

def _deserialize(self, value, attr, data):
try:
coordinates = []

for x, y in value['coordinates']:
coordinates.append([float(x), float(y)])
return dict(
type='LineString',
coordinates=coordinates
)
except Exception:
raise ValidationError('invalid value data `%s`' % value)

def _serialize(self, value, attr, obj):
if value is None:
return missing
return dict(
coordinates=value['coordinates']
)


class Reference(fields.Field):

"""
Expand Down
23 changes: 23 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,26 @@ class Meta:
data = {'point': { 'x': '10', 'y': '20foo' }}
load = DocSchema().load(data)
assert 'point' in load.errors

def test_LineStringField(self):
class Doc(me.Document):
line = me.LineStringField()
class DocSchema(ModelSchema):
class Meta:
model = Doc
doc = Doc(line={'type': 'LineString', 'coordinates': [[10, 20], [30, 40]]})
dump = DocSchema().dump(doc)
assert not dump.errors
assert dump.data['line']['coordinates'] == [[10, 20], [30, 40]]
load = DocSchema().load(dump.data)
assert not load.errors
assert load.data.line == {'type': 'LineString', 'coordinates': [[10, 20], [30, 40]]}
# Deserialize LineString with coordinates passed as strings
data = {'line': {'coordinates': [['10', '20'], ['30', '40']]}}
load = DocSchema().load(data)
assert not load.errors
assert load.data.line == {'type': 'LineString', 'coordinates': [[10, 20], [30, 40]]}
# Try to load invalid coordinates
data = {'line': {'coordinates': [['10', '20foo']]}}
load = DocSchema().load(data)
assert 'line' in load.errors