Skip to content

Commit

Permalink
opt actions update/add-record single/multi-select options
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexCXC committed Dec 16, 2024
1 parent a1b001d commit 9f08440
Showing 1 changed file with 42 additions and 79 deletions.
121 changes: 42 additions & 79 deletions dtable_events/automations/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,44 @@ def handle_file_path(self, dtable_uuid, repo_id, file_path):
url = gen_file_get_url(token, asset_name)
return asset_name, url

def add_or_create_options(self, table_name, column, value):
column_data = column.get('data') or {}

select_options = column_data.get('options') or []
for option in select_options:
if value == option.get('name'):
return value
self.auto_rule.dtable_server_api.add_column_options(
table_name,
column['name'],
options = [gen_random_option(value)]
)
self.auto_rule.cache_clean()

return value

def add_or_create_options_for_multiple_select(self, table_name, column, value):
column_data = column.get('data') or {}
select_options = column_data.get('options') or []

existing_names = set()
for op in select_options:
name = op.get('name')
if name and isinstance(name, str):
existing_names.add(name)

to_be_added_options = [name for name in value if name not in existing_names]

if to_be_added_options:
self.auto_rule.dtable_server_api.add_column_options(
table_name,
column['name'],
options = [gen_random_option(option) for option in to_be_added_options]
)
self.auto_rule.cache_clean()

return value


class UpdateAction(BaseAction):

Expand Down Expand Up @@ -323,46 +361,6 @@ def __init__(self, auto_rule, action_type, data, updates):
self.col_name_dict = {}
self.init_updates()

def add_or_create_options(self, column, value):
table_name = self.update_data['table_name']
column_data = column.get('data') or {}

select_options = column_data.get('options') or []
for option in select_options:
if value == option.get('name'):
return value
self.auto_rule.dtable_server_api.add_column_options(
table_name,
column['name'],
options = [gen_random_option(value)]
)
self.auto_rule.cache_clean()

return value

def add_or_create_options_for_multiple_select(self, column, value):
table_name = self.update_data['table_name']
column_data = column.get('data') or {}
select_options = column_data.get('options') or []

existing_names = set()
for op in select_options:
name = op.get('name')
if name and isinstance(name, str):
existing_names.add(name)

to_be_added_options = [name for name in value if name not in existing_names]

if to_be_added_options:
self.auto_rule.dtable_server_api.add_column_options(
table_name,
column['name'],
options = [gen_random_option(option) for option in to_be_added_options]
)
self.auto_rule.cache_clean()

return value

def format_time_by_offset(self, offset, format_length):
cur_datetime = datetime.now()
cur_datetime_offset = cur_datetime + timedelta(days=offset)
Expand Down Expand Up @@ -426,9 +424,9 @@ def format_update_datas(self, converted_row, row, fill_msg_blank_func):
value = src_row.get(src_col['name'])
if value:
if col_type == ColumnTypes.SINGLE_SELECT:
filtered_updates[col_name] = self.add_or_create_options(col, value)
filtered_updates[col_name] = self.add_or_create_options(self.auto_rule.table_info['name'], col, value)
else:
filtered_updates[col_name] = self.add_or_create_options_for_multiple_select(col, value)
filtered_updates[col_name] = self.add_or_create_options_for_multiple_select(self.auto_rule.table_info['name'], col, value)
elif set_type == 'set_empty':
filtered_updates[col_name] = None
else:
Expand Down Expand Up @@ -2234,41 +2232,6 @@ def format_time_by_offset(self, offset, format_length):
if format_length == 1:
return cur_datetime_offset.strftime("%Y-%m-%d")

def add_or_create_options(self, column, value):
table_name = self.row_data['table_name']
column_data = column.get('data') or {}
select_options = column_data.get('options') or []
for option in select_options:
if value == option.get('name'):
return value
self.auto_rule.dtable_server_api.add_column_options(
table_name,
column['name'],
options = [gen_random_option(value)]
)
return value

def add_or_create_options_for_multiple_select(self, column, value):
table_name = self.row_data['table_name']
column_data = column.get('data') or {}
select_options = column_data.get('options') or []

existing_names = set()
for op in select_options:
name = op.get('name')
if name and isinstance(name, str):
existing_names.add(name)

to_be_added_options = [name for name in value if name not in existing_names]

if to_be_added_options:
self.auto_rule.dtable_server_api.add_column_options(
table_name,
column['name'],
options = [gen_random_option(option) for option in to_be_added_options]
)
return value

def init_append_rows(self):
sql_row = self.auto_rule.get_sql_row()
src_row = self.data['converted_row']
Expand Down Expand Up @@ -2333,9 +2296,9 @@ def init_append_rows(self):
value = src_row.get(src_col['name'])
if value:
if col_type == ColumnTypes.SINGLE_SELECT:
filtered_updates[col_name] = self.add_or_create_options(col, value)
filtered_updates[col_name] = self.add_or_create_options(self.get_table_name(self.dst_table_id), col, value)
else:
filtered_updates[col_name] = self.add_or_create_options_for_multiple_select(col, value)
filtered_updates[col_name] = self.add_or_create_options_for_multiple_select(self.get_table_name(self.dst_table_id), col, value)
else:
value = data_dict # compatible with the old data strcture
filtered_updates[col_name] = self.parse_column_value(col, value)
Expand Down

0 comments on commit 9f08440

Please sign in to comment.