コード例 #1
0
def apply_view_middleware(request):
    """
    Apply all the `process_view` capable middleware configured
    into the given request.

    The logic is roughly copied from
    django.core.handlers.base.BaseHandler.get_response

    :param request: The request to massage.
    :type request: django.http.HttpRequest
    :return: The same request, massaged in-place.
    :rtype: django.http.HttpRequest
    """
    urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
    set_urlconf(urlconf)

    resolver = URLResolver(RegexPattern(r'^/'), urlconf)
    resolver_match = resolver.resolve(request.path_info)
    callback, callback_args, callback_kwargs = resolver_match
    request.resolver_match = resolver_match

    for middleware_path in get_middleware_classes():
        mw_class = import_string(middleware_path)
        try:
            mw_instance = mw_class()
        except MiddlewareNotUsed:
            continue

        if hasattr(mw_instance, 'process_view'):
            mw_instance.process_view(request, callback, callback_args,
                                     callback_kwargs)

    return request
コード例 #2
0
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 get_middleware_classes():
        mw_class = import_string(middleware_path)
        current_language = get_language()

        try:
            mw_instance = mw_class()
        except MiddlewareNotUsed:
            continue

        for key, value in attrs.items():
            setattr(request, key, value)

        if hasattr(mw_instance, 'process_request'):
            mw_instance.process_request(request)

        activate(current_language)

    assert request.shop

    if not attrs.get("skip_session", False):
        frm = inspect.stack()[1]
        mod = inspect.getmodule(frm[0])
        if mod.__name__.startswith("shuup_tests.admin"):
            shop_provider.set_shop(request, request.shop)

    return request
コード例 #3
0
                # fallback to LANGUAGE_CODE
                translation.activate(settings.LANGUAGE_CODE)
            request.LANGUAGE_CODE = translation.get_language()

    def _get_maintenance_response(self, request, view_func):
        # Allow media and static accesses in debug mode
        if settings.DEBUG and (
            request.path.startswith(settings.MEDIA_URL) or
            request.path.startswith(settings.STATIC_URL)
        ):
            return None

        if getattr(view_func, "maintenance_mode_exempt", False):
            return None
        if "login" in view_func.__name__:
            return None
        resolver_match = getattr(request, "resolver_match", None)
        if resolver_match and resolver_match.app_name == "shuup_admin":
            return None

        if request.shop.maintenance_mode and not is_admin_user(request):
            return HttpResponse(loader.render_to_string("shuup/front/maintenance.jinja", request=request), status=503)


if (
    "django.contrib.auth" in settings.INSTALLED_APPS and
    "shuup.front.middleware.ShuupFrontMiddleware" in get_middleware_classes()
):
    user_logged_in.connect(ShuupFrontMiddleware.refresh_on_user_change, dispatch_uid="shuup_front_refresh_on_login")
    user_logged_out.connect(ShuupFrontMiddleware.refresh_on_logout, dispatch_uid="shuup_front_refresh_on_logout")
コード例 #4
0
                translation.activate(settings.LANGUAGE_CODE)
            request.LANGUAGE_CODE = translation.get_language()

    def _get_maintenance_response(self, request, view_func):
        # Allow media and static accesses in debug mode
        if settings.DEBUG and (request.path.startswith(settings.MEDIA_URL) or
                               request.path.startswith(settings.STATIC_URL)):
            return None

        if getattr(view_func, "maintenance_mode_exempt", False):
            return None
        if "login" in view_func.__name__:
            return None
        resolver_match = getattr(request, "resolver_match", None)
        if resolver_match and resolver_match.app_name == "shuup_admin":
            return None

        if request.shop.maintenance_mode and not is_admin_user(request):
            return HttpResponse(loader.render_to_string(
                "shuup/front/maintenance.jinja", request=request),
                                status=503)


if ("django.contrib.auth" in settings.INSTALLED_APPS
        and "shuup.front.middleware.ShuupFrontMiddleware"
        in get_middleware_classes()):
    user_logged_in.connect(ShuupFrontMiddleware.refresh_on_user_change,
                           dispatch_uid="shuup_front_refresh_on_login")
    user_logged_out.connect(ShuupFrontMiddleware.refresh_on_logout,
                            dispatch_uid="shuup_front_refresh_on_logout")