def get_value(self): key = self.key assert key is not None request = self.serializer.context.get('request') type = self.type many = self.many return_list = False if many or type == 'integer': return_list = True value = request.query_params.getlist(key) else: return_list = False value = [request.query_params.get(key)] result = [] for v in value: if type == "boolean": if is_truthy(value): v = "True" elif value is not None: v = "False" elif type == "integer": v = str(int(v)) if v else None elif type == "decimal": v = str(Decimal(v)) if v else None else: v = str(v) if v else None result.append(v) return result if return_list else result[0]
def rewrite_filters(filters, serializer): out = {} for k, node in six.iteritems(filters): filter_key, field = node.generate_query_key(serializer) if isinstance(field, (BooleanField, NullBooleanField)): node.value = is_truthy(node.value) out[filter_key] = node.value return out
def test_is_truthy(self): self.assertTrue(is_truthy("faux")) self.assertTrue(is_truthy(1)) self.assertFalse(is_truthy("0")) self.assertFalse(is_truthy("False")) self.assertFalse(is_truthy("false")) self.assertFalse(is_truthy(""))
def get_request_patch_all(self): patch_all = self.get_request_feature(self.PATCH_ALL) if not patch_all: return None patch_all = patch_all.lower() if patch_all == 'query': pass elif is_truthy(patch_all): patch_all = True else: raise exceptions.ParseError('"%s" is not valid for %s' % (patch_all, self.PATCH_ALL)) return patch_all
def _get_requested_filters(self, **kwargs): """ Convert 'filters' query params into a dict that can be passed to Q. Returns a dict with two fields, 'include' and 'exclude', which can be used like: result = self._get_requested_filters() q = Q(**result['include'] & ~Q(**result['exclude']) """ filters_map = (kwargs.get('filters_map') or self.view.get_request_feature(self.view.FILTER)) out = TreeMap() for spec, value in six.iteritems(filters_map): # Inclusion or exclusion? if spec[0] == '-': spec = spec[1:] inex = '_exclude' else: inex = '_include' # for relational filters, separate out relation path part if '|' in spec: rel, spec = spec.split('|') rel = rel.split('.') else: rel = None parts = spec.split('.') # Last part could be operator, e.g. "events.capacity.gte" if len(parts) > 1 and parts[-1] in self.VALID_FILTER_OPERATORS: operator = parts.pop() else: operator = None # All operators except 'range' and 'in' should have one value if operator == 'range': value = value[:2] elif operator == 'in': # no-op: i.e. accept `value` as an arbitrarily long list pass elif operator in self.VALID_FILTER_OPERATORS: value = value[0] if (operator == 'isnull' and isinstance(value, six.string_types)): value = is_truthy(value) elif operator == 'eq': operator = None node = FilterNode(parts, operator, value) # insert into output tree path = rel if rel else [] path += [inex, node.key] out.insert(path, node) return out
def get_request_sideloading(self): sideloading = self.get_request_feature(self.SIDELOADING) return is_truthy(sideloading) if sideloading is not None else None
def get_request_debug(self): debug = self.get_request_feature(self.DEBUG) return is_truthy(debug) if debug is not None else None
def _get_requested_filters(self, **kwargs): """ Convert 'filters' query params into a dict that can be passed to Q. Returns a dict with two fields, 'include' and 'exclude', which can be used like: result = self._get_requested_filters() q = Q(**result['_include'] & ~Q(**result['_exclude']) """ filters_map = kwargs.get('filters_map') view = getattr(self, 'view', None) if view: serializer_class = view.get_serializer_class() serializer = serializer_class() if not filters_map: filters_map = view.get_request_feature(view.FILTER) else: serializer = None out = TreeMap() for key, value in six.iteritems(filters_map): # Inclusion or exclusion? if key[0] == '-': key = key[1:] category = '_exclude' else: category = '_include' # for relational filters, separate out relation path part if '|' in key: rel, key = key.split('|') rel = rel.split('.') else: rel = None terms = key.split('.') # Last part could be operator, e.g. "events.capacity.gte" if len(terms) > 1 and terms[-1] in self.VALID_FILTER_OPERATORS: operator = terms.pop() else: operator = None # All operators except 'range' and 'in' should have one value if operator == 'range': value = value[:2] if value[0] == '': operator = 'lte' value = value[1] elif value[1] == '': operator = 'gte' value = value[0] elif operator == 'in': # no-op: i.e. accept `value` as an arbitrarily long list pass elif operator in self.VALID_FILTER_OPERATORS: value = value[0] if (operator == 'isnull' and isinstance(value, six.string_types)): value = is_truthy(value) elif operator == 'eq': operator = None if serializer: s = serializer if rel: # get related serializer model_fields, serializer_fields = serializer.resolve(rel) s = serializer_fields[-1] s = getattr(s, 'serializer', s) rel = [Meta.get_query_name(f) for f in model_fields] # perform model-field resolution model_fields, serializer_fields = s.resolve(terms) field = serializer_fields[-1] if serializer_fields else None # if the field is a boolean, # coerce the value if field and isinstance( field, (serializers.BooleanField, serializers.NullBooleanField)): value = is_truthy(value) key = '__'.join([Meta.get_query_name(f) for f in model_fields]) else: key = '__'.join(terms) if operator: key += '__%s' % operator # insert into output tree path = rel if rel else [] path += [category, key] out.insert(path, value) return out