def process_dynamic_table_config()

in 5-app-infra/3-artifact-publish/docker/cdmc/tag_engine_api/main.py [0:0]


def process_dynamic_table_config():
    
    if 'credentials' not in session:
        return redirect('/')
        
    template_id = request.form['template_id']
    template_project = request.form['template_project']
    template_region = request.form['template_region']
    service_account = request.form['service_account']
    included_tables_uris = request.form['included_tables_uris'].rstrip()
    excluded_tables_uris = request.form['excluded_tables_uris'].rstrip()
    refresh_mode = request.form['refresh_mode']
    refresh_frequency = request.form['refresh_frequency']
    refresh_unit = request.form['refresh_unit']
    action = request.form['action']
    
    credentials, success = get_target_credentials(service_account)
    
    if success == False:
        print('Error acquiring credentials from', service_account)
    
    dcc = controller.DataCatalogController(credentials, None, None, template_id, template_project, template_region)
    template = dcc.get_template()
    
    if action == "Cancel Changes":
        
        return render_template(
            'tag_template.html',
            template_id=template_id,
            template_project=template_project,
            template_region=template_region, 
            service_account=service_account, 
            fields=template)

    fields = []
    selected_fields = request.form.getlist("selected")
    #print("selected_fields: " + str(selected_fields))
    
    for selected_field in selected_fields:
        query_expression = request.form.get(selected_field).replace('\t', '').replace('\r', '').replace('\n', ' ').strip()
        #print("query_expression: " + query_expression)
        selected_field_type = request.form.get(selected_field + "_datatype")
        #print("selected_field_type: " + selected_field_type)
        print(selected_field + ", " + query_expression + ", " + selected_field_type)
        
        for template_field in template:
            
            if template_field['field_id'] != selected_field:
                continue
        
            is_required = template_field['is_required']
            field = {'field_id': selected_field, 'query_expression': query_expression, 'field_type': selected_field_type,\
                     'is_required': is_required}
            fields.append(field)
            break
    
    #print('fields: ' + str(fields))
    
    if excluded_tables_uris == 'None':
        excluded_tables_uris = ''
    
    tag_history_option, _ = store.read_tag_history_settings()
    
    if tag_history_option == True:
        tag_history_display = "ON"
    else:
        tag_history_display = "OFF"
    
    template_uuid = store.write_tag_template(template_id, template_project, template_region)
    config_uuid = store.write_dynamic_table_config(service_account, fields, included_tables_uris, excluded_tables_uris, \
                                                   template_uuid, template_id, template_project, template_region, \
                                                   refresh_mode, refresh_frequency, refresh_unit, tag_history_option)
     
    # [END process_dynamic_table_config]
    # [START render_template]
    return render_template(
        'created_dynamic_table_config.html',
        config_uuid=config_uuid,
        config_type='DYNAMIC_TAG_TABLE',
        template_id=template_id,
        template_project=template_project,
        template_region=template_region,
        service_account=service_account,
        fields=fields,
        included_tables_uris=included_tables_uris,
        excluded_tables_uris=excluded_tables_uris,
        refresh_mode=refresh_mode,
        refresh_frequency=refresh_frequency,
        refresh_unit=refresh_unit,
        tag_history=tag_history_display)