Skip to content

Commit 83fa9b9

Browse files
authored
Merge pull request #22 from manjitkumar/add-python3-support
add python3 support
2 parents 65ec84d + 54999dc commit 83fa9b9

File tree

7 files changed

+34
-24
lines changed

7 files changed

+34
-24
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ to query you want to make on the column name on the queryset.
2929
# validations.py
3030

3131
```python
32+
import six
3233

33-
from filters.schema import base_query_param_schema
34+
from filters.schema import base_query_params_schema
3435
from filters.validations import (
3536
CSVofIntegers,
3637
IntegerLike,
@@ -41,7 +42,7 @@ from filters.validations import (
4142
players_query_schema = base_query_param_schema.extend(
4243
{
4344
"id": IntegerLike(),
44-
"name": unicode,
45+
"name": six.text_type, # Depends on python version
4546
"team_id": CSVofIntegers(), # /?team_id=1,2,3
4647
"install_ts": DatetimeWithTZ(),
4748
"update_ts": DatetimeWithTZ(),

docs/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ to query you want to make on the column name on the queryset.
2929
# validations.py
3030

3131
```python
32+
import six
3233

33-
from filters.schema import base_query_param_schema
34+
from filters.schema import base_query_params_schema
3435
from filters.validations import (
3536
CSVofIntegers,
3637
IntegerLike,
@@ -41,7 +42,7 @@ from filters.validations import (
4142
players_query_schema = base_query_param_schema.extend(
4243
{
4344
"id": IntegerLike(),
44-
"name": unicode,
45+
"name": six.text_type, # Depends on python version
4546
"team_id": CSVofIntegers(), # /?team_id=1,2,3
4647
"install_ts": DatetimeWithTZ(),
4748
"update_ts": DatetimeWithTZ(),

example_app/validations.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1+
import six
12

2-
from filters.schema import base_query_param_schema
3+
from filters.schema import base_query_params_schema
34
from filters.validations import (
45
CSVofIntegers,
56
IntegerLike,
67
DatetimeWithTZ
78
)
89

910
# make a validation schema for players filter query params
10-
players_query_schema = base_query_param_schema.extend(
11+
players_query_schema = base_query_params_schema.extend(
1112
{
1213
"id": IntegerLike(),
13-
"name": unicode,
14+
"name": six.text_type,
1415
"team_id": CSVofIntegers(), # /?team_id=1,2,3
1516
"install_ts": DatetimeWithTZ(),
1617
"update_ts": DatetimeWithTZ(),
1718
}
1819
)
1920

20-
teams_query_schema = base_query_param_schema.extend(
21+
teams_query_schema = base_query_params_schema.extend(
2122
{
2223
"id": IntegerLike(),
23-
"name": unicode,
24+
"name": six.text_type,
2425
"player_id": CSVofIntegers(), # /?player_id=1,2,3
2526
"install_ts": DatetimeWithTZ(),
2627
"update_ts": DatetimeWithTZ(),

filters/mixins.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import six
12
from voluptuous import Invalid
23
from rest_framework.exceptions import ParseError
34

@@ -37,7 +38,11 @@ def __get_queryset_filters(self, query_params, *args, **kwargs):
3738
except Invalid as inst:
3839
raise ParseError(detail=inst)
3940

40-
for query, value in query_params.iteritems():
41+
iterable_query_params = (
42+
query_params.iteritems() if six.PY2 else query_params.items()
43+
)
44+
45+
for query, value in iterable_query_params:
4146
# [1] ~ sign is used to exclude a filter.
4247
is_exclude = '~' in query
4348
if query in self.filter_mappings and value:

filters/schema.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import six
12
from voluptuous import Schema, ALLOW_EXTRA
23
from .validations import (
34
IntegerLike,
@@ -8,8 +9,8 @@
89
# specific requirements.
910
base_query_params_schema = Schema(
1011
{
11-
'q': unicode,
12-
'name': unicode,
12+
'q': six.text_type,
13+
'name': six.text_type,
1314
'offset': IntegerLike(),
1415
'limit': IntegerLike(),
1516
'install_ts': DatetimeWithTZ(),

filters/validations.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# This module is define and keep all generic type of data-validations.
2+
import six
23
import re
34
from voluptuous import Invalid
45
from django.utils.dateparse import parse_datetime, parse_date
@@ -15,10 +16,10 @@ def IntegerLike(msg=None):
1516
def fn(value):
1617
if not (
1718
isinstance(value, int) or
18-
isinstance(value, long) or
1919
(isinstance(value, float) and value.is_integer()) or
2020
(isinstance(value, str) and value.isdigit()) or
21-
(isinstance(value, unicode) and value.isdigit())
21+
((isinstance(value, unicode) and value.isdigit() or
22+
isinstance(value, long)) if six.PY2 else None)
2223
):
2324
raise Invalid(msg or (
2425
'Invalid input <{0}>; expected an integer'.format(value))
@@ -39,10 +40,10 @@ def Alphanumeric(msg=None):
3940
def fn(value):
4041
if not (
4142
isinstance(value, int) or
42-
isinstance(value, long) or
4343
(isinstance(value, float) and value.is_integer()) or
4444
(isinstance(value, str) and value.isalnum()) or
45-
(isinstance(value, unicode) and value.isalnum())
45+
((isinstance(value, unicode) and value.isdigit() or
46+
isinstance(value, long)) if six.PY2 else None)
4647
):
4748
raise Invalid(msg or (
4849
'Invalid input <{0}>; expected an integer'.format(value))
@@ -64,7 +65,7 @@ def StrictlyAlphanumeric(msg=None):
6465
'''
6566
def fn(value):
6667
if not (
67-
(isinstance(value, str) or isinstance(value, unicode)) and
68+
(isinstance(value, str) or (isinstance(value, unicode) if six.PY2 else None)) and
6869
re_alphabets.search(value) and
6970
re_digits.search(value)
7071
):
@@ -101,15 +102,15 @@ def CSVofIntegers(msg=None):
101102
'''
102103
def fn(value):
103104
try:
104-
if isinstance(value, unicode):
105+
if isinstance(value, six.text_type):
105106
if ',' in value:
106-
value = map(
107+
value = list(map(
107108
int, filter(
108-
bool, map(
109+
bool, list(map(
109110
lambda x: x.strip(), value.split(',')
110-
)
111+
))
111112
)
112-
)
113+
))
113114
return value
114115
else:
115116
return [int(value)]

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
try:
77
import atexit
88
import pypandoc
9-
README = pypandoc.convert('README.md', 'rst')
9+
README = pypandoc.convert('README.md', 'rst', 'markdown')
1010
with open('README.rst', 'w') as f:
1111
f.write(README)
1212
atexit.register(lambda: os.unlink('README.rst'))
@@ -19,7 +19,7 @@
1919
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
2020

2121
__name__ = 'drf-url-filters'
22-
__version__ = '0.2.1'
22+
__version__ = '0.3.0'
2323
__author__ = 'Manjit Kumar'
2424
__author_email__ = '[email protected]'
2525
__url__ = 'https://github.com/manjitkumar/drf-url-filters'

0 commit comments

Comments
 (0)