Skip to content

Commit

Permalink
AnyField introduced 🎅
Browse files Browse the repository at this point in the history
  • Loading branch information
Sibyx committed Dec 20, 2019
1 parent fcbee11 commit 1915fe0
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.3 : 19.12.2019

- **Feature**: Introduced generic `AnyField`

## 0.5.2 : 19.12.2019

- **Fix**: Skip processing of the `FormField` if value is not required and empty
Expand Down
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ python setup.py install
"title": "Unknown Pleasures",
"type": "vinyl",
"artist": {
"name": "Joy division",
"name": "Joy Division",
"genres": [
"rock",
"punk"
Expand All @@ -67,14 +67,31 @@ python setup.py install
},
{
"title": "Day of the Lords",
"duration": "4:48"
"duration": "4:48",
"metadata": {
"_section": {
"type": "ID3v2",
"offset": 0,
"byteLength": 2048
},
"header": {
"majorVersion": 3,
"minorRevision": 0,
"flagsOctet": 0,
"unsynchronisationFlag": false,
"extendedHeaderFlag": false,
"experimentalIndicatorFlag": false,
"size": 2038
}
}
}
],
"metadata": {
"created_at": "2019-10-21T18:57:03+00:00",
"updated_at": "2019-10-21T18:57:03+00:00"
"created_at": "2019-10-21T18:57:03+0100",
"updated_at": "2019-10-21T18:57:03+0100"
}
}

```

**DjangoRequestFormatter equivalent + validation**
Expand All @@ -85,7 +102,7 @@ from enum import Enum
from django.core.exceptions import ValidationError
from django.forms import fields

from django_request_formatter.fields import FieldList, FormField, FormFieldList, DictionaryField, EnumField
from django_request_formatter.fields import FieldList, FormField, FormFieldList, DictionaryField, EnumField, AnyField
from django_request_formatter.forms import Form


Expand All @@ -103,6 +120,7 @@ class ArtistForm(Form):
class SongForm(Form):
title = fields.CharField(required=True, max_length=100)
duration = fields.DurationField(required=False)
metadata = AnyField(required=False)


class AlbumForm(Form):
Expand All @@ -124,6 +142,7 @@ class AlbumForm(Form):
return self.cleaned_data



"""
Django view example
"""
Expand Down
2 changes: 1 addition & 1 deletion django_request_formatter/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.5.2'
__version__ = '0.5.3'
5 changes: 5 additions & 0 deletions django_request_formatter/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,8 @@ def to_python(self, value) -> dict:
raise ValidationError(errors)

return result


class AnyField(Field):
def to_python(self, value) -> typing.Union[typing.Dict, typing.List]:
return value
18 changes: 17 additions & 1 deletion tests/data/invalid.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@
},
{
"title": "Day of the Lords",
"duration": "4:48"
"duration": "4:48",
"metadata": {
"_section": {
"type": "ID3v2",
"offset": 0,
"byteLength": 2048
},
"header": {
"majorVersion": 3,
"minorRevision": 0,
"flagsOctet": 0,
"unsynchronisationFlag": false,
"extendedHeaderFlag": false,
"experimentalIndicatorFlag": false,
"size": 2038
}
}
}
],
"metadata": {
Expand Down
18 changes: 17 additions & 1 deletion tests/data/valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@
},
{
"title": "Day of the Lords",
"duration": "4:48"
"duration": "4:48",
"metadata": {
"_section": {
"type": "ID3v2",
"offset": 0,
"byteLength": 2048
},
"header": {
"majorVersion": 3,
"minorRevision": 0,
"flagsOctet": 0,
"unsynchronisationFlag": false,
"extendedHeaderFlag": false,
"experimentalIndicatorFlag": false,
"size": 2038
}
}
}
],
"metadata": {
Expand Down
21 changes: 19 additions & 2 deletions tests/test_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,28 @@ def test_valid(rf: RequestFactory):
'songs': [
{
'title': "Disorder",
'duration': datetime.timedelta(seconds=209)
'duration': datetime.timedelta(seconds=209),
'metadata': None # FIXME: this not suppose to be he
},
{
'title': "Day of the Lords",
'duration': datetime.timedelta(seconds=288)
'duration': datetime.timedelta(seconds=288),
'metadata': {
'_section': {
"type": "ID3v2",
"offset": 0,
"byteLength": 2048
},
'header': {
"majorVersion": 3,
"minorRevision": 0,
"flagsOctet": 0,
"unsynchronisationFlag": False,
"extendedHeaderFlag": False,
"experimentalIndicatorFlag": False,
"size": 2038
}
}
}
],
'metadata': {
Expand Down
3 changes: 2 additions & 1 deletion tests/testapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.core.exceptions import ValidationError
from django.forms import fields

from django_request_formatter.fields import FieldList, FormField, FormFieldList, DictionaryField, EnumField
from django_request_formatter.fields import FieldList, FormField, FormFieldList, DictionaryField, EnumField, AnyField
from django_request_formatter.forms import Form


Expand All @@ -21,6 +21,7 @@ class ArtistForm(Form):
class SongForm(Form):
title = fields.CharField(required=True, max_length=100)
duration = fields.DurationField(required=False)
metadata = AnyField(required=False)


class AlbumForm(Form):
Expand Down

0 comments on commit 1915fe0

Please sign in to comment.