Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add group of fields (fixes #11, fixes #88) #147

Merged
merged 4 commits into from

2 participants

@leplatrem
Owner

No description provided.

@Natim
Owner

Flake8 errors :

daybed/schemas/base.py:221:80: E501 line too long (80 > 79 characters)
daybed/tests/test_schemas_base.py:152:13: E128 continuation line under-indented for visual indent
@leplatrem
Owner

I changed a bit the behaviour. name and hint attributes make no sense, since groups are not form fields.

@Natim
Owner

Ok for me.

@leplatrem leplatrem merged commit 3ae5c3b into dev
@leplatrem leplatrem deleted the add_groups_of_fields branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 3, 2014
  1. @leplatrem
  2. @leplatrem
Commits on Mar 4, 2014
  1. @leplatrem

    Pep8ify

    leplatrem authored
  2. @leplatrem
This page is out of date. Refresh to see the latest.
View
4 daybed/schemas/__init__.py
@@ -72,14 +72,14 @@ class TypeField(object):
node = String
required = True
default_value = null
- hint = ''
+ hint = u''
@classmethod
def definition(cls):
schema = SchemaNode(Mapping())
schema.add(SchemaNode(String(), name='name',
validator=Regex(r'^[a-zA-Z][a-zA-Z0-9_\-]*$')))
- schema.add(SchemaNode(String(), name='label', missing=''))
+ schema.add(SchemaNode(String(), name='label', missing=u''))
schema.add(SchemaNode(String(), name='hint', missing=cls.hint))
schema.add(SchemaNode(Boolean(), name='required',
missing=cls.required))
View
18 daybed/schemas/base.py
@@ -17,7 +17,8 @@
Regex,
Email,
Date,
- DateTime
+ DateTime,
+ drop
)
from . import registry, TypeField
@@ -27,7 +28,7 @@
__all__ = ['IntField', 'StringField', 'RangeField',
'RegexField', 'EmailField', 'URLField',
'EnumField', 'ChoicesField', 'DecimalField',
- 'DateField', 'DateTimeField']
+ 'DateField', 'DateTimeField', 'GroupField']
@registry.add('int')
@@ -209,3 +210,16 @@ class DateTimeField(AutoNowMixin, TypeField):
@deferred
def auto_value(node, kw):
return datetime.datetime.now()
+
+
+@registry.add('group')
+class GroupField(TypeField):
+ @classmethod
+ def definition(cls):
+ schema = super(GroupField, cls).definition()
+ schema.children = [c for c in schema.children
+ if c.name not in ('hint', 'name', 'required')]
+ schema.add(SchemaNode(String(), name='description', missing=drop))
+ schema.add(SchemaNode(Sequence(), TypeField.definition(),
+ name='fields', validator=Length(min=1)))
+ return schema
View
37 daybed/tests/test_schemas_base.py
@@ -145,6 +145,43 @@ def test_url(self):
'http://lolnet/org')
+class GroupFieldTests(unittest.TestCase):
+ def setUp(self):
+ self.schema = schemas.GroupField.definition()
+ self.definition = {
+ 'type': u'group',
+ 'fields': [{'type': u'int',
+ 'name': u'a',
+ 'hint': u'',
+ 'label': u'',
+ 'required': True}]}
+
+ def test_a_group_has_no_name_nor_hint(self):
+ definition = self.definition.copy()
+ field = self.schema.deserialize(definition)
+ definition['label'] = u'' # default empty
+ self.assertDictEqual(definition, field)
+
+ def test_a_group_can_have_label_and_description(self):
+ definition = self.definition.copy()
+ definition['label'] = u'Address'
+ definition['description'] = u'A small text...'
+ field = self.schema.deserialize(definition)
+ self.assertDictEqual(definition, field)
+
+ def test_a_group_must_have_at_least_one_field(self):
+ definition = self.definition.copy()
+ definition['fields'] = []
+ self.assertRaises(colander.Invalid, self.schema.deserialize,
+ definition)
+
+ def test_a_group_must_have_valid_fields(self):
+ definition = self.definition.copy()
+ definition['fields'].append({'type': u'int'})
+ self.assertRaises(colander.Invalid, self.schema.deserialize,
+ definition)
+
+
class DateFieldTests(unittest.TestCase):
def test_date(self):
schema = schemas.DateField.definition()
View
4 daybed/views/fields.py
@@ -1,5 +1,5 @@
from cornice import Service
-from colander import required
+from colander import required, drop
from daybed.schemas import registry
@@ -32,7 +32,7 @@ def list_fields(request):
extras['type'] = 'array'
extras['items'] = dict(type=itemtype)
# Show default only if present
- if parameter.missing != required:
+ if parameter.missing not in (required, drop):
extras['default'] = parameter.missing
field.setdefault('parameters', []).append(extras)
fields.append(field)
Something went wrong with that request. Please try again.