<commit_before>"""
Third-party application inclusion support.
"""

from email.utils import parsedate
from time import mktime
import re

from django.core import urlresolvers
from django.core.urlresolvers import Resolver404, resolve, reverse, NoReverseMatch
from django.db import models
from django.db.models import signals
from django.http import HttpResponse
from django.utils.functional import curry as partial, lazy, wraps
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from feincms import settings
from feincms.admin.item_editor import ItemEditorForm
from feincms.contrib.fields import JSONField
from feincms.utils import get_object

try:
    from threading import local
except ImportError:
    from django.utils._threading_local import local

# Used to store MPTT informations about the currently requested
# page. The information will be used to find the best application
# content instance if a particular application has been added
# more than once to the current website.
# Additionally, we store the page class too, because when we have
# more than one page class, reverse() will want to prefer the page
# class used to render the current page. (See issue #240)
_local = local()


def retrieve_page_information(page, request=None):
    """This is the request processor responsible for retrieving information
    about the currently processed page so that we can make an optimal match
    when reversing app URLs when the same ApplicationContent has been added
    several times to the website."""
    _local.proximity_info = (page.tree_id, page.lft, page.rght, page.level)
    _local.page_class = page.__class__
    _local.page_cache_key_fn = page.cache_key


def _empty_reverse_cache(*args, **kwargs):
    _local.reverse_cache = {}


def app_reverse(viewname, urlconf, args=None, kwargs=None, prefix=None,
        *vargs, **vkwargs):
    """
    Reverse URLs from application contents

    Works almost like Django's own reverse() method except that it resolves
    URLs from application contents. The second argument, ``urlconf``, has to
    correspond to the URLconf parameter passed in the ``APPLICATIONS`` list
    to ``Page.create_content_type``::

        app_reverse('mymodel-detail', 'myapp.urls', args=...)

        or

        app_reverse('mymodel-detail', 'myapp.urls', kwargs=...)

    The second argument may also be a request object if you want to reverse
    an URL belonging to the current application content.<commit_msg>Make urlconf arg of app_reverse a kwarg, so that one can curry the app_reverse into a reverse instead of dribbling changes all over the external application.<commit_after>"""
Third-party application inclusion support.
"""

from email.utils import parsedate
from time import mktime
import re

from django.core import urlresolvers
from django.core.urlresolvers import Resolver404, resolve, reverse, NoReverseMatch
from django.db import models
from django.db.models import signals
from django.http import HttpResponse
from django.utils.functional import curry as partial, lazy, wraps
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from feincms import settings
from feincms.admin.item_editor import ItemEditorForm
from feincms.contrib.fields import JSONField
from feincms.utils import get_object

try:
    from threading import local
except ImportError:
    from django.utils._threading_local import local

# Used to store MPTT informations about the currently requested
# page. The information will be used to find the best application
# content instance if a particular application has been added
# more than once to the current website.
# Additionally, we store the page class too, because when we have
# more than one page class, reverse() will want to prefer the page
# class used to render the current page. (See issue #240)
_local = local()


def retrieve_page_information(page, request=None):
    """This is the request processor responsible for retrieving information
    about the currently processed page so that we can make an optimal match
    when reversing app URLs when the same ApplicationContent has been added
    several times to the website."""
    _local.proximity_info = (page.tree_id, page.lft, page.rght, page.level)
    _local.page_class = page.__class__
    _local.page_cache_key_fn = page.cache_key


def _empty_reverse_cache(*args, **kwargs):
    _local.reverse_cache = {}


def app_reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None,
        *vargs, **vkwargs):
    """
    Reverse URLs from application contents

    Works almost like Django's own reverse() method except that it resolves
    URLs from application contents. The second argument, ``urlconf``, has to
    correspond to the URLconf parameter passed in the ``APPLICATIONS`` list
    to ``Page.create_content_type``::

        app_reverse('mymodel-detail', 'myapp.urls', args=...)

        or

        app_reverse('mymodel-detail', 'myapp.urls', kwargs=...)

    The second argument may also be a request object if you want to reverse
    an URL belonging to the current application content.
{'fraction_non_alphanumeric': 0.06573589296102385, 'fraction_numerical': 0.002326934264107039, 'mean_word_length': 4.36732570239334, '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': '5434131', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1349, 'n_words': 588}