<commit_before>from django.shortcuts import render, redirect
from django.urls import reverse
from django.utils.http import is_safe_url


def get_next(request):
    # This is not a view
    return request.GET.get("next", request.META.get("HTTP_REFERER", "/"))


def render_code(code, request, context={}):
    return render(request, "%d.html" % code, context,
            status=code)


def render_400(request):
    return render_code(400, request)


def render_405(request):
    return render_code(405, request)


def render_403(request, message=''):
    return render_code(403, request,
            context={"message": message})


def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[-1].strip()
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip


def get_redirect_url(request, default='/', next=None):
    if next is None:
        next = request.POST.get('next',
            request.GET.get('next', request.session.get('next')))
        if 'next' in request.session:
            del request.session['next']
    if not is_safe_url(url=next, host=request.get_host()):
        next = None
    if next is None and default is not None:
        if not default.startswith('/'):
            default = reverse(default)
        next = default
    if next is None or not is_safe_url(url=next, host=request.get_host()):
        next = request.META.get('HTTP_REFERER')
    if next is None or not is_safe_url(url=next, host=request.get_host()):
        next = '/'
    return next


def get_redirect(request, **kwargs):
    url = get_redirect_url(request, **kwargs)
    return redirect(url)
<commit_msg>Fall back to pk_keyword for redirects<commit_after>from django.shortcuts import render, redirect
from django.urls import reverse
from django.utils.http import is_safe_url


def get_next(request):
    # This is not a view
    return request.GET.get("next", request.META.get("HTTP_REFERER", "/"))


def render_code(code, request, context={}):
    return render(request, "%d.html" % code, context,
            status=code)


def render_400(request):
    return render_code(400, request)


def render_405(request):
    return render_code(405, request)


def render_403(request, message=''):
    return render_code(403, request,
            context={"message": message})


def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[-1].strip()
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip


def get_redirect_url(request, default='/', next=None):
    if next is None:
        next = request.POST.get('next',
            request.GET.get('next', request.session.get('next')))
        if 'next' in request.session:
            del request.session['next']
    if next is None:
        keyword = request.GET.get('pk_keyword')
        if keyword.startswith('/'):
            next = keyword
    if not is_safe_url(url=next, host=request.get_host()):
        next = None
    if next is None and default is not None:
        if not default.startswith('/'):
            default = reverse(default)
        next = default
    if next is None or not is_safe_url(url=next, host=request.get_host()):
        next = request.META.get('HTTP_REFERER')
    if next is None or not is_safe_url(url=next, host=request.get_host()):
        next = '/'
    return next


def get_redirect(request, **kwargs):
    url = get_redirect_url(request, **kwargs)
    return redirect(url)

{'fraction_non_alphanumeric': 0.12465064281721633, 'fraction_numerical': 0.010620458356623811, 'mean_word_length': 3.18107476635514, 'pattern_counts': {'":': 2, '<': 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': '5072824', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1123, 'n_words': 281}