Skip to content

Commit 1a92c49

Browse files
committed
#7 feat: add options output type drf
1 parent a1b40aa commit 1a92c49

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,11 @@ For this to work perfectly, you must follow the following rules:
218218
DRF_NESTED_MULTIPART_PARSER = {
219219
"separator": "mixed-dot",
220220
"raise_duplicate": True,
221-
"assign_duplicate": False
221+
"assign_duplicate": False,
222222

223+
# output of parser is converted to querydict
224+
# if is set to False, dict python is returned
225+
"querydict": True,
223226
}
224227
```
225228

nested_multipart_parser/drf.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
from django.http import QueryDict
55
from django.conf import settings
66

7+
DRF_OPTIONS = {
8+
"querydict": True
9+
}
710

811
class NestedParser(NestPars):
912

1013
def __init__(self, data):
11-
super().__init__(data, getattr(settings, "DRF_NESTED_MULTIPART_PARSER", {}))
14+
# merge django settings to default DRF_OPTIONS ( special parser options in on parser)
15+
options = {
16+
**DRF_OPTIONS,
17+
**getattr(settings, "DRF_NESTED_MULTIPART_PARSER", {})
18+
}
19+
super().__init__(data, options)
1220

1321
def convert_value(self, data, key):
1422
# all value in querydict as set in list
@@ -19,12 +27,17 @@ def convert_value(self, data, key):
1927

2028
@property
2129
def validate_data(self):
30+
data = super().validate_data
31+
32+
# return dict ( not conver to querydict)
33+
if not self._options["querydict"]:
34+
return data
35+
2236
dtc = QueryDict(mutable=True)
23-
dtc.update(super().validate_data)
37+
dtc.update(data)
2438
dtc.mutable = False
2539
return dtc
2640

27-
2841
class DrfNestedParser(MultiPartParser):
2942

3043
def parse(self, stream, media_type=None, parser_context=None):

nested_multipart_parser/parser.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import re
2-
2+
DEFAULT_OPTIONS = {
3+
"separator": "mixed-dot",
4+
"raise_duplicate": True,
5+
"assign_duplicate": False
6+
}
37

48
class NestedParser:
59
_valid = None
@@ -10,12 +14,6 @@ def __init__(self, data, options={}):
1014
self._merge_options(options)
1115

1216
def _merge_options(self, options):
13-
DEFAULT_OPTIONS = {
14-
"separator": "mixed-dot",
15-
"raise_duplicate": True,
16-
"assign_duplicate": False
17-
}
18-
1917
options = {**DEFAULT_OPTIONS, **options}
2018
self._options = options
2119

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import subprocess
77

8-
version = "1.4.0"
8+
version = "1.4.1"
99

1010
if sys.argv[-1] == 'publish':
1111
if os.system("pip freeze | grep twine"):

tests/test_drf.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def toQueryDict(data):
2020

2121

2222
class TestDrfParser(unittest.TestCase):
23+
def setUp(self):
24+
# reset settings
25+
setattr(settings, "DRF_NESTED_MULTIPART_PARSER", {})
2326

2427
def test_querydict_mutable(self):
2528
parser = NestedParser(
@@ -212,6 +215,29 @@ def test_views_options_mixed_valid(self):
212215
}
213216

214217
self.assertEqual(results.data, toQueryDict(expected))
218+
219+
def test_output_querydict(self):
220+
setattr(settings, 'DRF_NESTED_MULTIPART_PARSER',
221+
{"separator": "mixed", "querydict": False})
222+
data = {
223+
"dtc.key": 'value',
224+
"dtc.hh.oo": "sub",
225+
"dtc.hh.aa": "sub2"
226+
}
227+
results = self.parser_boundary(data)
228+
229+
expected = {
230+
"dtc": {
231+
"key": "value",
232+
"hh": {
233+
"aa": "sub2",
234+
"oo": "sub"
235+
}
236+
}
237+
}
238+
239+
self.assertDictEqual(results.data, expected)
240+
215241

216242
def test_nested_files(self):
217243
file = SimpleUploadedFile("file.png", b"file_content", content_type="image/png")

0 commit comments

Comments
 (0)