From 1c54041385324b350f4e2340938f2503d8013c7a Mon Sep 17 00:00:00 2001 From: yezy Date: Tue, 1 Jun 2021 19:31:07 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9BFix=20ignoring=20query=20&=20parsed?= =?UTF-8?q?=5Fquery=20kwargs=20-=20This=20happens=20when=20request=20is=20?= =?UTF-8?q?not=20None=20and=20request=20query=20parameter=20is=20not=20pas?= =?UTF-8?q?sed=20-=20Fixes=20#241?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- django_restql/mixins.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/django_restql/mixins.py b/django_restql/mixins.py index 9a85b9c..0a514bd 100644 --- a/django_restql/mixins.py +++ b/django_restql/mixins.py @@ -133,7 +133,7 @@ def __init__(self, *args, **kwargs): assert not(is_query_kwarg_set and is_parsed_query_kwarg_set), msg # flag to toggle using restql fields - self._use_restql_fields = False + self._ready_to_use_dynamic_fields = False # initialize parsed restql query self.parsed_restql_query = None @@ -143,7 +143,7 @@ def __init__(self, *args, **kwargs): def to_representation(self, instance): # Activate using restql fields - self._use_restql_fields = True + self._ready_to_use_dynamic_fields = True if self.restql_kwargs["return_pk"]: return instance.pk @@ -360,16 +360,6 @@ def intersection(lst1, lst2): @cached_property def restql_fields(self): - request = self.context.get('request') - - is_not_a_request_to_process = ( - self.restql_kwargs["disable_dynamic_fields"] or - not (request is None or self.has_restql_query_param(request)) - ) - - if is_not_a_request_to_process: - return self.allowed_fields - is_top_retrieve_request = ( self.field_name is None and self.parent is None @@ -419,20 +409,24 @@ def get_parsed_restql_query_from_query_kwarg(self): return parser.parse(self.restql_kwargs["query"]) def get_parsed_restql_query(self): + request = self.context.get('request') + if self.restql_kwargs["query"] is not None: # Get from query kwarg return self.get_parsed_restql_query_from_query_kwarg() elif self.restql_kwargs["parsed_query"] is not None: # Get from parsed_query kwarg return self.restql_kwargs["parsed_query"] - # Get from request query parameter - request = self.context.get('request', None) - return self.get_parsed_restql_query_from_req(request) + elif request is not None and self.has_restql_query_param(request): + # Get from request query parameter + return self.get_parsed_restql_query_from_req(request) + return None # There is no query so we return None as a parsed query @property def fields(self): - if self._use_restql_fields: - # Use restql fields + dynamic_fields_disabled = self.restql_kwargs["disable_dynamic_fields"] + if self._ready_to_use_dynamic_fields and not dynamic_fields_disabled: + # Return restql fields return self.restql_fields return self.allowed_fields