<commit_before># coding: utf-8

from .html import TermsHTMLReconstructor
from django.core.urlresolvers import resolve, Resolver404
from .settings import TERMS_IGNORED_APPS
from django.conf import settings


class TermsMiddleware:
    def __init__(self):
        self.parser = TermsHTMLReconstructor()

    def process_response(self, request, response):
        url = request.path
        try:
            app_name = resolve(url).app_name
            app_ignored = app_name in TERMS_IGNORED_APPS
        except Resolver404:
            if settings.DEBUG:
                raise Resolver404("could not find whether the application of "
                                  "'%s' is in TERMS_IGNORED_APPS" % url)
            app_ignored = True

        is_html = 'text/html' in response['Content-Type']

        if not app_ignored and is_html:
            try:
                self.parser.feed(response.content.decode('utf-8'))
                response.content = self.parser.out
            finally:
                self.parser.reset()

        return response<commit_msg>Disables the middleware on pages with a status different from 200.<commit_after># coding: utf-8

from .html import TermsHTMLReconstructor
from django.core.urlresolvers import resolve, Resolver404
from .settings import TERMS_IGNORED_APPS
from django.conf import settings


class TermsMiddleware:
    def __init__(self):
        self.parser = TermsHTMLReconstructor()

    def process_response(self, request, response):
        url = request.path
        try:
            app_name = resolve(url).app_name
            app_ignored = app_name in TERMS_IGNORED_APPS
        except Resolver404:
            if settings.DEBUG:
                raise Resolver404("could not find whether the application of "
                                  "'%s' is in TERMS_IGNORED_APPS" % url)
            app_ignored = True

        is_html = 'text/html' in response['Content-Type']

        if not app_ignored and is_html and response.status_code == 200:
            try:
                self.parser.feed(response.content.decode('utf-8'))
                response.content = self.parser.out
            finally:
                self.parser.reset()

        return response
{'fraction_non_alphanumeric': 0.07223476297968397, 'fraction_numerical': 0.012641083521444696, 'mean_word_length': 2.1255289139633287, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5353848', 'n_tokens_mistral': 629, 'n_tokens_neox': 588, 'n_words': 162}