Exemplo n.º 1
0
def generate_copo_form(component=str(), target_id=str(), component_dict=dict(), message_dict=dict(), profile_id=str(),
                       **kwargs):
    # message_dict templates are defined in the lookup dictionary: "MESSAGES_LKUPS"

    label_dict = get_labels()

    da_object = DAComponent(component=component, profile_id=profile_id)

    if component in da_dict:
        da_object = da_dict[component](profile_id)

    form_value = component_dict

    # get record, if in edit mode
    if target_id:
        form_value = da_object.get_record(target_id)

    form_schema = list()

    # get schema fields
    for f in da_object.get_component_schema(**kwargs):
        if f.get("show_in_form", True):

            # if required, resolve data source for select-type controls,
            # i.e., if a callback is defined on the 'option_values' field
            if "option_values" in f or f.get("control", "text") in ["copo-lookup", "copo-lookup2"]:
                f['data'] = form_value.get(f["id"].split(".")[-1], str())
                f["option_values"] = get_control_options(f)

            # resolve values for unique items...
            # if a list of unique items is provided with the schema, use it, else dynamically
            # generate unique items based on the component records
            if "unique" in f and not f.get("unique_items", list()):
                f["unique_items"] = generate_unique_items(component=component, profile_id=profile_id,
                                                          elem_id=f["id"].split(".")[-1], record_id=target_id, **kwargs)

            # filter based on sample type
            if component == "sample" and not filter_sample_type(form_value, f):
                continue

            form_schema.append(f)

    if form_value:
        form_value["_id"] = str(target_id)
    else:
        form_value = str()

    return dict(component_name=component,
                form_label=label_dict.get(component, dict()).get("label", str()),
                form_value=form_value,
                target_id=target_id,
                form_schema=form_schema,
                form_message=message_dict,
                )
Exemplo n.º 2
0
def generate_component_records(component=str(), profile_id=str(), label_key=str(), **kwargs):
    da_object = DAComponent(component=component, profile_id=profile_id)

    if component in da_dict:
        da_object = da_dict[component](profile_id)

    component_records = list()
    schema = da_object.get_component_schema(**kwargs)

    # if label_key is not provided, we will assume the first element in the schema to be the label_key

    if not label_key:
        label_key = schema[0]["id"].split(".")[-1] if schema else ''

    for record in da_object.get_all_records(**kwargs):
        option = dict(value=str(record["_id"]), label=record.get(label_key, "N/A"))
        component_records.append(option)

    return component_records