from schematics.models import Model
class QArgParser(Model):
"""
Arguments Parser for Sanic Requests
>>> from schematics.types import IntType
>>> class ListQueryParser(QArgParser):
... limit = IntType(min_value=1, default=10, max_value=100)
... offset = IntType(min_value=0, default=0)
>>> parser = ListQueryParser({'limit': ['5'], 'offset': ['0']})
>>> parser.validate()
>>> parser.to_native()
{'limit': 5, 'offset': 0}
"""
def __init__(self, args=None, **kw):
converted = {}
for name, field in self._fields.items():
df = field.deserialize_from or [name]
for key in df:
if key in args:
converted[key] = args[key] if field.primitive_type is list else args[key][-1]
super(QArgParser, self).__init__(raw_data=converted, **kw)