Skip to content

Commit

Permalink
Added support for DictField / ListField (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfarrell authored and c17r committed Jan 22, 2020
1 parent 6c3a62f commit 10168a3
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 3 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

**Features and Improvements**

* Added support for DictField - peterfarrell
* Added support for ListField - peterfarrell

## 0.6.0 (2020-01-03)

**Features and Improvements**
Expand Down
68 changes: 68 additions & 0 deletions service_objects/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,71 @@ def clean(self, values):
self.check_type(value)
self.check_unsaved(value)
return values


class DictField(forms.Field):
"""
A field for :class:`Service` that accepts a dictionary:
class PDFGenerate(Service):
context = DictField()
process(self):
context = self.cleaned_data['context']
PDFGenerate.execute({
'context': {'a': 1, 'b': 2}
})
"""
error_required = _("Input is required. Expected dict but got %(value)r.")
error_type = _("Input needs to be of type dict.")

def clean(self, value):
if not value and value is not False:
if self.required:
raise ValidationError(self.error_required % {
'value': value
})
else:
return {}

if not isinstance(value, dict):
raise ValidationError(self.error_type)

return value


class ListField(forms.Field):
"""
A field for :class:`Service` that accepts a list:
class EmailWelcomeMessage(Service):
emails = ListField()
process(self):
emails = self.cleaned_data['emails']
EmailWelcomeMessage.execute({
'emails': ['blue@test.com', 'red@test.com']
})
"""
error_required = _("Input is required. Expected list but got %(value)r.")
error_type = _("Input needs to be of type list.")

def clean(self, value):
if not value and value is not False:
if self.required:
raise ValidationError(self.error_required % {
'value': value
})
else:
return {}

if not isinstance(value, list):
raise ValidationError(self.error_type)

return value
59 changes: 56 additions & 3 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from django.core.exceptions import ValidationError

from service_objects.fields import MultipleFormField, ModelField, MultipleModelField
from service_objects.fields import MultipleFormField, ModelField, MultipleModelField, \
DictField, ListField
from tests.forms import FooForm
from tests.models import FooModel, BarModel, NonModel

Expand Down Expand Up @@ -181,15 +182,67 @@ def test_multiple_valid(self):

self.assertEqual(objects, cleaned_data)

def test_is_requred(self):
def test_is_required(self):
f = MultipleModelField(FooModel)
with self.assertRaises(ValidationError) as cm:
f.clean(None)

self.assertEqual(
'Input is required expected list of models but got None.', cm.exception.message)

def test_is_not_requred(self):
def test_is_not_required(self):
f = MultipleModelField(FooModel, required=False)
# should not raise any exception
f.clean(None)


class DictFieldTest(TestCase):
def test_is_required(self):
dict_field = DictField(required=True)

with self.assertRaises(ValidationError) as cm:
dict_field.clean(None)

self.assertEqual(
'Input is required. Expected dict but got None.', cm.exception.message)

def test_is_not_required(self):
dict_field = DictField(required=False)

# should not raise any exception
dict_field.clean(None)

def test_invalid_type(self):
dict_field = DictField(required=True)

with self.assertRaises(ValidationError) as cm:
dict_field.clean('string test')

self.assertEqual(
'Input needs to be of type dict.', cm.exception.message)


class ListFieldTest(TestCase):
def test_is_required(self):
list_field = ListField(required=True)

with self.assertRaises(ValidationError) as cm:
list_field.clean(None)

self.assertEqual(
'Input is required. Expected list but got None.', cm.exception.message)

def test_is_not_required(self):
list_field = ListField(required=False)

# should not raise any exception
list_field.clean(None)

def test_invalid_type(self):
list_field = ListField(required=True)

with self.assertRaises(ValidationError) as cm:
list_field.clean('string test')

self.assertEqual(
'Input needs to be of type list.', cm.exception.message)

0 comments on commit 10168a3

Please sign in to comment.