Example #1
0
 def process_response(self, request, response):
     if 'text/html' in response['Content-Type']:
         try:
             response.content = minify_html(response.content.strip())
         except DjangoUnicodeDecodeError:
             pass
     return response
Example #2
0
 def process_response(self, request, response):
     if 'text/html' in response.get('Content-Type',[]):
         try:
             response.content = minify_html(response.content.strip())
         except DjangoUnicodeDecodeError:
             pass
     return response
Example #3
0
    def test_simple_quote(self):
        post = Post(user=self.staff, body='[quote="zeus;%d"]Super quote[/quote]' % self.post.pk, topic=self.topic)
        post.save()

        self.assertHTMLEqual(minify_html(post.body_html), u'<blockquote><div class="quote-author">        Posted by <a class="quote-author-name" href="/users/zeus/">zeus</a><a rel="nofollow" class="quote-message-link" href="/xfoo/{}-etopic#post{}"></a></div><div class="quote-message">        Super quote    </div></blockquote>'.format(self.post.topic_id, self.post.id))

        self.assertEqual(Quote.objects.filter(from_post=post).count(), 1)
Example #4
0
 def process_response(self, request, response):
     if response.has_header('Content-Type') and 'text/html' in response['Content-Type']:
         try:
             response.content = minify_html(response.content.strip())
         except DjangoUnicodeDecodeError:
             pass
     return response
Example #5
0
def _get_notice_html(verb_slug, ctx):
    t = get_notification_template(verb_slug)
    #
    # minify the html (as were using minification middleware)
    # also remove \r\n chars
    #
    return minify_html(t.render(loader.Context(ctx)).strip())
Example #6
0
 def process_response(self, request, response):
     if response.has_header('Content-Type') and 'text/html' in response['Content-Type']:
         try:
             response.content = minify_html(response.content.decode('utf-8').strip())
             response['Content-Length'] = str(len(response.content))
         except DjangoUnicodeDecodeError:
             pass
     return response
Example #7
0
def cache_response(
        request,
        response,
        cache_timeout=CACHE_TIME,
        cookie_name=CACHE_NGINX_DEFAULT_COOKIE,
        page_version_fn=None,
        lookup_identifier=None,
        supplementary_identifier=None
    ):

    """Class based view responses TemplateResponse objects and do not call
    render automatically, you we must trigger this."""
    if type(response) is TemplateResponse and not response.is_rendered:
        response.render()

    """ Minify the HTML outout if set in settings. """
    if CACHE_MINIFY_HTML:
        if 'text/html' in response['Content-Type']:
            try:
                response.content = minify_html(response.content.strip())
            except DjangoUnicodeDecodeError:
                pass

    """Cache this response for the web server to grab next time."""
    # get page version
    if page_version_fn:
        pv = page_version_fn(request)
    else:
        pv = ''
    cache_key = get_cache_key(
        request_host=request.get_host(),
        request_path=request.get_full_path(),
        page_version=pv,
        cookie_name=cookie_name
    )
    logging.info("Cacheing %s %s %s %s with key %s" % (
        request.get_host(), request.get_full_path(), pv, cookie_name, cache_key)
    )

    nginx_cache.set(cache_key, response.content, cache_timeout)

    # Store the version, if any specified.
    if pv:
        response.set_cookie(cookie_name, pv)

    # Add record of cacheing taking place to
    # invalidation lookup table, if appropriate
    if getattr(settings, 'CACHE_NGINX_USE_LOOKUP_TABLE', False):
        if not lookup_identifier:
            # If no identifier specified, use the hostname.
            # If you prefer, you could pass in a Site.id, etc
            lookup_identifier = request.get_host()
        add_key_to_lookup(
            cache_key,
            lookup_identifier,
            supplementary_identifier
        )
def cache_response(
        request,
        response,
        cache_timeout=CACHE_TIME,
        cookie_name=CACHE_NGINX_DEFAULT_COOKIE,
        page_version_fn=None,
        lookup_identifier=None,
        supplementary_identifier=None
    ):

    """Class based view responses TemplateResponse objects and do not call
    render automatically, you we must trigger this."""
    if type(response) is TemplateResponse and not response.is_rendered:
        response.render()

    """ Minify the HTML outout if set in settings. """
    if CACHE_MINIFY_HTML:
        if 'text/html' in response['Content-Type']:
            try:
                response.content = minify_html(response.content.strip())
            except DjangoUnicodeDecodeError:
                pass

    """Cache this response for the web server to grab next time."""
    # get page version
    if page_version_fn:
        pv = page_version_fn(request)
    else:
        pv = ''
    cache_key = get_cache_key(
        request_host=request.get_host(),
        request_path=request.get_full_path(),
        page_version=pv,
        cookie_name=cookie_name
    )
    logging.info("Cacheing %s %s %s %s with key %s" % (
        request.get_host(), request.get_full_path(), pv, cookie_name, cache_key)
    )

    nginx_cache.set(cache_key, response._get_content(), cache_timeout)

    # Store the version, if any specified.
    if pv:
        response.set_cookie(cookie_name, pv)

    # Add record of cacheing taking place to
    # invalidation lookup table, if appropriate
    if getattr(settings, 'CACHE_NGINX_USE_LOOKUP_TABLE', False):
        if not lookup_identifier:
            # If no identifier specified, use the hostname.
            # If you prefer, you could pass in a Site.id, etc
            lookup_identifier = request.get_host()
        add_key_to_lookup(
            cache_key,
            lookup_identifier,
            supplementary_identifier
        )
 def process_response(self, request, response):
     # Originally the code did not check whether the header was present;
     # the assumption was that every HTTP request would include it
     # This is not necessarily the case as certain requests naturally don't
     # carry a payload, such as DELETE; this failed in those cases
     if 'Content-Type' in response and 'text/html' in response['Content-Type']:
         try:
             response.content = minify_html(response.content.strip())
         except DjangoUnicodeDecodeError:
             pass
     return response
Example #10
0
    def test_simple_quote(self):
        post = Post(user=self.staff,
                    body='[quote="zeus;%d"]Super quote[/quote]' % self.post.pk,
                    topic=self.topic)
        post.save()

        self.assertHTMLEqual(
            minify_html(post.body_html),
            u'<blockquote><div class="quote-author">        Posted by <a class="quote-author-name" href="/users/zeus/">zeus</a><a rel="nofollow" class="quote-message-link" href="/xfoo/{}-etopic#post{}"></a></div><div class="quote-message">        Super quote    </div></blockquote>'
            .format(self.post.topic_id, self.post.id))

        self.assertEqual(Quote.objects.filter(from_post=post).count(), 1)
Example #11
0
    def __call__(self, request):
        response = self.get_response(request)

        if response.has_header('Content-Type') and 'text/html' in response['Content-Type']:

            try:
                response.content = minify_html(response.content.decode('utf-8', errors='ignore').strip())
                response['Content-Length'] = str(len(response.content))

            except DjangoUnicodeDecodeError:
                pass

        return response
Example #12
0
    def test_double_quote(self):
        post_1 = Post(user=self.staff, body='[quote="%s;%d"]%s[/quote]' % (self.post.user.username,
                                                                         self.post.pk,
                                                                         self.post.body),
                    topic=self.topic)
        post_1.save()

        post_2 = Post(user=self.superuser, body='[quote="%s;%d"]%s[/quote]' % (post_1.user.username,
                                                                             post_1.pk,
                                                                             post_1.body), topic=self.topic)
        post_2.save()
        self.assertHTMLEqual(minify_html(post_2.body_html), u'<blockquote><div class="quote-author">        Posted by <a class="quote-author-name" href="/users/thoas/">thoas</a><a rel="nofollow" class="quote-message-link" href="/xfoo/{}-etopic#post{}"></a></div><div class="quote-message"><blockquote><div class="quote-author">        Posted by <a class="quote-author-name" href="/users/zeus/">zeus</a><a rel="nofollow" class="quote-message-link" href="/xfoo/{}-etopic#post{}"></a></div><div class="quote-message">        bbcode <strong>test</strong></div></blockquote></div></blockquote>'.format(post_1.topic_id, post_1.id, self.post.topic_id, self.post.id))

        self.assertEqual(Quote.objects.filter(from_post=post_2).count(), 2)
Example #13
0
    def __call__(self, request):
        response = self.get_response(request)

        if (response.has_header("Content-Type")
                and "text/html" in response["Content-Type"]):

            try:
                response.content = minify_html(
                    response.content.decode("utf-8").strip())
                response["Content-Length"] = str(len(response.content))

            except DjangoUnicodeDecodeError:
                pass

        return response
Example #14
0
    def __call__(self, request):
        response = self.get_response(request)

        if (response.has_header("Content-Type")
                and "text/html" in response["Content-Type"]):
            try:
                match = re.search(r"charset=([^;\s]+)",
                                  response["Content-Type"])
                encoding = match.group(1) if match else "utf-8"

                response.content = str(
                    minify_html(response.content.strip().decode(encoding)))
                response["Content-Length"] = str(len(response.content))
            except DjangoUnicodeDecodeError:
                pass

        return response
Example #15
0
    def test_double_quote(self):
        post_1 = Post(user=self.staff,
                      body='[quote="%s;%d"]%s[/quote]' %
                      (self.post.user.username, self.post.pk, self.post.body),
                      topic=self.topic)
        post_1.save()

        post_2 = Post(user=self.superuser,
                      body='[quote="%s;%d"]%s[/quote]' %
                      (post_1.user.username, post_1.pk, post_1.body),
                      topic=self.topic)
        post_2.save()
        self.assertHTMLEqual(
            minify_html(post_2.body_html),
            u'<blockquote><div class="quote-author">        Posted by <a class="quote-author-name" href="/users/thoas/">thoas</a><a rel="nofollow" class="quote-message-link" href="/xfoo/{}-etopic#post{}"></a></div><div class="quote-message"><blockquote><div class="quote-author">        Posted by <a class="quote-author-name" href="/users/zeus/">zeus</a><a rel="nofollow" class="quote-message-link" href="/xfoo/{}-etopic#post{}"></a></div><div class="quote-message">        bbcode <strong>test</strong></div></blockquote></div></blockquote>'
            .format(post_1.topic_id, post_1.id, self.post.topic_id,
                    self.post.id))

        self.assertEqual(Quote.objects.filter(from_post=post_2).count(), 2)
Example #16
0
 def process_response(self, request, response):
     if response.has_header(
             'Content-Type') and 'text/html' in response['Content-Type']:
         charset = response.charset
         try:
             response.content = force_bytes(
                 minify_html(
                     force_str(
                         response.content.strip(),
                         encoding=charset,
                     )),
                 encoding=charset,
             )
             response['Content-Length'] = len(response.content)
         except DjangoUnicodeDecodeError:
             # For some reason convertion can not be done.
             # Let's ignore the minification and return the response
             # as is.
             pass
     return response
Example #17
0
 def process_response(self, request, response):
     if response.has_header('Content-Type') and 'text/html' in response['Content-Type']:
         charset = response.charset
         try:
             response.content = force_bytes(
                 minify_html(
                     force_str(
                         response.content.strip(),
                         encoding=charset,
                     )
                 ),
                 encoding=charset,
             )
             response['Content-Length'] = len(response.content)
         except DjangoUnicodeDecodeError:
             # For some reason convertion can not be done.
             # Let's ignore the minification and return the response
             # as is.
             pass
     return response
Example #18
0
    def handle(self, *args, **options):
        env = Environment(loader=FileSystemLoader(join(settings.SITE_ROOT, 'templates/')), autoescape=True, trim_blocks=True)
        template = env.get_template('table.html')

        formules = formules_definitoires()

        for f in (glob(join(settings.SITE_ROOT, 'resources/tables-3.4/verbes/*.csv')) +
                glob(join(settings.SITE_ROOT, 'resources/tables-3.4/figees/*.csv'))):

            classe = re.search('([^.]+).lgt.csv', basename(f)).group(1)
            stripped_class = classe[2:] if classe.startswith('V_') else classe

            with open(f) as csvf, open(join(settings.SITE_ROOT, 'assets/verbes-html/{}.lgt.html'.format(classe)), 'w') as html:
                ladlreader = csv.reader(csvf, delimiter=';', quotechar='"')

                context = {
                    'classe': stripped_class,
                    'formule': formules.get(stripped_class),
                    'header': next(ladlreader),
                    'verb_lines': ladlreader,
                    'toalign': ['+', '~', '-', '?', '<E>']
                }

                html.write(minify_html(template.render(context)))
Example #19
0
 def process_response(self, request, response):
     if response.has_header('Content-Type') and 'text/html' in response['Content-Type'] and not getattr(request, '_cache_update_cache', True):
             response.content = minify_html(response.content.strip())
             response['Content-Length'] = str(len(response.content))
         except DjangoUnicodeDecodeError:
             pass