<commit_before>import re

from django.db import models
from django.http import Http404
from django.template import Template
from django.utils.cache import add_never_cache_headers
from django.utils.datastructures import SortedDict
from django.views.generic import TemplateView

from feincms import settings


class ContentMixin(object):
    """
    Mixin for ``feincms.models.Base`` subclasses which need need some degree of
    additional control over the request-response cycle.
    """

    #: Collection of request processors
    request_processors = SortedDict()

    #: Collection of response processors
    response_processors = SortedDict()

    def setup_request(self, request):
        import warnings
        warnings.warn(
            '%s.setup_request does nothing anymore, and will be removed in'
            ' FeinCMS v1.8',
            DeprecationWarning, stacklevel=2)

    @classmethod
    def register_request_processor(cls, fn, key=None):
        """
        Registers the passed callable as request processor. A request processor
        always receives two arguments, the current object and the request.
        """
        cls.request_processors[fn if key is None else key] = fn

    @classmethod
    def register_response_processor(cls, fn, key=None):
        """
        Registers the passed callable as response processor. A response
        processor always receives three arguments, the current object, the
        request and the response.
        """
        cls.response_processors[fn if key is None else key] = fn


class ContentView(TemplateView):
    #: The name of the object for the template rendering context
    context_object_name = 'feincms_object'

    def handle_object(self, object):
        if not hasattr(self.request, '_feincms_extra_context'):
            self.request._feincms_extra_context = {}

        self.object = object

        r = self.run_request_processors()
        if r:
            return r

        r = self.process_content_types()
        if r:
            return r

        response = self.render_to_response(self.get_context_data())

        r = self.finalize_content_types(response)
        if r:
            return r

        r = self.run_response_processors(response)
        if r:
            return r

        return response

    def get_template_names(self):
        # According to the documentation this method is supposed to return
        # a list. However, we can also return a Template instance...
        if isinstance(self.template_name, (Template, list, tuple)):
            return self.template_name

        if self.template_name:
            return [self.template_name]

        # Hopefully someone run register_templates on the object class
        # beforehand, otherwise we'll crash...
        return [self.object.template.path]

    def get_context_data(self, **kwargs):
        context = self.request._feincms_extra_context
        context[self.context_object_name] = self.object
        return context

    @property
    def __name__(self):
        """
        Dummy property to make this handler behave like a normal function.
        This property is used by django-debug-toolbar
        """
        return self.__class__.__name__

    def run_request_processors(self):
        """
        Before rendering an object, run all registered request processors. A
        request processor may peruse and modify the page or the request. It can
        also return a ``HttpResponse`` for shortcutting the rendering and
        returning that response immediately to the client.
        """

        self.request._feincms_extra_context.update({
            # XXX This variable name isn't accurate anymore.
            # We _are_ in a subpage, but it isn't necessarily
            # an appcontent subpage.
            'in_appcontent_subpage': False,
            'extra_path': '/',
            })

        url = self.object.get_absolute_url()
        if self.request.path != url:
            # extra_path must not end with a slash
            self.request._feincms_extra_context.update({
                'in_appcontent_subpage': True,
                'extra_path': re.sub('^' + re.escape(url.rstrip('/')), '',
                    self.request.path),
                })

        for fn in reversed(self.object.request_processors.values()):
            r = fn(self.object, self.request)
            if r:
                return r

    def run_response_processors(self, response):
        """
        After rendering an object to a response, the registered response
        processors are called to modify the response, eg. for setting cache or
        expiration headers, keeping statistics, etc.
        """
        for fn in self.object.response_processors.values():<commit_msg>Move _feincms_extra_context handling into handle object<commit_after>import re

from django.db import models
from django.http import Http404
from django.template import Template
from django.utils.cache import add_never_cache_headers
from django.utils.datastructures import SortedDict
from django.views.generic import TemplateView

from feincms import settings


class ContentMixin(object):
    """
    Mixin for ``feincms.models.Base`` subclasses which need need some degree of
    additional control over the request-response cycle.
    """

    #: Collection of request processors
    request_processors = SortedDict()

    #: Collection of response processors
    response_processors = SortedDict()

    def setup_request(self, request):
        import warnings
        warnings.warn(
            '%s.setup_request does nothing anymore, and will be removed in'
            ' FeinCMS v1.8',
            DeprecationWarning, stacklevel=2)

    @classmethod
    def register_request_processor(cls, fn, key=None):
        """
        Registers the passed callable as request processor. A request processor
        always receives two arguments, the current object and the request.
        """
        cls.request_processors[fn if key is None else key] = fn

    @classmethod
    def register_response_processor(cls, fn, key=None):
        """
        Registers the passed callable as response processor. A response
        processor always receives three arguments, the current object, the
        request and the response.
        """
        cls.response_processors[fn if key is None else key] = fn


class ContentView(TemplateView):
    #: The name of the object for the template rendering context
    context_object_name = 'feincms_object'

    def handle_object(self, object):
        self.object = object

        if not hasattr(self.request, '_feincms_extra_context'):
            self.request._feincms_extra_context = {}

        self.request._feincms_extra_context.update({
            # XXX This variable name isn't accurate anymore.
            'in_appcontent_subpage': False,
            'extra_path': '/',
            })

        url = self.object.get_absolute_url()
        if self.request.path != url:
            self.request._feincms_extra_context.update({
                'in_appcontent_subpage': True,
                'extra_path': re.sub('^' + re.escape(url.rstrip('/')), '',
                    self.request.path),
                })

        r = self.run_request_processors()
        if r:
            return r

        r = self.process_content_types()
        if r:
            return r

        response = self.render_to_response(self.get_context_data())

        r = self.finalize_content_types(response)
        if r:
            return r

        r = self.run_response_processors(response)
        if r:
            return r

        return response

    def get_template_names(self):
        # According to the documentation this method is supposed to return
        # a list. However, we can also return a Template instance...
        if isinstance(self.template_name, (Template, list, tuple)):
            return self.template_name

        if self.template_name:
            return [self.template_name]

        # Hopefully someone run register_templates on the object class
        # beforehand, otherwise we'll crash...
        return [self.object.template.path]

    def get_context_data(self, **kwargs):
        context = self.request._feincms_extra_context
        context[self.context_object_name] = self.object
        return context

    @property
    def __name__(self):
        """
        Dummy property to make this handler behave like a normal function.
        This property is used by django-debug-toolbar
        """
        return self.__class__.__name__

    def run_request_processors(self):
        """
        Before rendering an object, run all registered request processors. A
        request processor may peruse and modify the page or the request. It can
        also return a ``HttpResponse`` for shortcutting the rendering and
        returning that response immediately to the client.
        """
        for fn in reversed(self.object.request_processors.values()):
            r = fn(self.object, self.request)
            if r:
                return r

    def run_response_processors(self, response):
        """
        After rendering an object to a response, the registered response
        processors are called to modify the response, eg. for setting cache or
        expiration headers, keeping statistics, etc.
        """
        for fn in self.object.response_processors.values():
{'fraction_non_alphanumeric': 0.07269635455415029, 'fraction_numerical': 0.001275374641300882, 'mean_word_length': 2.3607142857142858, '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': '7024209', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2352, 'n_words': 795}