<commit_before>from corehq.apps.locations.models import SQLLocation

from custom.enikshay.management.commands.utils import (
    BaseEnikshayCaseMigration,
    get_form_path,
)

DATAMIGRATION_CASE_PROPERTY = 'datamigration_referring_facility'
REFERRING_FACILITY_ID = 'referring_facility_id'
REFERRING_FACILITY_SAVED_NAME = 'referring_facility_saved_name'


class Command(BaseEnikshayCaseMigration):
    case_type = 'test'
    case_properties_to_update = [
        REFERRING_FACILITY_ID,
        REFERRING_FACILITY_SAVED_NAME,
    ]
    datamigration_case_property = DATAMIGRATION_CASE_PROPERTY
    include_public_cases = True
    include_private_cases = False

    @staticmethod
    def get_case_property_updates(test, domain):
        if (
            test.get_case_property(DATAMIGRATION_CASE_PROPERTY) == 'yes'
            or not test.get_case_property(REFERRING_FACILITY_ID)
        ):
            return {}

        form_requesting_test = test.actions[0].form
        referring_facility_id = get_form_path(
            ['ql_referring_facility_details', 'referring_facility_id'],
            form_requesting_test.form_data
        )
        if not referring_facility_id:
            return {}

        update = {
            REFERRING_FACILITY_ID: referring_facility_id,
        }
        try:
            referring_facility_name = SQLLocation.active_objects.get(
                domain=domain,
                location_id=referring_facility_id,<commit_msg>reuse existing function<commit_after>from corehq.apps.locations.models import SQLLocation

from custom.enikshay.management.commands.utils import (
    BaseEnikshayCaseMigration,
    get_form_path,
    get_test_created_form,
)

DATAMIGRATION_CASE_PROPERTY = 'datamigration_referring_facility'
REFERRING_FACILITY_ID = 'referring_facility_id'
REFERRING_FACILITY_SAVED_NAME = 'referring_facility_saved_name'


class Command(BaseEnikshayCaseMigration):
    case_type = 'test'
    case_properties_to_update = [
        REFERRING_FACILITY_ID,
        REFERRING_FACILITY_SAVED_NAME,
    ]
    datamigration_case_property = DATAMIGRATION_CASE_PROPERTY
    include_public_cases = True
    include_private_cases = False

    @staticmethod
    def get_case_property_updates(test, domain):
        if (
            test.get_case_property(DATAMIGRATION_CASE_PROPERTY) == 'yes'
            or not test.get_case_property(REFERRING_FACILITY_ID)
        ):
            return {}

        form_data_requesting_test = get_test_created_form(test)
        referring_facility_id = get_form_path(
            ['ql_referring_facility_details', 'referring_facility_id'],
            form_data_requesting_test
        )
        if not referring_facility_id:
            return {}

        update = {
            REFERRING_FACILITY_ID: referring_facility_id,
        }
        try:
            referring_facility_name = SQLLocation.active_objects.get(
                domain=domain,
                location_id=referring_facility_id,
{'fraction_non_alphanumeric': 0.060687795010114634, 'fraction_numerical': 0.00033715441672285906, 'mean_word_length': 3.115117891816921, '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': '3229574', 'n_tokens_mistral': 951, 'n_tokens_neox': 952, 'n_words': 118}