diff --git a/README.md b/README.md index 47d59e5..154a960 100644 --- a/README.md +++ b/README.md @@ -218,8 +218,11 @@ For this to work perfectly, you must follow the following rules: DRF_NESTED_MULTIPART_PARSER = { "separator": "mixed-dot", "raise_duplicate": True, - "assign_duplicate": False + "assign_duplicate": False, + # output of parser is converted to querydict + # if is set to False, dict python is returned + "querydict": True, } ``` diff --git a/nested_multipart_parser/drf.py b/nested_multipart_parser/drf.py index 90ef858..3454398 100644 --- a/nested_multipart_parser/drf.py +++ b/nested_multipart_parser/drf.py @@ -4,11 +4,19 @@ from django.http import QueryDict from django.conf import settings +DRF_OPTIONS = { + "querydict": True +} class NestedParser(NestPars): def __init__(self, data): - super().__init__(data, getattr(settings, "DRF_NESTED_MULTIPART_PARSER", {})) + # merge django settings to default DRF_OPTIONS ( special parser options in on parser) + options = { + **DRF_OPTIONS, + **getattr(settings, "DRF_NESTED_MULTIPART_PARSER", {}) + } + super().__init__(data, options) def convert_value(self, data, key): # all value in querydict as set in list @@ -19,12 +27,17 @@ def convert_value(self, data, key): @property def validate_data(self): + data = super().validate_data + + # return dict ( not conver to querydict) + if not self._options["querydict"]: + return data + dtc = QueryDict(mutable=True) - dtc.update(super().validate_data) + dtc.update(data) dtc.mutable = False return dtc - class DrfNestedParser(MultiPartParser): def parse(self, stream, media_type=None, parser_context=None): diff --git a/nested_multipart_parser/parser.py b/nested_multipart_parser/parser.py index f93c5c2..8925e37 100644 --- a/nested_multipart_parser/parser.py +++ b/nested_multipart_parser/parser.py @@ -1,5 +1,9 @@ import re - +DEFAULT_OPTIONS = { + "separator": "mixed-dot", + "raise_duplicate": True, + "assign_duplicate": False +} class NestedParser: _valid = None @@ -10,12 +14,6 @@ def __init__(self, data, options={}): self._merge_options(options) def _merge_options(self, options): - DEFAULT_OPTIONS = { - "separator": "mixed-dot", - "raise_duplicate": True, - "assign_duplicate": False - } - options = {**DEFAULT_OPTIONS, **options} self._options = options diff --git a/setup.py b/setup.py index 8176531..474311b 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ import sys import subprocess -version = "1.4.0" +version = "1.4.1" if sys.argv[-1] == 'publish': if os.system("pip freeze | grep twine"): diff --git a/tests/test_drf.py b/tests/test_drf.py index 5bc6819..5f4f859 100644 --- a/tests/test_drf.py +++ b/tests/test_drf.py @@ -20,6 +20,9 @@ def toQueryDict(data): class TestDrfParser(unittest.TestCase): + def setUp(self): + # reset settings + setattr(settings, "DRF_NESTED_MULTIPART_PARSER", {}) def test_querydict_mutable(self): parser = NestedParser( @@ -212,6 +215,29 @@ def test_views_options_mixed_valid(self): } self.assertEqual(results.data, toQueryDict(expected)) + + def test_output_querydict(self): + setattr(settings, 'DRF_NESTED_MULTIPART_PARSER', + {"separator": "mixed", "querydict": False}) + data = { + "dtc.key": 'value', + "dtc.hh.oo": "sub", + "dtc.hh.aa": "sub2" + } + results = self.parser_boundary(data) + + expected = { + "dtc": { + "key": "value", + "hh": { + "aa": "sub2", + "oo": "sub" + } + } + } + + self.assertDictEqual(results.data, expected) + def test_nested_files(self): file = SimpleUploadedFile("file.png", b"file_content", content_type="image/png")