def randomize_messages(case):
    message_list = []
    risk_profiles = case.get_case_property("risk_profiles").upper()

    # Add messages specific to each risk profile
    if PROFILE_A in risk_profiles:
        message_list += get_message_bank(case.domain, risk_profile=PROFILE_A)
    if PROFILE_B in risk_profiles:
        message_list += get_message_bank(case.domain, risk_profile=PROFILE_B)
    if PROFILE_C in risk_profiles:
        message_list += get_message_bank(case.domain, risk_profile=PROFILE_C)
    if PROFILE_D in risk_profiles:
        message_list += get_message_bank(case.domain, risk_profile=PROFILE_D)
    if PROFILE_E in risk_profiles:
        message_list += get_message_bank(case.domain, risk_profile=PROFILE_E)
    if PROFILE_F in risk_profiles:
        message_list += get_message_bank(case.domain, risk_profile=PROFILE_F)

    # Add generic messages to get to 280
    additional_messages_required = 280 - len(message_list)
    if additional_messages_required > 0:
        generic_messages = get_message_bank(case.domain, risk_profile=PROFILE_G)
        random.shuffle(generic_messages)
        for i in range(additional_messages_required):
            message_list.append(generic_messages[i])

    # Randomize the list, and save
    random.shuffle(message_list)
    order = 0
    for message in message_list:
        randomized_message = FRIRandomizedMessage(
            domain = case.domain,
            case_id = case._id,
            message_bank_message_id = message._id,
            order = order,
        )
        randomized_message.save()
        order += 1
Create a randomized list of 280 messages for the case, based on its risk profile.
dataset/ETHPy150Open dimagi/commcare-hq/custom/fri/api.py/randomize_messages