<commit_before>"""Middleware used by django-watson."""

from __future__ import unicode_literals

from watson.registration import search_context_manager


WATSON_MIDDLEWARE_FLAG = "watson.search_context_middleware_active"


class SearchContextMiddleware(object):
    
    """Wraps the entire request in a search context."""
    
    def process_request(self, request):
        """Starts a new search context."""
        request.META[(WATSON_MIDDLEWARE_FLAG, self)] = True
        search_context_manager.start()
    
    def _close_search_context(self, request):
        """Closes the search context."""
        if request.META.get((WATSON_MIDDLEWARE_FLAG, self), False):
            del request.META[(WATSON_MIDDLEWARE_FLAG, self)]
            search_context_manager.end()
    
    def process_response(self, request, response):
        """Closes the search context."""
        self._close_search_context(request)
        return response
        
    def process_exception(self, request, exception):
        """Closes the search context."""
        search_context_manager.invalidate()    
        self._close_search_context(request)<commit_msg>No longer using tuple for WATSON_MIDDLEWARE_FLAG<commit_after>"""Middleware used by django-watson."""

from __future__ import unicode_literals

from django.core.exceptions import ImproperlyConfigured

from watson.registration import search_context_manager


WATSON_MIDDLEWARE_FLAG = "watson.search_context_middleware_active"


class SearchContextMiddleware(object):

    """Wraps the entire request in a search context."""

    def process_request(self, request):
        """Starts a new search context."""
        if request.META.get(WATSON_MIDDLEWARE_FLAG, False):
            raise ImproperlyConfigured("SearchContextMiddleware can only be included in MIDDLEWARE_CLASSES once.")
        request.META[WATSON_MIDDLEWARE_FLAG] = True
        search_context_manager.start()

    def _close_search_context(self, request):
        """Closes the search context."""
        if request.META.get(WATSON_MIDDLEWARE_FLAG, False):
            del request.META[WATSON_MIDDLEWARE_FLAG]
            search_context_manager.end()

    def process_response(self, request, response):
        """Closes the search context."""
        self._close_search_context(request)
        return response

    def process_exception(self, request, exception):
        """Closes the search context."""
        search_context_manager.invalidate()
        self._close_search_context(request)
{'fraction_non_alphanumeric': 0.08876449420231908, 'fraction_numerical': 0.0, 'mean_word_length': 3.6247689463955637, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5686078', 'n_tokens_mistral': 722, 'n_tokens_neox': 703, 'n_words': 161}