<commit_before>from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import resolve
from django.shortcuts import redirect

DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS = getattr(
    settings,
    "DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS",
    ()
)

from .models import Customer

# So we don't have crazy long lines of code
EXEMPT = list(DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS)
EXEMPT.append("[djstripe]")


class SubscriptionPaymentMiddleware(object):
    """
    Rules:

        * "(app_name)" means everything from this app is exempt
        * "[namespace]" means everything with this name is exempt
        * "namespace:name" means this namespaced URL is exempt
        * "name" means this URL is exempt
        * The entire djtripe namespace is exempt

    Example::

        DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS = (
            "(allauth)",  # anything in the django-allauth URLConf
            "[blogs]",  # Anything in the blogs namespace
            "products:detail",  # A ProductDetail view you want shown to non-payers
            "home",  # Site homepage
        )
    """

    # TODO - needs tests

    def process_request(self, request):

        if request.user.is_authenticated() and not request.user.is_staff:
            match = resolve(request.path)
            if "({0})".format(match.app_name) in EXEMPT:
                return

            if "[{0}]".format(match.namespace) in EXEMPT:
                return

            if "{0}:{1}".format(match.namespace, match.url_name) in EXEMPT:
                return

            if match.url_name in EXEMPT:
                return

            # TODO: Consider converting to use
            #       djstripe.utils.user_has_active_subscription function
            customer, created = Customer.get_or_create(request.user)
            if created:
                return redirect(settings.DJSTRIPE_REDIRECT_IF_NOT_SUBSCRIBED)

            if not customer.has_active_subscription():
                return redirect(settings.DJSTRIPE_REDIRECT_IF_NOT_SUBSCRIBED)

        # TODO get this working in tests
        # if request.user.is_anonymous():
        #     raise ImproperlyConfigured<commit_msg>Middleware update to ignore media / static queries if no subscription<commit_after>from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import resolve
from django.shortcuts import redirect

DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS = getattr(
    settings,
    "DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS",
    ()
)

from .models import Customer

# So we don't have crazy long lines of code
EXEMPT = list(DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS)
EXEMPT.append("[djstripe]")


class SubscriptionPaymentMiddleware(object):
    """
    Rules:

        * "(app_name)" means everything from this app is exempt
        * "[namespace]" means everything with this name is exempt
        * "namespace:name" means this namespaced URL is exempt
        * "name" means this URL is exempt
        * The entire djtripe namespace is exempt

    Example::

        DJSTRIPE_SUBSCRIPTION_REQUIRED_EXCEPTION_URLS = (
            "(allauth)",  # anything in the django-allauth URLConf
            "[blogs]",  # Anything in the blogs namespace
            "products:detail",  # A ProductDetail view you want shown to non-payers
            "home",  # Site homepage
        )
    """

    # TODO - needs tests

    def process_request(self, request):

        if request.user.is_authenticated() and not request.user.is_staff and not request.path.startswith('/media/') and not request.path.startswith('/static/'):
            match = resolve(request.path)
            if "({0})".format(match.app_name) in EXEMPT:
                return

            if "[{0}]".format(match.namespace) in EXEMPT:
                return

            if "{0}:{1}".format(match.namespace, match.url_name) in EXEMPT:
                return

            if match.url_name in EXEMPT:
                return

            # TODO: Consider converting to use
            #       djstripe.utils.user_has_active_subscription function
            customer, created = Customer.get_or_create(request.user)
            if created:
                return redirect(settings.DJSTRIPE_REDIRECT_IF_NOT_SUBSCRIBED)

            if not customer.has_active_subscription():
                return redirect(settings.DJSTRIPE_REDIRECT_IF_NOT_SUBSCRIBED)

        # TODO get this working in tests
        # if request.user.is_anonymous():
        #     raise ImproperlyConfigured

{'fraction_non_alphanumeric': 0.07783527382492454, 'fraction_numerical': 0.0017248814144027599, 'mean_word_length': 2.5385202135774216, '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': '3587713', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1267, 'n_words': 340}