<commit_before>from rdrf.hooking import hook

from logging import getLogger

logger = getLogger("registry_log")


@hook("patient_created_from_relative")
def mark_as_relative_in_clinical_form(patient):
    # Ensure that a patient created from a relative is marked as a relative in the clinical form
    if patient.in_registry('fh'):
        patient.set_form_value(
            "fh", "ClinicalData", "fhDateSection", "CDEIndexOrRelative", "fh_is_relative")


@hook("registry_added")
def mark_created_patient_as_index(patient, registry_ids):
    from rdrf.models import Registry
    fh = Registry.objects.get(code="fh")
    if fh.has_feature('family_linkage') and fh.pk in registry_ids:
        logger.debug("marking patient %s as index in FH mongo db" % patient)
        patient.set_form_value(
            "fh", "ClinicalData", "fhDateSection", "CDEIndexOrRelative", "fh_is_index")
    else:
        logger.debug("registry changed but no FH marking made for %s" % patient)<commit_msg>RDR-1042 marking patient as an index in the clinical form only occurs once on patient creation<commit_after>from rdrf.hooking import hook

from logging import getLogger

logger = getLogger("registry_log")


@hook("patient_created_from_relative")
def mark_as_relative_in_clinical_form(patient):
    # Ensure that a patient created from a relative is marked as a relative in the clinical form
    if patient.in_registry('fh'):
        patient.set_form_value("fh", "ClinicalData", "fhDateSection", "CDEIndexOrRelative", "fh_is_relative")


@hook("registry_added")
def mark_created_patient_as_index(patient, registry_ids):
    from rdrf.models import Registry
    from rdrf.dynamic_data import DynamicDataWrapper

    def has_no_mongo_data(patient):
        wrapper = DynamicDataWrapper(patient)
        data = wrapper.load_dynamic_data('fh', 'cdes')
        return data is None

    fh = Registry.objects.get(code="fh")

    if fh.has_feature('family_linkage') and fh.pk in registry_ids and has_no_mongo_data(patient):
        # patient has just been added to fh
        logger.debug("fh registry added hook running setting to index")
        patient.set_form_value("fh", "ClinicalData", "fhDateSection", "CDEIndexOrRelative", "fh_is_index")
{'fraction_non_alphanumeric': 0.09275101305718145, 'fraction_numerical': 0.0018009905447996398, 'mean_word_length': 4.568922305764411, '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': '4982349', 'n_tokens_mistral': 704, 'n_tokens_neox': 653, 'n_words': 185}