Skip to content

Commit aae71ad

Browse files
author
Chris Hand
committed
Allow choice fields with human readable key tuples
- Allow for a list of tuples or lists which contain both the allowed value and the human readable key.
1 parent e0a7781 commit aae71ad

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

eve_mongoengine/schema.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ def process_field(cls, field, lowercase):
138138
if field.unique:
139139
fdict['unique'] = True
140140
if field.choices:
141-
fdict['allowed'] = field.choices
141+
allowed = []
142+
for choice in field.choices:
143+
if isinstance(choice, (list, tuple)):
144+
allowed.append(choice[0])
145+
else:
146+
allowed.append(choice)
147+
fdict['allowed'] = tuple(allowed)
142148
if getattr(field, 'max_length', None) is not None:
143149
fdict['maxlength'] = field.max_length
144150
if getattr(field, 'min_length', None) is not None:

tests/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ class LimitedDoc(Document):
7676
d = StringField(max_length=10)
7777
e = StringField(min_length=10)
7878
f = IntField(min_value=5, max_value=10)
79+
g = StringField(choices=[
80+
['val1', 'test value 1'],
81+
['val2', 'test value 2'],
82+
['val3', 'test value 3']])
7983

8084
class WrongDoc(Document):
8185
updated = IntField() # this is bad name

tests/test_post.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ def test_post_invalid_schema_type(self):
4949
def test_post_invalid_schema_limits(self):
5050
# break min_length
5151
response = self.client.post('/limiteddoc/',
52-
data='{"a": "hi", "b": "ho", "c": "x", "d": "<10 chars", "e": "<10 chars"}',
52+
data='{"a": "hi", "b": "ho", "c": "x", "d": "<10 chars", "e": "<10 chars", "g": "val1"}',
5353
content_type='application/json')
5454
self.assertEqual(response.status_code, POST_VALIDATION_ERROR_CODE)
5555
json_data = response.get_json()
5656
self.assertDictEqual(json_data[config.ISSUES], {'e': "min length is 10"})
5757
# break max_length
5858
response = self.client.post('/limiteddoc/',
59-
data='{"a": "hi", "b": "ho", "c": "x", "d": "string > 10 chars", "e": "some very long text"}',
59+
data='{"a": "hi", "b": "ho", "c": "x", "d": "string > 10 chars", "e": "some very long text", "g": "val2"}',
6060
content_type='application/json')
6161
self.assertEqual(response.status_code, POST_VALIDATION_ERROR_CODE)
6262
json_data = response.get_json()

0 commit comments

Comments
 (0)