Skip to content

Commit

Permalink
#7 feat: add options output type drf
Browse files Browse the repository at this point in the history
  • Loading branch information
remigermain committed Nov 12, 2022
1 parent a1b40aa commit 1a92c49
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
```

Expand Down
19 changes: 16 additions & 3 deletions nested_multipart_parser/drf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
12 changes: 5 additions & 7 deletions nested_multipart_parser/parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import re

DEFAULT_OPTIONS = {
"separator": "mixed-dot",
"raise_duplicate": True,
"assign_duplicate": False
}

class NestedParser:
_valid = None
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_drf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 1a92c49

Please sign in to comment.