def get_filters(view):
    """
    For a given view set returns which query filters are available for it a
    Markdown formatted list. The list does not include query filters specified
    on serializer or query arguments used for paging.
    """
    if view in FILTERS_CACHE:
        return FILTERS_CACHE[view]

    allowed_keys = drf_introspection.get_allowed_query_params(view)
    filter_class = getattr(view, 'filter_class', None)
    filterset = filter_class() if filter_class is not None else None
    filterset_fields = filterset.filters if filterset is not None else []
    filter_fields = set(getattr(view, 'filter_fields', []))
    extra_query_params = set(getattr(view, 'extra_query_params', []))

    filters = []
    for key in sorted(allowed_keys):
        if key in filterset_fields:
            # filter defined in FilterSet
            filter = filterset_fields.get(key)
            filter_type = FILTER_DEFS.get(filter.__class__.__name__, 'string')
            lookup_type = LOOKUP_TYPES.get(filter.lookup_type)
            if lookup_type:
                lookup_type = ', %s' % lookup_type
            filters.append(' * `%s` (%s%s)' % (key, filter_type, lookup_type or ''))
        elif key in filter_fields or key in extra_query_params:
            # filter defined in viewset directly; type depends on model, not easily available
            filters.append(' * `%s`' % key)
        # else filter defined somewhere else and not relevant here (e.g.
        # serializer or pagination settings).
    filters = '\n'.join(filters)
    FILTERS_CACHE[view] = filters
    return filters
def get_filters(view):
    """
    For a given view set returns which query filters are available for it a
    Markdown formatted list. The list does not include query filters specified
    on serializer or query arguments used for paging.
    """
    if view in FILTERS_CACHE:
        return FILTERS_CACHE[view]

    allowed_keys = drf_introspection.get_allowed_query_params(view)
    filter_class = getattr(view, 'filter_class', None)
    filterset = filter_class() if filter_class is not None else None
    filterset_fields = filterset.filters if filterset is not None else []
    filter_fields = set(getattr(view, 'filter_fields', []))
    extra_query_params = set(getattr(view, 'extra_query_params', []))

    filters = []
    for key in sorted(allowed_keys):
        if key in filterset_fields:
            # filter defined in FilterSet
            filter = filterset_fields.get(key)
            filter_type = FILTER_DEFS.get(filter.__class__.__name__, 'string')
            lookup_type = LOOKUP_TYPES.get(filter.lookup_type)
            if lookup_type:
                lookup_type = ', %s' % lookup_type
            filters.append(' * `%s` (%s%s)' % (key, filter_type, lookup_type or ''))
        elif key in filter_fields or key in extra_query_params:
            # filter defined in viewset directly; type depends on model, not easily available
            filters.append(' * `%s`' % key)
        # else filter defined somewhere else and not relevant here (e.g.
        # serializer or pagination settings).
    filters = '\n'.join(filters)
    FILTERS_CACHE[view] = filters
    return filters
Пример #3
0
def get_filters(view):
    """
    For a given view set returns which query filters are available for it a
    Markdown formatted list. The list does not include query filters specified
    on serializer or query arguments used for paging.
    """
    allowed_keys = drf_introspection.get_allowed_query_params(view)
    filter_class = getattr(view, 'filter_class', None)
    filterset = filter_class() if filter_class is not None else None
    filterset_fields = filterset.filters if filterset is not None else []
    filter_fields = set(getattr(view, 'filter_fields', []))
    extra_query_params = set(getattr(view, 'extra_query_params', []))

    filters = []
    for key in sorted(allowed_keys):
        if key in filterset_fields:
            # filter defined in FilterSet
            filter = filterset_fields.get(key)
            filters.append(_get_filter(key, filter))
        elif key in filter_fields or key in extra_query_params:
            # filter defined in viewset directly; type depends on model, not easily available
            filters.append(' * `%s`' % key)
        # else filter defined somewhere else and not relevant here (e.g.
        # serializer or pagination settings).

    return '\n'.join(filters)
Пример #4
0
    def initial(self, request, *args, **kwargs):
        super(StrictQueryParamMixin, self).initial(request, *args, **kwargs)

        # We should not raise the exception if there is no handler for the
        # requested method, as it is better to return 405 METHOD NOT ALLOWED in
        # such case even if there are extra query parameters.
        if (request.method.lower() not in self.http_method_names or
                not hasattr(self, request.method.lower())):
            return

        allowed_keys = drf_introspection.get_allowed_query_params(self)
        extra_keys = set(request.query_params.keys()) - allowed_keys
        if extra_keys:
            raise FieldError('Unknown query params: %s.' % ', '.join(sorted(extra_keys)))
Пример #5
0
    def initial(self, request, *args, **kwargs):
        super(StrictQueryParamMixin, self).initial(request, *args, **kwargs)

        # We should not raise the exception if there is no handler for the
        # requested method, as it is better to return 405 METHOD NOT ALLOWED in
        # such case even if there are extra query parameters.
        if (request.method.lower() not in self.http_method_names or
                not hasattr(self, request.method.lower())):
            return

        allowed_keys = drf_introspection.get_allowed_query_params(self)
        extra_keys = set(request.query_params.keys()) - allowed_keys
        if extra_keys:
            raise FieldError('Unknown query params: %s.' % ', '.join(sorted(extra_keys)))