def _get_model_filter(self, identifiers_prefix, identifiers, model, resource, request, filters_fields_rfs): """ :param identifiers_prefix: because filters are recursive if model relations property contains list of identifiers that was used for recursive searching the filter. :param identifiers: list of identifiers that conclusively identifies the filter. :param model: django model class. :param resource: resource object. :param request: django HTTP request. :param filters_fields_rfs: RFS of fields that is allowed to filter. :return: method returns filter from model fields or methods """ current_identifier = identifiers[0] identifiers_suffix = identifiers[1:] if current_identifier not in filters_fields_rfs: raise FilterIdentifierError suffix = LOOKUP_SEP.join(identifiers_suffix) model_field = get_field_or_none(model, current_identifier) model_method = get_method_or_none(model, current_identifier) if model_field and model_field.filter and (not suffix or suffix in model_field.filter.get_suffixes()): return model_field.filter(identifiers_prefix, [current_identifier], identifiers_suffix, field=model_field) elif model_field and model_field.is_relation and model_field.related_model: # recursive search for filter via related model fields next_model = model_field.related_model next_resource = get_resource_or_none(request, next_model, getattr(resource, 'resource_typemapper')) return self._get_filter_recursive( identifiers_prefix + [identifiers[0]], identifiers[1:], next_model, next_resource, request, filters_fields_rfs[current_identifier].subfieldset ) elif model_method: return self._get_method_filter(model_method, identifiers_prefix, [current_identifier], identifiers_suffix, model, resource, request, filters_fields_rfs)
def _get_order_string_from_model(self, identifiers_prefix, identifiers, model, resource, request, order_fields_rfs): """ :param identifiers_prefix: because order strings are recursive if model relations property contains list of identifiers that was used for recursive searching the order string. :param identifiers: list of identifiers that conclusively identifies order string. :param model: django model class. :param resource: resource object. :param request: django HTTP request. :param order_fields_rfs: RFS of fields that is allowed to order. :return: db order method string that is obtained from model fields or methods. """ current_identifier = identifiers[0] identifiers_suffix = identifiers[1:] if current_identifier not in order_fields_rfs: raise OrderIdentifierError model_field = get_field_or_none(model, current_identifier) model_method = get_method_or_none(model, current_identifier) if model_field and not identifiers_suffix and (not model_field.is_relation or model_field.related_model): return LOOKUP_SEP.join(identifiers_prefix + identifiers) elif model_field and model_field.is_relation and model_field.related_model: next_model = model_field.related_model next_resource = get_resource_or_none(request, next_model, getattr(resource, 'resource_typemapper')) return self._get_order_string_recursive( identifiers_prefix + [identifiers[0]], identifiers[1:], next_model, next_resource, request, order_fields_rfs[current_identifier].subfieldset ) elif model_method and not identifiers_suffix: return self._get_order_string_from_method( model_method, identifiers_prefix, identifiers, model, resource, request, order_fields_rfs )
def _get_sorter_from_model(self, identifiers_prefix, identifiers, direction, model, resource, request, order_fields_rfs): """ :param identifiers_prefix: because order strings are recursive if model relations property contains list of identifiers that was used for recursive searching the order string. :param identifiers: list of identifiers that conclusively identifies order string. :param direction: direction of ordering ASC or DESC. :param model: django model class. :param resource: resource object. :param request: django HTTP request. :param order_fields_rfs: RFS of fields that is allowed to order. :return: db order method string that is obtained from model fields or methods. """ current_identifier = identifiers[0] identifiers_suffix = identifiers[1:] if current_identifier not in order_fields_rfs: raise OrderIdentifierError model_field = get_field_or_none(model, current_identifier) model_method = get_method_or_none(model, current_identifier) if model_field and not identifiers_suffix and (not model_field.is_relation or model_field.related_model): return DefaultSorter(identifiers_prefix + identifiers, direction) elif model_field and model_field.is_relation and model_field.related_model: next_model = model_field.related_model next_resource = get_resource_or_none(request, next_model, getattr(resource, 'resource_typemapper')) return self._get_sorter_recursive( identifiers_prefix + [identifiers[0]], identifiers[1:], direction, next_model, next_resource, request, order_fields_rfs[current_identifier].subfieldset ) elif model_method and not identifiers_suffix: return self._get_sorter_from_method( model_method, identifiers_prefix, identifiers, direction, model, resource, request, order_fields_rfs )
def _get_model_filter(self, identifiers_prefix, identifiers, model, resource, request, filters_fields_rfs): """ :param identifiers_prefix: because filters are recursive if model relations property contains list of identifiers that was used for recursive searching the filter. :param identifiers: list of identifiers that conclusively identifies the filter. :param model: django model class. :param resource: resource object. :param request: django HTTP request. :param filters_fields_rfs: RFS of fields that is allowed to filter. :return: method returns filter from model fields or methods """ current_identifier = identifiers[0] identifiers_suffix = identifiers[1:] if current_identifier not in filters_fields_rfs: raise FilterIdentifierError suffix = LOOKUP_SEP.join(identifiers_suffix) model_field = get_field_or_none(model, current_identifier) model_method = get_method_or_none(model, current_identifier) if model_field and model_field.filter and ( not suffix or suffix in model_field.filter.get_suffixes()): return model_field.filter(identifiers_prefix, [current_identifier], identifiers_suffix, field=model_field) elif model_field and model_field.is_relation and model_field.related_model: # recursive search for filter via related model fields next_model = model_field.related_model next_resource = get_resource_or_none( request, next_model, getattr(resource, 'resource_typemapper')) return self._get_filter_recursive( identifiers_prefix + [identifiers[0]], identifiers[1:], next_model, next_resource, request, filters_fields_rfs[current_identifier].subfieldset) elif model_method: return self._get_method_filter(model_method, identifiers_prefix, [current_identifier], identifiers_suffix, model, resource, request, filters_fields_rfs)