Python django.utils.deprecation.MiddlewareMixin() Examples

The following are 3 code examples of django.utils.deprecation.MiddlewareMixin(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.utils.deprecation , or try the search function .
Example #1
Source File: middleware.py    From django-querycount with MIT License 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        # Call super first, so the MiddlewareMixin's __init__ does its thing.
        super(QueryCountMiddleware, self).__init__(*args, **kwargs)

        if settings.DEBUG:
            self.request_path = None
            self.stats = {"request": {}, "response": {}}
            self.dbs = [c.alias for c in connections.all()]
            self.queries = Counter()
            self._reset_stats()

            self._start_time = None
            self._end_time = None
            self.host = None  # The HTTP_HOST pulled from the request

            # colorizing methods
            self.white = termcolors.make_style(opts=('bold',), fg='white')
            self.red = termcolors.make_style(opts=('bold',), fg='red')
            self.yellow = termcolors.make_style(opts=('bold',), fg='yellow')
            self.green = termcolors.make_style(fg='green')

            # query type detection regex
            # TODO: make stats classification regex more robust
            self.threshold = QC_SETTINGS['THRESHOLDS'] 
Example #2
Source File: test_django.py    From python-dockerflow with Mozilla Public License 2.0 6 votes vote down vote up
def test_request_summary_failed_request(
    admin_user, caplog, dockerflow_middleware, dockerflow_request
):
    class HostileMiddleware(MiddlewareMixin):
        def process_request(self, request):
            # simulating resetting request changes
            delattr(request, "_id")
            delattr(request, "_start_timestamp")

        def process_response(self, request, response):
            return response

    hostile_middleware = HostileMiddleware()
    response = dockerflow_middleware.process_request(dockerflow_request)
    response = hostile_middleware.process_request(dockerflow_request)
    response = hostile_middleware.process_response(dockerflow_request, response)
    dockerflow_middleware.process_response(dockerflow_request, response)
    assert len(caplog.records) == 1
    record = caplog.records[0]
    assert getattr(record, "rid", None) is None
    assert getattr(record, "t", None) is None 
Example #3
Source File: middleware.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 4 votes vote down vote up
def process_view(self, request, view_func, args, kwargs):
        assert hasattr(request, 'user')
        if not request.user.is_authenticated:
            path = request.path_info.lstrip('/')
            if not any(m.match(path) for m in EXEMPT_URLS):
                return redirect(settings.LOGIN_URL)


# class AutoLoginMiddleware(MiddlewareMixin):
#     """
#     Auto login test user.
#     Create test user if needed.
#     settings should have AUTOLOGIN_TEST_USER_FORBIDDEN_URLS
#     and AUTOLOGIN_ALWAYS_OPEN_URLS
#     MIDDLEWARE_CLASSES should have 'django.contrib.auth.middleware.AuthenticationMiddleware'.
#     TEMPLATE_CONTEXT_PROCESSORS setting should include 'django.core.context_processors.auth'.
#
#     # settings for AutoLoginMiddleware
#     # urls available for unauthorized users,
#     # otherwise login as "test_user"
#     AUTOLOGIN_ALWAYS_OPEN_URLS = [
#         reverse_lazy('account_login'),
#     ]
#     # forbidden urls for "test user" (all account/* except login/logout)
#     AUTOLOGIN_TEST_USER_FORBIDDEN_URLS = [
#         'accounts/(?!login|logout)',
#     ]
#
#     """
#     TEST_USER_FORBIDDEN_URLS = [
#         re.compile(str(expr)) for expr in settings.AUTOLOGIN_TEST_USER_FORBIDDEN_URLS]
#
#     def process_view(self, request, view_func, args, kwargs):
#
#         if config.auto_login:
#
#             path = request.path_info
#             if path in settings.AUTOLOGIN_ALWAYS_OPEN_URLS:
#                 return
#
#             if not request.user.is_authenticated:
#                 get_test_user()
#                 user = authenticate(username='test_user', password='test_user')
#                 request.user = user
#                 login(request, user)
#
#             if request.user.username == 'test_user':
#                 if any(m.search(path) for m in self.TEST_USER_FORBIDDEN_URLS):
#                     return redirect(reverse_lazy('home'))