Source code for shoop.testing.utils
# -*- coding: utf-8 -*-
# This file is part of Shoop.
#
# Copyright (c) 2012-2016, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.
from django.conf import settings
from django.core.exceptions import MiddlewareNotUsed
from django.utils.module_loading import import_string
[docs]def apply_request_middleware(request, **attrs):
"""
Apply all the `process_request` capable middleware configured
into the given request.
:param request: The request to massage.
:type request: django.http.HttpRequest
:param attrs: Additional attributes to set after massage.
:type attrs: dict
:return: The same request, massaged in-place.
:rtype: django.http.HttpRequest
"""
for middleware_path in settings.MIDDLEWARE_CLASSES:
mw_class = import_string(middleware_path)
try:
mw_instance = mw_class()
except MiddlewareNotUsed:
continue
if hasattr(mw_instance, 'process_request'):
mw_instance.process_request(request)
for key, value in attrs.items():
setattr(request, key, value)
return request