<commit_before>import random
import string
from typing import Any

from django.conf import settings
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt

from confirmation.models import Confirmation, create_confirmation_link
from zerver.context_processors import get_realm_from_request
from zerver.lib.response import json_success
from zerver.models import Realm, UserProfile
from zerver.views.auth import create_preregistration_user
from zerver.views.registration import accounts_register


# This is used only by the Puppeteer test in realm-creation.ts.
def confirmation_key(request: HttpRequest) -> HttpResponse:
    return json_success(request, data=request.session["confirmation_key"])


def modify_postdata(request: HttpRequest, **kwargs: Any) -> None:
    request.POST._mutable = True
    for key, value in kwargs.items():
        request.POST[key] = value
    request.POST._mutable = False


def generate_demo_realm_name() -> str:
    letters = "".join(random.SystemRandom().choice(string.ascii_lowercase) for _ in range(4))
    digits = "".join(random.SystemRandom().choice(string.digits) for _ in range(4))
    demo_realm_name = f"demo-{letters}{digits}"
    return demo_realm_name


@csrf_exempt
def register_development_user(request: HttpRequest) -> HttpResponse:
    realm = get_realm_from_request(request)
    if realm is None:
        return HttpResponseRedirect(
            f"{settings.EXTERNAL_URI_SCHEME}{settings.REALM_HOSTS['zulip']}/devtools/register_user/",
            status=307,
        )

    count = UserProfile.objects.count()
    name = f"user-{count}"
    email = f"{<EMAIL>"
    prereg = create_preregistration_user(
        email, realm, realm_creation=False, password_required=False
    )
    activation_url = create_confirmation_link(prereg, Confirmation.USER_REGISTRATION)
    key = activation_url.split("/")[-1]<commit_msg>registration: Maintain immutability of the POST QueryDict.<commit_after>import random
import string
from typing import TYPE_CHECKING, Any, cast

from django.conf import settings
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt

from confirmation.models import Confirmation, create_confirmation_link
from zerver.context_processors import get_realm_from_request
from zerver.lib.response import json_success
from zerver.models import Realm, UserProfile
from zerver.views.auth import create_preregistration_user
from zerver.views.registration import accounts_register

if TYPE_CHECKING:
    from django.http.request import _ImmutableQueryDict

# This is used only by the Puppeteer test in realm-creation.ts.
def confirmation_key(request: HttpRequest) -> HttpResponse:
    return json_success(request, data=request.session["confirmation_key"])


def modify_postdata(request: HttpRequest, **kwargs: Any) -> None:
    new_post = request.POST.copy()
    for key, value in kwargs.items():
        new_post[key] = value
    new_post._mutable = False
    request.POST = cast("_ImmutableQueryDict", new_post)


def generate_demo_realm_name() -> str:
    letters = "".join(random.SystemRandom().choice(string.ascii_lowercase) for _ in range(4))
    digits = "".join(random.SystemRandom().choice(string.digits) for _ in range(4))
    demo_realm_name = f"demo-{letters}{digits}"
    return demo_realm_name


@csrf_exempt
def register_development_user(request: HttpRequest) -> HttpResponse:
    realm = get_realm_from_request(request)
    if realm is None:
        return HttpResponseRedirect(
            f"{settings.EXTERNAL_URI_SCHEME}{settings.REALM_HOSTS['zulip']}/devtools/register_user/",
            status=307,
        )

    count = UserProfile.objects.count()
    name = f"user-{count}"
    email = f"{<EMAIL>"
    prereg = create_preregistration_user(
        email, realm, realm_creation=False, password_required=False
    )
    activation_url = create_confirmation_link(prereg, Confirmation.USER_REGISTRATION)
    key = activation_url.split("/")[-1]
{'fraction_non_alphanumeric': 0.09173859432799014, 'fraction_numerical': 0.002959309494451295, 'mean_word_length': 4.7860199714693294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '4879474', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1202, 'n_words': 278}