middleware.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:tumanov_castleoaks 作者: Roamdev 项目源码 文件源码
def process_request(request):
        # ????????? ?????? ????????????? ????? GET-????????
        if request.GET.get(options.MULTILANGUAGE_GET_PARAM):
            request.disable_autoredirect = True

            # ?????????, ??? ?? ??????? ????? ???? ????? ????????,
            # ?????, ?????????? ?? MULTILANGUAGE_FALLBACK_URL
            urlconf = getattr(request, 'urlconf', None)
            if not is_valid_path(request.path_info, urlconf):
                return redirect(options.MULTILANGUAGE_FALLBACK_URL)

            return

        # ????????? ?????? ????????????? ????? ????
        if request.COOKIES.get(options.MULTILANGUAGE_COOKIE_KEY, None):
            return

        # ????????? ?????? ????????????? ??? ???????????? User-Agent
        ua_string = request.META.get('HTTP_USER_AGENT', '').lower()
        for pattern in options.ROBOTS_UA:
            if pattern in ua_string:
                return

        # ????????? ?????????? ? IP
        ip = get_client_ip(request)
        ip_info = info(ip, detailed=True)
        if not ip_info:
            request.disable_autoredirect = True
            return

        # ??????????? ???? ?????, ?? ??????? ????? ???????????
        current_code = settings.LANGUAGE_CODE
        redirect_code = current_code
        try:
            ip_iso = ip_info.get('country').get('iso')
        except AttributeError:
            pass
        else:
            for code, opts in options.MULTILANGUAGE_SITES.items():
                iso = opts.get('iso')
                if iso and ip_iso in iso:
                    redirect_code = code
                    break

        if current_code != redirect_code:
            request.disable_autoredirect = True
            language = options.MULTILANGUAGE_SITES.get(redirect_code)
            if language:
                redirect_url = noredirect_url(language['url'], forced_path=request.path)
                return redirect(redirect_url)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号