コード例 #1
0
ファイル: middleware.py プロジェクト: alexsilva/Report
    def process_response(self, request, response):
        translation.deactivate()
        path = unicode(request.path)

        if not path.startswith(settings.MEDIA_URL) and \
            not path.startswith(settings.ADMIN_MEDIA_PREFIX) and \
            response.status_code == 200 and \
            response._headers['content-type'][1].split(';')[0] == "text/html":

            response.content = HREF_REGEX.sub(
                ur'<a\1href="/%s/\3"\4>' %
                language_as_slug(request.LANGUAGE_CODE),
                response.content.decode('utf-8'))
            response.content = FORM_REGEX.sub(
                ur'<form\1action="/%s/\3"\4>' %
                language_as_slug(request.LANGUAGE_CODE),
                response.content.decode('utf-8'))

        if (response.status_code == 301 or response.status_code == 302):
            location = response._headers['location']
            prefix = has_lang_prefix(location[1])
            if not prefix and location[1].startswith("/") and \
               not location[1].startswith(settings.MEDIA_URL) and \
               not location[1].startswith(settings.ADMIN_MEDIA_PREFIX):
                response._headers['location'] = (
                    location[0], "/%s%s" %
                    (language_as_slug(request.LANGUAGE_CODE), location[1]))
        return response
コード例 #2
0
ファイル: middleware.py プロジェクト: asavoy/django-easymode
    def process_response(self, request, response):
        translation.deactivate()
        path = unicode(request.path)

        if not path.startswith(settings.MEDIA_URL) and \
            not path.startswith(admin_media_prefix) and \
            response.status_code == 200 and \
            response._headers['content-type'][1].split(';')[0] == "text/html":
                
            response.content = HREF_REGEX.sub(
                ur'<a\1href="/%s/\3"\4>' % language_as_slug(request.LANGUAGE_CODE), 
                response.content.decode('utf-8'))
            response.content = FORM_REGEX.sub(
                ur'<form\1action="/%s/\3"\4>' % language_as_slug(request.LANGUAGE_CODE), 
                response.content.decode('utf-8'))
                
        if (response.status_code == 301 or response.status_code == 302 ):
            location = response._headers['location']
            prefix = has_lang_prefix(location[1])
            if not prefix and location[1].startswith("/") and \
               not location[1].startswith(settings.MEDIA_URL) and \
               not location[1].startswith(admin_media_prefix):
                response._headers['location'] = (
                    location[0], "/%s%s" % (
                        language_as_slug(request.LANGUAGE_CODE), location[1]
                    )
                )
        return response