(core) Add RenameChoices user action

Summary:
["RenameChoices", table_id, col_id, renames]
Updates the data in a Choice/ChoiceList column to reflect the new choice names.
`renames` should be a dict of `{old_choice_name: new_choice_name}`.
This doesn't touch the choices configuration in widgetOptions, that must be done separately.
Frontend to be done in another diff.

Test Plan: Added two Python unit tests.

Reviewers: jarek

Reviewed By: jarek

Differential Revision: https://phab.getgrist.com/D3050
This commit is contained in:
Alex Hall
2021-09-30 14:14:52 +02:00
parent 02fd71d9bb
commit d4ea5b3761
5 changed files with 184 additions and 2 deletions

View File

@@ -12,7 +12,7 @@ from acl_formula import parse_acl_formula_json
import actions
import column
import identifiers
from objtypes import strict_equal
from objtypes import strict_equal, encode_object
import schema
from schema import RecalcWhen
import summary
@@ -1247,6 +1247,25 @@ class UserActions(object):
self.SetDisplayFormula(dst_col.parentId.tableId, None, dst_col.id,
re.sub((r'\$%s\b' % src_col.colId), '$' + dst_col.colId, src_col.displayCol.formula))
@useraction
def RenameChoices(self, table_id, col_id, renames):
"""
Updates the data in a Choice/ChoiceList column to reflect the new choice names.
`renames` should be a dict of {old_choice_name: new_choice_name}.
This doesn't touch the choices configuration in widgetOptions, that must be done separately.
"""
table = self._engine.tables[table_id]
col = table.get_column(col_id)
if col.is_formula():
# We don't set the values of formula columns, they should just recalculate themselves
return None
row_ids, values = col.rename_choices(renames)
values = [encode_object(v) for v in values]
return self.BulkUpdateRecord(table_id, row_ids, {col_id: values})
#----------------------------------------
# User actions on tables.
#----------------------------------------