Skip to content

Commit

Permalink
Merge pull request #15 from cloudblue/refactoring/LITE-18084
Browse files Browse the repository at this point in the history
LITE-18084 General Connect Flake8/pytest rules are applied
  • Loading branch information
maxipavlovic authored Apr 14, 2021
2 parents 051f04e + 867a750 commit e524d9a
Show file tree
Hide file tree
Showing 42 changed files with 420 additions and 329 deletions.
4 changes: 2 additions & 2 deletions dj_rql/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright © 2020 Ingram Micro Inc. All rights reserved.
# Copyright © 2021 Ingram Micro Inc. All rights reserved.
#

from django.db import models
Expand Down Expand Up @@ -190,7 +190,7 @@ class LogicalOperators:

@staticmethod
def get_grammar_key(key):
return '{}_op'.format(key)
return '{0}_op'.format(key)


class SearchOperators:
Expand Down
7 changes: 4 additions & 3 deletions dj_rql/drf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#
# Copyright © 2020 Ingram Micro Inc. All rights reserved.
# Copyright © 2021 Ingram Micro Inc. All rights reserved.
#

from dj_rql.drf.backend import FilterCache, RQLFilterBackend
from dj_rql.drf.paginations import RQLLimitOffsetPagination, RQLContentRangeLimitOffsetPagination
from dj_rql.drf._utils import get_query
from dj_rql.drf.backend import FilterCache, RQLFilterBackend
from dj_rql.drf.paginations import RQLContentRangeLimitOffsetPagination, RQLLimitOffsetPagination


__all__ = [
'get_query',
Expand Down
8 changes: 4 additions & 4 deletions dj_rql/drf/backend.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#
# Copyright © 2020 Ingram Micro Inc. All rights reserved.
# Copyright © 2021 Ingram Micro Inc. All rights reserved.
#

from rest_framework.filters import BaseFilterBackend

from dj_rql.drf._utils import get_query

from rest_framework.filters import BaseFilterBackend


class FilterCache:
CACHE = {}
Expand Down Expand Up @@ -61,7 +61,7 @@ def get_query(cls, filter_instance, request, view):

@staticmethod
def _get_filter_instance(filter_class, queryset, view):
qual_name = '{}.{}'.format(view.basename, filter_class.__name__)
qual_name = '{0}.{1}'.format(view.basename, filter_class.__name__)

filter_instance = FilterCache.CACHE.get(qual_name)
if filter_instance:
Expand Down
34 changes: 18 additions & 16 deletions dj_rql/drf/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
from dj_rql.constants import (
ComparisonOperators as CO,
DjangoLookups as DJL,
FilterTypes, RQL_NULL,
SearchOperators as SO,
FilterTypes,
RQL_ANY_SYMBOL,
RQL_FALSE,
RQL_LIMIT_PARAM,
RQL_NULL,
RQL_OFFSET_PARAM,
RQL_ORDERING_OPERATOR,
RQL_TRUE,
SearchOperators as SO,
)
from dj_rql.drf.backend import RQLFilterBackend
from dj_rql.drf._utils import get_query
from dj_rql.drf.backend import RQLFilterBackend
from dj_rql.exceptions import RQLFilterParsingError


Expand Down Expand Up @@ -178,18 +179,18 @@ def _get_one_filter_value_pair(cls, filter_instance, filter_name, value):
return

if filter_name in (RQL_LIMIT_PARAM, RQL_OFFSET_PARAM):
return '{}={}'.format(filter_name, value)
return '{0}={1}'.format(filter_name, value)

if filter_name in cls.RESERVED_ORDERING_WORDS:
return '{}({})'.format(RQL_ORDERING_OPERATOR, value)
return '{0}({1})'.format(RQL_ORDERING_OPERATOR, value)

f_item = filter_instance.get_filter_base_item(filter_name)
is_nc_item = f_item and (not f_item.get('custom', False))
if is_nc_item and FilterTypes.field_filter_type(f_item['field']) == FilterTypes.BOOLEAN:
value = cls._convert_bool_value(value)

if not cls._is_old_style_filter(filter_name):
return '{}={}'.format(filter_name, cls._add_quotes_to_value(value))
return '{0}={1}'.format(filter_name, cls._add_quotes_to_value(value))

return cls._convert_filter_to_rql(filter_name, value)

Expand All @@ -202,13 +203,13 @@ def _convert_filter_to_rql(cls, filter_name, value):
filter_base, lookup = cls._get_filter_and_lookup(filter_name)

if lookup == DJL.IN:
return 'in({},({}))'.format(
return 'in({0},({1}))'.format(
filter_base, ','.join(cls._add_quotes_to_value(v) for v in value.split(',') if v),
)

if lookup == DJL.NULL:
operator = CO.EQ if cls._convert_bool_value(value) == 'true' else CO.NE
return '{}={}={}'.format(filter_base, operator, RQL_NULL)
return '{0}={1}={2}'.format(filter_base, operator, RQL_NULL)

if lookup in (DJL.GT, DJL.GTE, DJL.LT, DJL.LTE):
if lookup == DJL.GTE:
Expand All @@ -217,18 +218,19 @@ def _convert_filter_to_rql(cls, filter_name, value):
operator = CO.LE
else:
operator = lookup
return '{}={}={}'.format(filter_base, operator, value)
return '{0}={1}={2}'.format(filter_base, operator, value)

operator = SO.I_LIKE if lookup[0] == 'i' else SO.LIKE
if lookup in (DJL.CONTAINS, DJL.I_CONTAINS, DJL.ENDSWITH, DJL.I_ENDSWITH) and \
value[0] != RQL_ANY_SYMBOL:

lookups = (DJL.CONTAINS, DJL.I_CONTAINS, DJL.ENDSWITH, DJL.I_ENDSWITH)
if lookup in lookups and value[0] != RQL_ANY_SYMBOL:
value = RQL_ANY_SYMBOL + value

if lookup in (DJL.CONTAINS, DJL.I_CONTAINS, DJL.STARTSWITH, DJL.I_STARTSWITH) and \
value[-1] != RQL_ANY_SYMBOL:
lookups = (DJL.CONTAINS, DJL.I_CONTAINS, DJL.STARTSWITH, DJL.I_STARTSWITH)
if lookup in lookups and value[-1] != RQL_ANY_SYMBOL:
value += RQL_ANY_SYMBOL

return '{}({},{})'.format(operator, filter_base, cls._add_quotes_to_value(value))
return '{0}({1},{2})'.format(operator, filter_base, cls._add_quotes_to_value(value))

@classmethod
def _convert_bool_value(cls, value):
Expand All @@ -243,7 +245,7 @@ def _convert_bool_value(cls, value):
def _add_quotes_to_value(cls, value):
for quote in ('"', "'"):
if quote not in value:
return '{q}{}{q}'.format(value, q=quote)
return '{q}{0}{q}'.format(value, q=quote)

cls._conversion_error()

Expand All @@ -262,7 +264,7 @@ def _get_filters_similar_to_old_syntax(cls, filter_instance):
if cls._is_old_style_filter(filter_name):
similar_to_old_syntax_filters.add(filter_name)

setattr(filter_instance, 'old_syntax_filters', similar_to_old_syntax_filters)
filter_instance.old_syntax_filters = similar_to_old_syntax_filters
return similar_to_old_syntax_filters

@classmethod
Expand Down
17 changes: 9 additions & 8 deletions dj_rql/drf/paginations.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#
# Copyright © 2020 Ingram Micro Inc. All rights reserved.
# Copyright © 2021 Ingram Micro Inc. All rights reserved.
#

from lark.exceptions import LarkError
from rest_framework.pagination import LimitOffsetPagination, _positive_int
from rest_framework.response import Response

from dj_rql.drf._utils import get_query
from dj_rql.exceptions import RQLFilterParsingError
from dj_rql.parser import RQLParser
from dj_rql.transformer import RQLLimitOffsetTransformer

from lark.exceptions import LarkError

from rest_framework.pagination import LimitOffsetPagination, _positive_int
from rest_framework.response import Response


class RQLLimitOffsetPagination(LimitOffsetPagination):
""" RQL limit offset pagination. """
Expand All @@ -26,7 +27,7 @@ def get_paginated_response_schema(self, schema):
def paginate_queryset(self, queryset, request, view=None):
rql_ast = None
try:
rql_ast = getattr(request, 'rql_ast')
rql_ast = request.rql_ast
except AttributeError:
query = get_query(request)
if query:
Expand Down Expand Up @@ -76,7 +77,7 @@ class RQLContentRangeLimitOffsetPagination(RQLLimitOffsetPagination):

def get_paginated_response(self, data):
length = len(data) - 1 if data else 0
content_range = "items {}-{}/{}".format(
content_range = 'items {0}-{1}/{2}'.format(
self.offset, self.offset + length, self.count,
)
return Response(data, headers={"Content-Range": content_range})
return Response(data, headers={'Content-Range': content_range})
12 changes: 8 additions & 4 deletions dj_rql/drf/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright © 2020 Ingram Micro Inc. All rights reserved.
# Copyright © 2021 Ingram Micro Inc. All rights reserved.
#

from collections import OrderedDict
Expand All @@ -14,7 +14,7 @@ def to_representation(self, instance):
def apply_rql_select(self):
rql_select = self._get_field_rql_select(self)

setattr(self, 'rql_select', rql_select)
self.rql_select = rql_select
deeper_rql_select = self._get_deeper_rql_select()

for field_name, is_included in rql_select['select'].items():
Expand Down Expand Up @@ -47,8 +47,12 @@ def _get_deeper_rql_select(self):
return self._deeper_rql_select

def _get_field_rql_select(self, field):
if field.parent and getattr(field.parent, 'many', False) \
and isinstance(field, field.parent.child.__class__):
take_parent = bool(
field.parent and getattr(field.parent, 'many', False) and isinstance(
field, field.parent.child.__class__,
),
)
if take_parent:
rql_field = field.parent
else:
rql_field = field
Expand Down
Loading

0 comments on commit e524d9a

Please sign in to comment.