<commit_before>"""
Middleware to restrict the admin by IP address
"""
from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import Http404

class RestrictAdminMiddleware(object):
    """
    Simple middlware to only accept admin requests coming from internal IPs
    based on: http://djangosnippets.org/snippets/2095/
    """

    def process_request(self, request):
        try:
            # Get the admin url
            admin_index = reverse('admin:index')
        except NoReverseMatch:
            # No admin url exists, all set
            return

        # This isn't an admin request, all set
        if not request.path.startswith(admin_index):
            return

        # -------------------------------------------
        # OK, this is a call to the admin
        # Try to get the remote address from headers:
        #   - HTTP_X_REAL_IP or
        #   - REMOTE_ADDR
        # -------------------------------------------
        remote_addr = request.META.get('HTTP_X_REAL_IP',\
                      request.META.get('REMOTE_ADDR', None))

        # -------------------------------------------
        # No remote address, sorry, no access!
        # -------------------------------------------
        if remote_addr is None:
            # We're not even here...
            raise Http404

        #--------------------------------------------------------
        # Is this an INTERNAL_IP address?
        #--------------------------------------------------------
        if remote_addr in settings.INTERNAL_IPS:
            # Yes!  Looking good, looking good.
            return

        #--------------------------------------------------------
        #  Also acceptable if INTERNAL_IP listing is two segments
        #   and those match.
        #   For HU, this is 140.147 and allow internal 10.1 settings
        #--------------------------------------------------------
        remote_addr_pieces = remote_addr.split('.')
        print 'remote_addr_pieces', remote_addr_pieces
        if len(remote_addr_pieces) == 4:
            if '.'.join(remote_addr_pieces[:2]) in settings.INTERNAL_IPS:
                # Yes! 1st two segments match. Looking good, looking good.
                return
            elif '.'.join(remote_addr_pieces[:3]) in settings.INTERNAL_IPS:
                # Yes! 1st three segments match. Looking good, looking good.
                return

        #--------------------------------------------------------
        # You ain't got a thing, if you ain't got that admin
        # (no address match, show a 404)
        #--------------------------------------------------------
        raise Http404<commit_msg>debug stmt for #10 on heroku<commit_after>"""
Middleware to restrict the admin by IP address
"""
from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import Http404

class RestrictAdminMiddleware(object):
    """
    Simple middlware to only accept admin requests coming from internal IPs
    based on: http://djangosnippets.org/snippets/2095/
    """

    def process_request(self, request):
        try:
            # Get the admin url
            admin_index = reverse('admin:index')
        except NoReverseMatch:
            # No admin url exists, all set
            return

        # This isn't an admin request, all set
        if not request.path.startswith(admin_index):
            return

        # -------------------------------------------
        # OK, this is a call to the admin
        # Try to get the remote address from headers:
        #   - HTTP_X_REAL_IP or
        #   - REMOTE_ADDR
        # -------------------------------------------
        remote_addr = request.META.get('HTTP_X_REAL_IP',\
                      request.META.get('REMOTE_ADDR', None))

        # -------------------------------------------
        # No remote address, sorry, no access!
        # -------------------------------------------
        if remote_addr is None:
            # We're not even here...
            raise Http404('remote_addr: %s' % remote_addr)

        #--------------------------------------------------------
        # Is this an INTERNAL_IP address?
        #--------------------------------------------------------
        if remote_addr in settings.INTERNAL_IPS:
            # Yes!  Looking good, looking good.
            return

        #--------------------------------------------------------
        #  Also acceptable if INTERNAL_IP listing is two segments
        #   and those match.
        #   For HU, this is 140.147 and allow internal 10.1 settings
        #--------------------------------------------------------
        remote_addr_pieces = remote_addr.split('.')
        print 'remote_addr_pieces', remote_addr_pieces
        if len(remote_addr_pieces) == 4:
            if '.'.join(remote_addr_pieces[:2]) in settings.INTERNAL_IPS:
                # Yes! 1st two segments match. Looking good, looking good.
                return
            elif '.'.join(remote_addr_pieces[:3]) in settings.INTERNAL_IPS:
                # Yes! 1st three segments match. Looking good, looking good.
                return

        #--------------------------------------------------------
        # You ain't got a thing, if you ain't got that admin
        # (no address match, show a 404)
        #--------------------------------------------------------
        raise Http404('remote_addr: %s' % remote_addr)
{'fraction_non_alphanumeric': 0.24904214559386972, 'fraction_numerical': 0.011311804415252692, 'mean_word_length': 1.1052227342549923, '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': '4106567', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1232, 'n_words': 402}