-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for DictField / ListField (#59)
- Loading branch information
1 parent
6c3a62f
commit 10168a3
Showing
3 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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': ['[email protected]', '[email protected]'] | ||
}) | ||
""" | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters