Skip to content

Commit

Permalink
more specific field types - text
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Jan 3, 2017
1 parent 7253f33 commit 4735982
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
6 changes: 3 additions & 3 deletions patterns/base/serializers.py
Expand Up @@ -165,10 +165,10 @@ def get_fields(self):
fields.update(self.build_natural_key_fields())
return fields

def get_wq_field_info(self, name, field):
def get_wq_field_info(self, name, field, model=None):
if isinstance(field, NaturalKeySerializer):
children = [
self.get_wq_field_info(n, f)
self.get_wq_field_info(n, f, model=field.Meta.model)
for n, f in field.get_fields().items()
]
if len(children) == 1:
Expand All @@ -189,7 +189,7 @@ def get_wq_field_info(self, name, field):
return info
else:
return super(NaturalKeyModelSerializer, self).get_wq_field_info(
name, field
name, field, model
)


Expand Down
24 changes: 17 additions & 7 deletions rest/serializers.py
Expand Up @@ -117,7 +117,7 @@ def get_wq_config(self):
config['label_template'] = label_template
return config

def get_wq_field_info(self, name, field):
def get_wq_field_info(self, name, field, model=None):
info = {
'name': name,
'label': field.label or name.replace('_', ' ').title(),
Expand Down Expand Up @@ -158,17 +158,27 @@ def get_wq_field_info(self, name, field):

if 'type' not in info:
info['type'] = 'string'

for field_type, xlsform_type in self.xlsform_types.items():
if isinstance(field, field_type):
info['type'] = xlsform_type
break

if info['type'] == 'geoshape':
source = self.Meta.model._meta.get_field(name)
geom_type = getattr(source, 'geom_type', None)
if geom_type == 'POINT':
info['type'] = 'geopoint'
elif geom_type == 'LINESTRING':
info['type'] = 'geotrace'
model = model or self.Meta.model
source = model._meta.get_field(name)
if info['type'] == 'geoshape':
geom_type = getattr(source, 'geom_type', None)
if geom_type == 'POINT':
info['type'] = 'geopoint'
elif geom_type == 'LINESTRING':
info['type'] = 'geotrace'

if info['type'] == 'string':
model = model or self.Meta.model
source = model._meta.get_field(name)
if source.get_internal_type() == "TextField":
info['type'] = "text"

return info

Expand Down
2 changes: 1 addition & 1 deletion tests/test_annotate.py
Expand Up @@ -75,7 +75,7 @@ def test_annotate_config(self):
}, {
'name': 'value',
'label': 'Value',
'type': 'string',
'type': 'text',
}],
'initial': {
'type_field': 'type',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mark.py
Expand Up @@ -53,7 +53,7 @@ def test_mark_config(self):
}, {
'name': 'markdown',
'label': 'Markdown',
'type': 'string',
'type': 'text',
}],
'initial': {'type_field': 'type', 'filter': {}},
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_naturalkey.py
Expand Up @@ -16,7 +16,7 @@ def test_naturalkey_config(self):
{
'name': 'note',
'label': 'Note',
'type': 'string',
'type': 'text',
'bind': {'required': True},
},
{
Expand Down
16 changes: 12 additions & 4 deletions tests/test_rest.py
Expand Up @@ -173,19 +173,27 @@ def test_rest_config_json_label(self):
def test_rest_config_subtype(self):
conf = self.get_config('geometrymodel')
field = self.get_field(conf, 'geometry')
self.assertEqual(field['type'], 'geoshape')
self.assertEqual('geoshape', field['type'])

conf = self.get_config('pointmodel')
field = self.get_field(conf, 'geometry')
self.assertEqual(field['type'], 'geopoint')
self.assertEqual('geopoint', field['type'])

conf = self.get_config('filemodel')
field = self.get_field(conf, 'file')
self.assertEqual(field['type'], 'binary')
self.assertEqual('binary', field['type'])

conf = self.get_config('imagemodel')
field = self.get_field(conf, 'image')
self.assertEqual(field['type'], 'image')
self.assertEqual('image', field['type'])

conf = self.get_config('item')
field = self.get_field(conf, 'name')
self.assertEqual('string', field['type'])

conf = self.get_config('rootmodel')
field = self.get_field(conf, 'description')
self.assertEqual('text', field['type'])

def test_rest_config_field_order(self):
conf = self.get_config('slugrefparent')
Expand Down

0 comments on commit 4735982

Please sign in to comment.