<commit_before>import logging

from urlparse import urljoin

from django.conf import settings
from django.http import HttpResponseRedirect

from ipware.ip import get_ip

logger = logging.getLogger(__name__)


class HttpResponseTemporaryRedirect(HttpResponseRedirect):
    """
    Redirect the request in a way that it is re-POSTed.
    """
    status_code = 307


def get_production_redirect(request):
    """
    Generate an appropriate redirect to production.
    """
    redirect_url = urljoin(settings.PRODUCTION_URL, request.get_full_path())

    logger.warning('Redirecting production client URL "%s" to "%s"',
                   request.get_full_path(), redirect_url)

    return HttpResponseTemporaryRedirect(redirect_url)


class QueryStringAccessTokenToBearerMiddleware:
    """
    django-oauth-toolkit wants access tokens specified using the
    "Authorization: Bearer" header.
    """
    def process_request(self, request):
        if 'access_token' not in request.GET:
            return

        request.META['HTTP_AUTHORIZATION'] = 'Bearer {}'.format(
            request.GET['access_token'])

        # I don't think access_token should be removed but am leaving this here
        # just in case.
        # request.GET = request.GET.copy()
        # del request.GET['access_token']


class RedirectAmericanGutToProductionMiddleware:
    """
    Redirect a request from American Gut to production.
    """
    def process_request(self, request):
        if get_ip(request) != '192.168.3.11':
            return

        return get_production_redirect(request)


class RedirectStagingToProductionMiddleware:
    """
    Redirect a staging URL to production if it contains a production client ID.
    """
    def process_request(self, request):
        if settings.ENV != 'staging':
            return

        if 'client_id' not in request.GET:
            return

        if request.GET['client_id'] not in settings.PRODUCTION_CLIENT_IDS:
            return

        return get_production_redirect(request)
<commit_msg>🐛 stop spurious production redirects<commit_after>import logging

from urlparse import urljoin

from django.conf import settings
from django.http import HttpResponseRedirect

from ipware.ip import get_ip

logger = logging.getLogger(__name__)


class HttpResponseTemporaryRedirect(HttpResponseRedirect):
    """
    Redirect the request in a way that it is re-POSTed.
    """
    status_code = 307


def get_production_redirect(request):
    """
    Generate an appropriate redirect to production.
    """
    redirect_url = urljoin(settings.PRODUCTION_URL, request.get_full_path())

    logger.warning('Redirecting production client URL "%s" to "%s"',
                   request.get_full_path(), redirect_url)

    return HttpResponseTemporaryRedirect(redirect_url)


class QueryStringAccessTokenToBearerMiddleware:
    """
    django-oauth-toolkit wants access tokens specified using the
    "Authorization: Bearer" header.
    """
    def process_request(self, request):
        if 'access_token' not in request.GET:
            return

        request.META['HTTP_AUTHORIZATION'] = 'Bearer {}'.format(
            request.GET['access_token'])

        # I don't think access_token should be removed but am leaving this here
        # just in case.
        # request.GET = request.GET.copy()
        # del request.GET['access_token']


class RedirectAmericanGutToProductionMiddleware:
    """
    Redirect a request from American Gut to production.
    """
    def process_request(self, request):
        if settings.ENV != 'staging':
            return

        if get_ip(request) != '192.168.3.11':
            return

        return get_production_redirect(request)


class RedirectStagingToProductionMiddleware:
    """
    Redirect a staging URL to production if it contains a production client ID.
    """
    def process_request(self, request):
        if settings.ENV != 'staging':
            return

        if 'client_id' not in request.GET:
            return

        if request.GET['client_id'] not in settings.PRODUCTION_CLIENT_IDS:
            return

        return get_production_redirect(request)

{'fraction_non_alphanumeric': 0.08307247772694437, 'fraction_numerical': 0.005778954972309174, 'mean_word_length': 2.9561904761904763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6168764', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1097, 'n_words': 318}