How to use the djangorestframework.settings.api_settings function in djangorestframework

To help you get started, we’ve selected a few djangorestframework examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github encode / django-rest-framework / djangorestframework / views.py View on Github external
whitespace_pattern = '^' + (' ' * min(whitespace_counts))
        return re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content)
    return content


def _camelcase_to_spaces(content):
    """
    Translate 'CamelCaseNames' to 'Camel Case Names'.
    Used when generating names from view classes.
    """
    camelcase_boundry = '(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))'
    return re.sub(camelcase_boundry, ' \\1', content).strip()


class APIView(_View):
    renderer_classes = api_settings.DEFAULT_RENDERERS
    parser_classes = api_settings.DEFAULT_PARSERS
    authentication_classes = api_settings.DEFAULT_AUTHENTICATION
    throttle_classes = api_settings.DEFAULT_THROTTLES
    permission_classes = api_settings.DEFAULT_PERMISSIONS

    @classmethod
    def as_view(cls, **initkwargs):
        """
        Override the default :meth:`as_view` to store an instance of the view
        as an attribute on the callable function.  This allows us to discover
        information about the view when we do URL reverse lookups.
        """
        view = super(APIView, cls).as_view(**initkwargs)
        view.cls_instance = cls(**initkwargs)
        return view
github encode / django-rest-framework / djangorestframework / request.py View on Github external
return not getattr(obj, name) is Empty


class Request(object):
    """
    Wrapper allowing to enhance a standard `HttpRequest` instance.

    Kwargs:
        - request(HttpRequest). The original request instance.
        - parsers_classes(list/tuple). The parsers to use for parsing the
          request content.
        - authentication_classes(list/tuple). The authentications used to try
          authenticating the request's user.
    """

    _METHOD_PARAM = api_settings.FORM_METHOD_OVERRIDE
    _CONTENT_PARAM = api_settings.FORM_CONTENT_OVERRIDE
    _CONTENTTYPE_PARAM = api_settings.FORM_CONTENTTYPE_OVERRIDE

    def __init__(self, request, parser_classes=None, authentication_classes=None):
        self._request = request
        self.parser_classes = parser_classes or ()
        self.authentication_classes = authentication_classes or ()
        self._data = Empty
        self._files = Empty
        self._method = Empty
        self._content_type = Empty
        self._stream = Empty

    def get_parsers(self):
        """
        Instantiates and returns the list of parsers the request will use.
github encode / django-rest-framework / djangorestframework / response.py View on Github external
def get_renderers(self):
        """
        Instantiates and returns the list of renderers the response will use.
        """
        if self.renderer_classes is None:
            renderer_classes = api_settings.DEFAULT_RENDERERS
        else:
            renderer_classes = self.renderer_classes

        if self.format:
            return [cls(self.view) for cls in renderer_classes
                    if cls.format == self.format]
        return [cls(self.view) for cls in renderer_classes]
github encode / django-rest-framework / djangorestframework / request.py View on Github external
def _not_authenticated(self):
        """
        Return a two-tuple of (user, authtoken), representing an
        unauthenticated request.

        By default this will be (AnonymousUser, None).
        """
        if api_settings.UNAUTHENTICATED_USER:
            user = api_settings.UNAUTHENTICATED_USER()
        else:
            user = None

        if api_settings.UNAUTHENTICATED_TOKEN:
            auth = api_settings.UNAUTHENTICATED_TOKEN()
        else:
            auth = None

        return (user, auth)
github encode / django-rest-framework / djangorestframework / response.py View on Github external
class NotAcceptable(Exception):
    pass


class Response(SimpleTemplateResponse):
    """
    An HttpResponse that may include content that hasn't yet been serialized.

    Kwargs:
        - content(object). The raw content, not yet serialized.
        This must be native Python data that renderers can handle.
        (e.g.: `dict`, `str`, ...)
        - renderer_classes(list/tuple). The renderers to use for rendering the response content.
    """

    _ACCEPT_QUERY_PARAM = api_settings.URL_ACCEPT_OVERRIDE
    _IGNORE_IE_ACCEPT_HEADER = True

    def __init__(self, content=None, status=None, headers=None, view=None,
                 request=None, renderer_classes=None, format=None):
        # First argument taken by `SimpleTemplateResponse.__init__` is template_name,
        # which we don't need
        super(Response, self).__init__(None, status=status)

        self.raw_content = content
        self.has_content_body = content is not None
        self.headers = headers and headers[:] or []
        self.view = view
        self.request = request
        self.renderer_classes = renderer_classes
        self.format = format
github encode / django-rest-framework / djangorestframework / views.py View on Github external
def _camelcase_to_spaces(content):
    """
    Translate 'CamelCaseNames' to 'Camel Case Names'.
    Used when generating names from view classes.
    """
    camelcase_boundry = '(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))'
    return re.sub(camelcase_boundry, ' \\1', content).strip()


class APIView(_View):
    renderer_classes = api_settings.DEFAULT_RENDERERS
    parser_classes = api_settings.DEFAULT_PARSERS
    authentication_classes = api_settings.DEFAULT_AUTHENTICATION
    throttle_classes = api_settings.DEFAULT_THROTTLES
    permission_classes = api_settings.DEFAULT_PERMISSIONS

    @classmethod
    def as_view(cls, **initkwargs):
        """
        Override the default :meth:`as_view` to store an instance of the view
        as an attribute on the callable function.  This allows us to discover
        information about the view when we do URL reverse lookups.
        """
        view = super(APIView, cls).as_view(**initkwargs)
        view.cls_instance = cls(**initkwargs)
        return view

    @property
    def allowed_methods(self):
        """
        Return the list of allowed HTTP methods, uppercased.
github encode / django-rest-framework / djangorestframework / request.py View on Github external
def _not_authenticated(self):
        """
        Return a two-tuple of (user, authtoken), representing an
        unauthenticated request.

        By default this will be (AnonymousUser, None).
        """
        if api_settings.UNAUTHENTICATED_USER:
            user = api_settings.UNAUTHENTICATED_USER()
        else:
            user = None

        if api_settings.UNAUTHENTICATED_TOKEN:
            auth = api_settings.UNAUTHENTICATED_TOKEN()
        else:
            auth = None

        return (user, auth)
github encode / django-rest-framework / djangorestframework / renderers.py View on Github external
context = RequestContext(self.view.request, {
            'content': content,
            'view': self.view,
            'request': self.view.request,
            'response': self.view.response,
            'description': description,
            'name': name,
            'version': VERSION,
            'breadcrumblist': breadcrumb_list,
            'allowed_methods': self.view.allowed_methods,
            'available_formats': self.view._rendered_formats,
            'put_form': put_form_instance,
            'post_form': post_form_instance,
            'FORMAT_PARAM': self._FORMAT_QUERY_PARAM,
            'METHOD_PARAM': getattr(self.view, '_METHOD_PARAM', None),
            'api_settings': api_settings
        })

        ret = template.render(context)

        # Munge DELETE Response code to allow us to return content
        # (Do this *after* we've rendered the template so that we include
        # the normal deletion response code in the output)
        if self.view.response.status_code == 204:
            self.view.response.status_code = 200

        return ret
github encode / django-rest-framework / djangorestframework / views.py View on Github external
def finalize_response(self, request, response, *args, **kwargs):
        """
        Returns the final response object.
        """
        if isinstance(response, Response):
            response.view = self
            response.request = request
            response.renderer_classes = self.renderer_classes
            if api_settings.FORMAT_SUFFIX_KWARG:
                response.format = kwargs.get(api_settings.FORMAT_SUFFIX_KWARG, None)

        for key, value in self.headers.items():
            response[key] = value

        return response