當前位置: 首頁>>代碼示例>>Python>>正文


Python deprecation.MiddlewareMixin方法代碼示例

本文整理匯總了Python中django.utils.deprecation.MiddlewareMixin方法的典型用法代碼示例。如果您正苦於以下問題:Python deprecation.MiddlewareMixin方法的具體用法?Python deprecation.MiddlewareMixin怎麽用?Python deprecation.MiddlewareMixin使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在django.utils.deprecation的用法示例。


在下文中一共展示了deprecation.MiddlewareMixin方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from django.utils import deprecation [as 別名]
# 或者: from django.utils.deprecation import MiddlewareMixin [as 別名]
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'] 
開發者ID:bradmontgomery,項目名稱:django-querycount,代碼行數:26,代碼來源:middleware.py

示例2: test_request_summary_failed_request

# 需要導入模塊: from django.utils import deprecation [as 別名]
# 或者: from django.utils.deprecation import MiddlewareMixin [as 別名]
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 
開發者ID:mozilla-services,項目名稱:python-dockerflow,代碼行數:23,代碼來源:test_django.py

示例3: process_view

# 需要導入模塊: from django.utils import deprecation [as 別名]
# 或者: from django.utils.deprecation import MiddlewareMixin [as 別名]
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')) 
開發者ID:LexPredict,項目名稱:lexpredict-contraxsuite,代碼行數:51,代碼來源:middleware.py


注:本文中的django.utils.deprecation.MiddlewareMixin方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。