diff --git a/README.md b/README.md index 55aa321..6bfeca8 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,9 @@ to query you want to make on the column name on the queryset. # validations.py ```python +import six -from filters.schema import base_query_param_schema +from filters.schema import base_query_params_schema from filters.validations import ( CSVofIntegers, IntegerLike, @@ -41,7 +42,7 @@ from filters.validations import ( players_query_schema = base_query_param_schema.extend( { "id": IntegerLike(), - "name": unicode, + "name": six.text_type, # Depends on python version "team_id": CSVofIntegers(), # /?team_id=1,2,3 "install_ts": DatetimeWithTZ(), "update_ts": DatetimeWithTZ(), diff --git a/docs/README.md b/docs/README.md index 55aa321..c002c24 100644 --- a/docs/README.md +++ b/docs/README.md @@ -29,8 +29,9 @@ to query you want to make on the column name on the queryset. # validations.py ```python +import six -from filters.schema import base_query_param_schema +from filters.schema import base_query_params_schema from filters.validations import ( CSVofIntegers, IntegerLike, @@ -41,7 +42,7 @@ from filters.validations import ( players_query_schema = base_query_param_schema.extend( { "id": IntegerLike(), - "name": unicode, + "name": six.text_type, # Depends on python version "team_id": CSVofIntegers(), # /?team_id=1,2,3 "install_ts": DatetimeWithTZ(), "update_ts": DatetimeWithTZ(), diff --git a/example_app/validations.py b/example_app/validations.py index 554f411..5acf180 100644 --- a/example_app/validations.py +++ b/example_app/validations.py @@ -1,5 +1,6 @@ +import six -from filters.schema import base_query_param_schema +from filters.schema import base_query_params_schema from filters.validations import ( CSVofIntegers, IntegerLike, @@ -7,20 +8,20 @@ ) # make a validation schema for players filter query params -players_query_schema = base_query_param_schema.extend( +players_query_schema = base_query_params_schema.extend( { "id": IntegerLike(), - "name": unicode, + "name": six.text_type, "team_id": CSVofIntegers(), # /?team_id=1,2,3 "install_ts": DatetimeWithTZ(), "update_ts": DatetimeWithTZ(), } ) -teams_query_schema = base_query_param_schema.extend( +teams_query_schema = base_query_params_schema.extend( { "id": IntegerLike(), - "name": unicode, + "name": six.text_type, "player_id": CSVofIntegers(), # /?player_id=1,2,3 "install_ts": DatetimeWithTZ(), "update_ts": DatetimeWithTZ(), diff --git a/filters/mixins.py b/filters/mixins.py index 88640a5..1dfd840 100644 --- a/filters/mixins.py +++ b/filters/mixins.py @@ -1,3 +1,4 @@ +import six from voluptuous import Invalid from rest_framework.exceptions import ParseError @@ -37,7 +38,11 @@ def __get_queryset_filters(self, query_params, *args, **kwargs): except Invalid as inst: raise ParseError(detail=inst) - for query, value in query_params.iteritems(): + iterable_query_params = ( + query_params.iteritems() if six.PY2 else query_params.items() + ) + + for query, value in iterable_query_params: # [1] ~ sign is used to exclude a filter. is_exclude = '~' in query if query in self.filter_mappings and value: diff --git a/filters/schema.py b/filters/schema.py index fa06459..a0a8112 100644 --- a/filters/schema.py +++ b/filters/schema.py @@ -1,3 +1,4 @@ +import six from voluptuous import Schema, ALLOW_EXTRA from .validations import ( IntegerLike, @@ -8,8 +9,8 @@ # specific requirements. base_query_params_schema = Schema( { - 'q': unicode, - 'name': unicode, + 'q': six.text_type, + 'name': six.text_type, 'offset': IntegerLike(), 'limit': IntegerLike(), 'install_ts': DatetimeWithTZ(), diff --git a/filters/validations.py b/filters/validations.py index ecdc051..2181b25 100644 --- a/filters/validations.py +++ b/filters/validations.py @@ -1,4 +1,5 @@ # This module is define and keep all generic type of data-validations. +import six import re from voluptuous import Invalid from django.utils.dateparse import parse_datetime, parse_date @@ -15,10 +16,10 @@ def IntegerLike(msg=None): def fn(value): if not ( isinstance(value, int) or - isinstance(value, long) or (isinstance(value, float) and value.is_integer()) or (isinstance(value, str) and value.isdigit()) or - (isinstance(value, unicode) and value.isdigit()) + ((isinstance(value, unicode) and value.isdigit() or + isinstance(value, long)) if six.PY2 else None) ): raise Invalid(msg or ( 'Invalid input <{0}>; expected an integer'.format(value)) @@ -39,10 +40,10 @@ def Alphanumeric(msg=None): def fn(value): if not ( isinstance(value, int) or - isinstance(value, long) or (isinstance(value, float) and value.is_integer()) or (isinstance(value, str) and value.isalnum()) or - (isinstance(value, unicode) and value.isalnum()) + ((isinstance(value, unicode) and value.isdigit() or + isinstance(value, long)) if six.PY2 else None) ): raise Invalid(msg or ( 'Invalid input <{0}>; expected an integer'.format(value)) @@ -64,7 +65,7 @@ def StrictlyAlphanumeric(msg=None): ''' def fn(value): if not ( - (isinstance(value, str) or isinstance(value, unicode)) and + (isinstance(value, str) or (isinstance(value, unicode) if six.PY2 else None)) and re_alphabets.search(value) and re_digits.search(value) ): @@ -101,15 +102,15 @@ def CSVofIntegers(msg=None): ''' def fn(value): try: - if isinstance(value, unicode): + if isinstance(value, six.text_type): if ',' in value: - value = map( + value = list(map( int, filter( - bool, map( + bool, list(map( lambda x: x.strip(), value.split(',') - ) + )) ) - ) + )) return value else: return [int(value)] diff --git a/setup.py b/setup.py index fa0e046..e84bf47 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ try: import atexit import pypandoc - README = pypandoc.convert('README.md', 'rst') + README = pypandoc.convert('README.md', 'rst', 'markdown') with open('README.rst', 'w') as f: f.write(README) atexit.register(lambda: os.unlink('README.rst')) @@ -19,7 +19,7 @@ os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) __name__ = 'drf-url-filters' -__version__ = '0.2.1' +__version__ = '0.3.0' __author__ = 'Manjit Kumar' __author_email__ = 'manjit1727@gmail.com' __url__ = 'https://github.com/manjitkumar/drf-url-filters'