Ejemplo n.º 1
0
 def convert_key(self, field_name, field):
     """Used so that that queries on fields with the source attribute set will work
     :param basestring field_name: text representation of the field name
     :param rest_framework.fields.Field field: Field instance
     """
     field = utils.decompose_field(field)
     source = field.source
     if source == '*':
         source = getattr(field, 'filter_key', None)
     return source or field_name
Ejemplo n.º 2
0
 def convert_key(self, field_name, field):
     """Used so that that queries on fields with the source attribute set will work
     :param basestring field_name: text representation of the field name
     :param rest_framework.fields.Field field: Field instance
     """
     field = utils.decompose_field(field)
     source = field.source
     if source == '*':
         source = getattr(field, 'filter_key', None)
     return source or field_name
Ejemplo n.º 3
0
 def convert_value(self, value, field):
     """Used to convert incoming values from query params to the appropriate types for filter comparisons
     :param basestring value: value to be resolved
     :param rest_framework.fields.Field field: Field instance
     """
     field = utils.decompose_field(field)
     if isinstance(field, ShowIfVersion):
         field = field.field
     if isinstance(field, ser.BooleanField):
         if utils.is_truthy(value):
             return True
         elif utils.is_falsy(value):
             return False
         else:
             raise InvalidFilterValue(
                 value=value,
                 field_type='bool',
             )
     elif isinstance(field, self.DATE_FIELDS):
         try:
             ret = date_parser.parse(value, ignoretz=False)
             if not ret.tzinfo:
                 ret = ret.replace(tzinfo=pytz.utc)
             return ret
         except ValueError:
             raise InvalidFilterValue(
                 value=value,
                 field_type='date',
             )
     elif isinstance(field,
                     (self.RELATIONSHIP_FIELDS, ser.SerializerMethodField,
                      ser.ManyRelatedField)):
         if value == 'null':
             value = None
         return value
     elif isinstance(field, self.LIST_FIELDS) or isinstance(
         (getattr(field, 'field', None)), self.LIST_FIELDS):
         if value == 'null':
             value = []
         return value
     else:
         try:
             return field.to_internal_value(value)
         except ValidationError:
             raise InvalidFilterValue(value=value, )
Ejemplo n.º 4
0
 def convert_value(self, value, field):
     """Used to convert incoming values from query params to the appropriate types for filter comparisons
     :param basestring value: value to be resolved
     :param rest_framework.fields.Field field: Field instance
     """
     field = utils.decompose_field(field)
     if isinstance(field, ShowIfVersion):
         field = field.field
     if isinstance(field, ser.BooleanField):
         if utils.is_truthy(value):
             return True
         elif utils.is_falsy(value):
             return False
         else:
             raise InvalidFilterValue(
                 value=value,
                 field_type='bool',
             )
     elif isinstance(field, self.DATE_FIELDS):
         try:
             ret = date_parser.parse(value, ignoretz=False)
             if not ret.tzinfo:
                 ret = ret.replace(tzinfo=pytz.utc)
             return ret
         except ValueError:
             raise InvalidFilterValue(
                 value=value,
                 field_type='date',
             )
     elif isinstance(field, (self.RELATIONSHIP_FIELDS, ser.SerializerMethodField)):
         if value == 'null':
             value = None
         return value
     elif isinstance(field, self.LIST_FIELDS) or isinstance((getattr(field, 'field', None)), self.LIST_FIELDS):
         if value == 'null':
             value = []
         return value
     else:
         try:
             return field.to_internal_value(value)
         except ValidationError:
             raise InvalidFilterValue(
                 value=value,
             )