A parser for nested data in multipart form
Project description
Nested-multipart-parser
Parser for nested data in multipart form, you can use it anyways, and you have a django rest framework integration
Installation
pip install nested-multipart-parser
Usage
What is doing
the parser take the request data and transform to dictionary
exemple:
# input
{
'title': 'title',
'date': "time",
'simple_object[my_key]': 'title'
'simple_object[my_list][0]': True
'langs[0][id]': 666,
'langs[0][title]': 'title',
'langs[0][description]': 'description',
'langs[0][language]': "language",
'langs[1][id]': 4566,
'langs[1][title]': 'title1',
'langs[1][description]': 'description1',
'langs[1][language]': "language1"
}
# results are:
{
'title': 'title',
'date': "time",
'simple_object': {
'my_key': 'title',
'my_list': [
True
]
},
'langs': [
{
'id': 666,
'title': 'title',
'description': 'description',
'language': 'language'
},
{
'id': 4566,
'title': 'title1',
'description': 'description1',
'language': 'language1'
}
]
}
How is work
for this working perfectly you need to follow this rules:
- a first key need to be set ex: 'title[0]' or 'title', in both the first key is 'title'
- each sub key need to enclose by brackets "[--your-key--]"
- if sub key are a full number, is converted to list
- if sub key is Not a number is converted to dictionary
- the key can't be rewite ex:
data = {
'title[0]': 'my-value'
}
# output
output = {
'title': [
'my-value'
]
}
# invalid key
data = {
'title[688]': 'my-value'
}
# ERROR , you set a number is upper thans actual list
# wrong format
data = {
'title[0]]]': 'my-value',
'title[0': 'my-value',
'title[': 'my-value',
'title[]': 'my-value',
'[]': 'my-value',
}
data = {
'title': 42,
'title[object]': 42
}
# Error , title as alerady set by primitive value (int, boolean or string)
# many element in list
data = {
'title[0]': 'my-value',
'title[1]': 'my-second-value'
}
# output
output = {
'title': [
'my-value',
'my-second-value'
]
}
# converted to object
data = {
'title[key0]': 'my-value',
'title[key7]': 'my-second-value'
}
# output
output = {
'title': {
'key0': 'my-value',
'key7': 'my-second-value'
}
}
# you have no limit for chained key
data = {
'the[0][chained][key][0][are][awesome][0][0]': 'im here !!'
}
# output
output: {
'the': [
{
'chained':{
'key': [
{
'are': {
'awesome':
[
[
'im here !!'
]
]
}
}
]
}
}
]
}
How to use it
for every framwork
from nested_multipart_parser import NestedParser
def my_view():
parser = NestedParser(data)
if parser.is_valid():
validate_data = parser.validate_data
...
else:
print(parser.errors)
for django rest framwork
from nested_multipart_parser.drf import DrfNestedParser
...
class YourViewSet(viewsets.ViewSet):
parser_classes = (DrfNestedParser,)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file nested-multipart-parser-0.1.0.tar.gz.
File metadata
- Download URL: nested-multipart-parser-0.1.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c05d85bb9c301c99af6380866cfd689762fcaa043c35d0c47fc3583397aa759
|
|
| MD5 |
b29ef6da399182ae47462b164684f376
|
|
| BLAKE2b-256 |
9b329cbb58536d132891792912ecd0c585b468e51e9004ea791cc114318a9c8d
|