Skip to content

Commit

Permalink
LITE-27567 Add black
Browse files Browse the repository at this point in the history
* Double quotes replaced with single quotes
  • Loading branch information
Hairash committed Jun 8, 2023
1 parent 77be154 commit bc62db1
Show file tree
Hide file tree
Showing 30 changed files with 1,390 additions and 886 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,9 @@ Development
1. Python 3.8+
2. Install poetry: `pip install poetry`
3. Install dependencies: `poetry install`
4. We use `isort` library to order and format our imports, and we check it using `flake8-isort` library (automatically on `flake8` run).
For convenience you may run `isort .` to order imports.
4. We use `isort` library to order and format our imports, and `black` - to format the code.
We check it using `flake8-isort` and `flake8-black` libraries (automatically on `flake8` run).
For convenience you may run `isort . && black .` to format the code.
5. Run flake8: `poetry run flake8`

Testing
Expand Down
5 changes: 3 additions & 2 deletions dj_rql/_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ def __init__(self, queryset, select_data, filter_tree, filter_node=None, filter_


class FilterArgs:
def __init__(self, filter_name, operator, str_value, list_operator=None,
namespace=None, **kwargs):
def __init__(
self, filter_name, operator, str_value, list_operator=None, namespace=None, **kwargs
):
"""
:param str filter_name: Full filter name (f.e. ns1.ns2.filter1)
:param str operator: RQL operator (f.e. eq, like, etc.)
Expand Down
3 changes: 2 additions & 1 deletion dj_rql/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class FilterTypes(FT):
def field_filter_type(cls, field):
return next(
(
filter_type for base_cls, filter_type in cls.mapper
filter_type
for base_cls, filter_type in cls.mapper
if issubclass(field.__class__, base_cls)
),
cls.STRING,
Expand Down
22 changes: 14 additions & 8 deletions dj_rql/drf/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def get_rql_filter_class(self):
return ModelFilterClass
```
"""

OPENAPI_RETRIEVE_SPECIFICATION = False

_CACHES = {}
Expand All @@ -59,11 +60,13 @@ def filter_queryset(self, request, queryset, view):
filter_instance = self._get_filter_instance(filter_class, queryset, view)
query = self.get_query(filter_instance, request, view)

can_query_be_cached = all((
filter_class.QUERIES_CACHE_BACKEND,
filter_class.QUERIES_CACHE_SIZE,
request.method in ('GET', 'HEAD', 'OPTIONS'),
))
can_query_be_cached = all(
(
filter_class.QUERIES_CACHE_BACKEND,
filter_class.QUERIES_CACHE_SIZE,
request.method in ('GET', 'HEAD', 'OPTIONS'),
),
)
if can_query_be_cached:
# We must use the combination of queryset and query to make a cache key as
# queryset can already contain some filters (e.x. based on authentication)
Expand Down Expand Up @@ -117,7 +120,8 @@ def get_query(cls, filter_instance, request, view):
def _get_or_init_cache(cls, filter_class, view):
qual_name = cls._get_filter_cls_qual_name(view, filter_class)
return cls._CACHES.setdefault(
qual_name, filter_class.QUERIES_CACHE_BACKEND(int(filter_class.QUERIES_CACHE_SIZE)),
qual_name,
filter_class.QUERIES_CACHE_BACKEND(int(filter_class.QUERIES_CACHE_SIZE)),
)

@classmethod
Expand All @@ -135,6 +139,8 @@ def _get_filter_instance(cls, filter_class, queryset, view):
@staticmethod
def _get_filter_cls_qual_name(view, filter_class):
return '{0}.{1}+{2}.{3}'.format(
view.__class__.__module__, view.__class__.__name__,
filter_class.__module__, filter_class.__name__,
view.__class__.__module__,
view.__class__.__name__,
filter_class.__module__,
filter_class.__name__,
)
9 changes: 7 additions & 2 deletions dj_rql/drf/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CompatibilityRQLFilterBackend(RQLFilterBackend):
filter backend (without raising API version number or losing compatibility), this base
compatibility DRF backend must be inherited from.
"""

@classmethod
def get_query(cls, filter_instance, request, view):
try:
Expand Down Expand Up @@ -70,6 +71,7 @@ class DjangoFiltersRQLFilterBackend(CompatibilityRQLFilterBackend):
* ?&& syntax in queries;
* etc.
"""

RESERVED_ORDERING_WORDS = {'order_by', 'ordering'}

_POSSIBLE_DF_LOOKUPS = DJL.all()
Expand Down Expand Up @@ -161,7 +163,9 @@ def get_rql_query(cls, filter_instance, request, query_string):
one_filter_value_pairs = []
for value in request.query_params.getlist(filter_name):
name_value_pair = cls._get_one_filter_value_pair(
filter_instance, filter_name, value,
filter_instance,
filter_name,
value,
)
if name_value_pair is not None:
one_filter_value_pairs.append(name_value_pair)
Expand Down Expand Up @@ -202,7 +206,8 @@ def _convert_filter_to_rql(cls, filter_name, value):

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

if lookup == DJL.NULL:
Expand Down
18 changes: 11 additions & 7 deletions dj_rql/drf/paginations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


class RQLLimitOffsetPagination(LimitOffsetPagination):
""" RQL limit offset pagination. """
"""RQL limit offset pagination."""

def __init__(self, *args, **kwargs):
super(RQLLimitOffsetPagination, self).__init__(*args, **kwargs)
Expand All @@ -37,9 +37,11 @@ def paginate_queryset(self, queryset, request, view=None):
try:
self._rql_limit, self._rql_offset = RQLLimitOffsetTransformer().transform(rql_ast)
except LarkError:
raise RQLFilterParsingError(details={
'error': 'Limit and offset are set incorrectly.',
})
raise RQLFilterParsingError(
details={
'error': 'Limit and offset are set incorrectly.',
},
)

self.limit = self.get_limit(request)
if self.limit == 0:
Expand All @@ -62,7 +64,7 @@ def paginate_queryset(self, queryset, request, view=None):
if self.limit + self.offset > self.count:
self.limit = self.count - self.offset

return list(queryset[self.offset:self.offset + self.limit])
return list(queryset[self.offset : self.offset + self.limit])

def get_limit(self, *args):
if self._rql_limit is not None:
Expand All @@ -82,7 +84,7 @@ def get_offset(self, *args):


class RQLContentRangeLimitOffsetPagination(RQLLimitOffsetPagination):
""" RQL RFC2616 limit offset pagination.
"""RQL RFC2616 limit offset pagination.
Examples:
```
Expand All @@ -96,6 +98,8 @@ class RQLContentRangeLimitOffsetPagination(RQLLimitOffsetPagination):
def get_paginated_response(self, data):
length = len(data) - 1 if data else 0
content_range = 'items {0}-{1}/{2}'.format(
self.offset, self.offset + length, self.count,
self.offset,
self.offset + length,
self.count,
)
return Response(data, headers={'Content-Range': content_range})
9 changes: 6 additions & 3 deletions dj_rql/drf/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def apply_rql_select(self):

for field_name, is_included in rql_select['select'].items():
split_field_name = field_name.split('.')
is_current_level_field = (len(split_field_name) == 1)
is_current_level_field = len(split_field_name) == 1
current_depth_field_name = split_field_name[0]

if is_current_level_field:
Expand Down Expand Up @@ -48,8 +48,11 @@ def _get_deeper_rql_select(self):

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

0 comments on commit bc62db1

Please sign in to comment.