Skip to content

Commit

Permalink
add python3 support
Browse files Browse the repository at this point in the history
 - replace iteritems with items
 - cast map into list to avoid python3 changes on iterables
 - This is a temp change, until we make a permanent fix
 - fix pypandoc exception
  • Loading branch information
RS Krishna authored and Manjit Kumar committed May 2, 2017
1 parent 65ec84d commit 54999dc
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
5 changes: 3 additions & 2 deletions docs/README.md
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
11 changes: 6 additions & 5 deletions example_app/validations.py
@@ -1,26 +1,27 @@
import six

from filters.schema import base_query_param_schema
from filters.schema import base_query_params_schema
from filters.validations import (
CSVofIntegers,
IntegerLike,
DatetimeWithTZ
)

# 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(),
Expand Down
7 changes: 6 additions & 1 deletion filters/mixins.py
@@ -1,3 +1,4 @@
import six
from voluptuous import Invalid
from rest_framework.exceptions import ParseError

Expand Down Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions filters/schema.py
@@ -1,3 +1,4 @@
import six
from voluptuous import Schema, ALLOW_EXTRA
from .validations import (
IntegerLike,
Expand All @@ -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(),
Expand Down
21 changes: 11 additions & 10 deletions 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
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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)
):
Expand Down Expand Up @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -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'))
Expand All @@ -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'
Expand Down

0 comments on commit 54999dc

Please sign in to comment.