<commit_before>from django.contrib.auth.models import User
from pyquery import PyQuery as pq


class OverrideUserMiddleware(object):
    """
    Provide a way to quickly test views with a variety of users in the
    same browser session.

    Just set `u` to ``alice``, ``bob``, or ``carla`` in the URL, e.g.::

        http://localhost:8000/iris/?u=carla
    """

    def process_view(self, request, view_func, view_args, view_kwargs):
        override_user = request.GET.get('u')
        if override_user:
            request.user = User.objects.get(username=override_user)

    def process_response(self, request, response):
        """Rewrite relative URLs in http responses to include user override."""
        override_user = request.GET.get('u')
        if override_user:
            if response.status_code == 200:
                doc = pq(response.content, parser='html')
                # Replace hrefs in 'a' tags.
                for a in doc('a'):
                    href = a.attrib.get('href')
                    if href and '://' not in href:
                        delim = '&' if '?' in href else '?'
                        a.attrib['href'] = '{0}{1}u={2}'.format(href, delim, override_user)
                # Replace actions in 'form' tags.
                for form in doc('form'):
                    action = form.attrib.get('action')
                    if action and '://' not in action:
                        delim = '&' if '?' in action else '?'
                        form.attrib['action'] = '{0}{1}u={2}'.format(action, delim, override_user)
                # Add a header.
                header = pq('<div class="override-user"></div>').text(override_user)
                header.prependTo(doc('html'))
                # Add a title.
                title = doc('title')
                title.text('[{0}] {1}'.format(override_user, title.text()))
                response.content = '<!DOCTYPE html>' + doc.__html__()
            elif response.status_code == 302:
                location = response['Location']
                delim = '&' if '?' in location else '?'
                response['Location'] = '{0}{1}u={2}'.format(location, delim, override_user)
        return response<commit_msg>Correct behavior of OverrideUserMiddleware when hashes are present in the URL.<commit_after>from django.contrib.auth.models import User
from pyquery import PyQuery as pq


class OverrideUserMiddleware(object):
    """
    Provide a way to quickly test views with a variety of users in the
    same browser session.

    Just set `u` to ``alice``, ``bob``, or ``carla`` in the URL, e.g.::

        http://localhost:8000/iris/?u=carla
    """

    def process_view(self, request, view_func, view_args, view_kwargs):
        override_user = request.GET.get('u')
        if override_user:
            request.user = User.objects.get(username=override_user)

    def process_response(self, request, response):
        """Rewrite relative URLs in http responses to include user override."""
        override_user = request.GET.get('u')
        if override_user:
            def rewrite_url(url, always=False):
                if always or '://' not in url:
                    hashbits = url.split('#', 1)
                    bits = hashbits.pop(0).split('?', 1)
                    url = bits.pop(0)
                    url += '?u={0}'.format(override_user)
                    if bits:
                        url = '{0}&{1}'.format(url, bits[0])
                    if hashbits:
                        url = '{0}#{1}'.format(url, hashbits[0])
                return url
            if response.status_code == 200:
                doc = pq(response.content, parser='html')
                # Replace hrefs in 'a' tags.
                for a in doc('a'):
                    url = a.attrib.get('href')
                    if url:
                        a.attrib['href'] = rewrite_url(url)
                # Replace actions in 'form' tags.
                for form in doc('form'):
                    url = form.attrib.get('action')
                    if url:
                        form.attrib['action'] = rewrite_url(url)
                # Add a header.
                header = pq('<div class="override-user"></div>').text(override_user)
                header.prependTo(doc('html'))
                # Add a title.
                title = doc('title')
                title.text('[{0}] {1}'.format(override_user, title.text()))
                response.content = '<!DOCTYPE html>' + doc.__html__()
            elif response.status_code == 302:
                response['Location'] = rewrite_url(response['Location'], always=True)
        return response
{'fraction_non_alphanumeric': 0.12967314676351208, 'fraction_numerical': 0.009399700918607135, 'mean_word_length': 1.7268491555037857, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4090587', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1234, 'n_words': 340}