Example #1
0
 def perform_filtering(self, qs, value, context):
     handler = self.get_handler(context)
     if self.condition:
         return qs.where(self.condition)
     elif handler:
         return self.call_handler(qs, value, handler)
     raise FilterInvalidArgumentException("Both handler and condition were not found.")
Example #2
0
 def perform_filtering(self, qs, value, context=None):
     for f in self.filters:
         try:
             return f.filter(qs, value, context)
         except FilterInvalidArgumentException:
             # we're passing because just one nested filter has to pass
             pass
     raise FilterInvalidArgumentException("Invalid value: '{}'.".format(value))
Example #3
0
 def get_handler(self, context=None):
     if isinstance(self.method, str):
         method = getattr(context, self.method)
         assert method, "method '{}' not found in context '{}'".format(self.method, context)
         assert callable(method), "method '{}' must be callable".format(self.method)
         return method
     if self.method:
         raise FilterInvalidArgumentException("Method with type '{}' is not supported.".format(type(self.method)))
     return None
Example #4
0
 def perform_filtering(self, qs, value, context):
     for name, filter in self.filters.items():
         # if filter is FilterSet, we should pass the same value as we received
         filter_args = [value] if isinstance(filter, FilterSet) else value.get(name, [])
         for arg in filter_args:
             try:
                 qs = filter.filter(qs, arg, context=self)
             except FilterInvalidArgumentException as e:
                 if not self.ignore_failure:
                     raise FilterInvalidArgumentException("Filter '{}' raised error!".format(name)) from e
     return qs
Example #5
0
 def prepare_value(self, value):
     match = self._compiled.match(value)
     if not match:
         raise FilterInvalidArgumentException("Value '{}' not matches regex '{}'.".format(value, self.regex))
     return match
Example #6
0
 def prepare_value(self, value):
     if not isinstance(value, dict):
         raise FilterInvalidArgumentException("Invalid value. Should be dict, found '{}'.".format(value))
     return {k: v if isinstance(v, list) else [v] for k, v in value.items()}
Example #7
0
 def prepare_value(self, value):
     try:
         values = value.split(',')
         return [self.inner_filter.prepare_value(val) for val in values]
     except (AttributeError, ValueError) as e:
         raise FilterInvalidArgumentException("Value '{}' is not proper CSV.".format(value)) from e
Example #8
0
 def prepare_value(self, value):
     try:
         return self.type(value)
     except Exception as e:
         raise FilterInvalidArgumentException("Value '{}' not coercible to '{}'.".format(value, self.type)) from e