<commit_before>import json
import uuid
from base64 import urlsafe_b64encode

from django.utils.translation import gettext as _

from casexml.apps.phone.xml import get_registration_element_data
from corehq.apps.userreports.specs import EvaluationContext
from corehq.motech.generic_inbound.exceptions import GenericInboundUserError


def make_url_key():
    raw_key = urlsafe_b64encode(uuid.uuid4().bytes).decode()
    return raw_key.removesuffix("==")


def get_context_from_request(request):
    try:
        body = json.loads(request.body.decode('utf-8'))
    except (UnicodeDecodeError, json.JSONDecodeError):
        raise GenericInboundUserError(_("Payload must be valid JSON"))

    couch_user = request.couch_user
    if couch_user.is_commcare_user():
        restore_user = couch_user.to_ota_restore_user(couch_user)
    elif couch_user.is_web_user():
        restore_user = couch_user.to_ota_restore_user(request.domain, couch_user)
    else:
        raise GenericInboundUserError(_("Unknown user type"))

    return get_evaluation_context(
        restore_user,
        request.method,
        request.META['QUERY_STRING'],
        dict(request.headers),
        body
    )


def get_evaluation_context(restore_user, method, query, headers, body):
    return EvaluationContext({
        'request_method': method,
        'query': query,
        'headers': headers,
        'body': body,
        'user': get_registration_element_data(restore_user)
    })<commit_msg>standardize context names<commit_after>import json
import uuid
from base64 import urlsafe_b64encode

from django.utils.translation import gettext as _

from casexml.apps.phone.xml import get_registration_element_data
from corehq.apps.userreports.specs import EvaluationContext
from corehq.motech.generic_inbound.exceptions import GenericInboundUserError


def make_url_key():
    raw_key = urlsafe_b64encode(uuid.uuid4().bytes).decode()
    return raw_key.removesuffix("==")


def get_context_from_request(request):
    try:
        body = json.loads(request.body.decode('utf-8'))
    except (UnicodeDecodeError, json.JSONDecodeError):
        raise GenericInboundUserError(_("Payload must be valid JSON"))

    couch_user = request.couch_user
    if couch_user.is_commcare_user():
        restore_user = couch_user.to_ota_restore_user(couch_user)
    elif couch_user.is_web_user():
        restore_user = couch_user.to_ota_restore_user(request.domain, couch_user)
    else:
        raise GenericInboundUserError(_("Unknown user type"))

    return get_evaluation_context(
        restore_user,
        request.method,
        request.META['QUERY_STRING'],
        dict(request.headers),
        body
    )


def get_evaluation_context(restore_user, method, query, headers, body):
    return EvaluationContext({
        'request_method': method,
        'request_query': query,
        'request_headers': headers,
        'body': body,
        'user': get_registration_element_data(restore_user)
    })
{'fraction_non_alphanumeric': 0.09005376344086022, 'fraction_numerical': 0.005376344086021506, 'mean_word_length': 3.9369817578772803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '395782', 'n_tokens_mistral': 906, 'n_tokens_neox': 889, 'n_words': 174}