Skip to content

Commit

Permalink
Handle wrong/unsupported syntax in pred formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
SleepyLeslie committed Jun 15, 2024
1 parent b9ea716 commit 9847b84
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
Binary file removed dump.rdb
Binary file not shown.
11 changes: 9 additions & 2 deletions sandbox/grist/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,15 @@ def renamer(subject):
return new_col_id

new_acl_formula = predicate_formula.process_renames(formula, _ACLEntityCollector(), renamer)
rule_updates.append((rule_rec, {'aclFormula': new_acl_formula,
'aclFormulaParsed': parse_predicate_formula_json(new_acl_formula)}))
new_rule_record = {"aclFormula": new_acl_formula}
try:
new_rule_record["aclFormulaParsed"] = parse_predicate_formula_json(new_acl_formula)
except SyntaxError:
pass
except ValueError as e:
if not str(e).startswith("Unsupported syntax"):
raise e
rule_updates.append((rule_rec, new_rule_record))

useractions.doBulkUpdateFromPairs('_grist_ACLResources', resource_updates)
useractions.doBulkUpdateFromPairs('_grist_ACLRules', rule_updates)
12 changes: 9 additions & 3 deletions sandbox/grist/dropdown_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ def renamer(subject):

new_dc_formula = predicate_formula.process_renames(dc_formula, _DCEntityCollector(), renamer)

# Parse the new dropdown condition formula.
widget_options["dropdownCondition"] = {"text": new_dc_formula,
"parsed": parse_predicate_formula_json(new_dc_formula)}
widget_options["dropdownCondition"] = {"text": new_dc_formula}
try:
# Parse the new dropdown condition formula if it is syntactically correct.
widget_options["dropdownCondition"]["parsed"] = parse_predicate_formula_json(new_dc_formula)
except SyntaxError:
pass
except ValueError as e:
if not str(e).startswith("Unsupported syntax"):
raise e
updates.append((col, {"widgetOptions": json.dumps(widget_options)}))

# Update the dropdown condition in the database.
Expand Down
7 changes: 5 additions & 2 deletions sandbox/grist/predicate_formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def parse_predicate_formula(formula):
result = ['Comment', result, part[1][1:].strip()]
break
return result
except SyntaxError as err:
except SyntaxError as e:
# In case of an error, include line and offset.
raise SyntaxError("%s on line %s col %s" % (err.args[0], err.lineno, err.offset))
raise SyntaxError("%s on line %s col %s" % (e.args[0], e.lineno, e.offset))

def parse_predicate_formula_json(formula):
"""
Expand Down Expand Up @@ -91,6 +91,9 @@ def process_renames(formula, collector, renamer):
except SyntaxError:
# Don't do anything to a syntactically wrong formula.
return formula
except ValueError as e:
if str(e).startswith("Unsupported syntax"):
return formula

for subject in collector.entities:
new_name = renamer(subject)
Expand Down
18 changes: 4 additions & 14 deletions sandbox/grist/test_dropdown_condition_renames.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,9 @@ def assert_invalid_formula_untouched(self):
})]
])

def apply_user_action_ignore_syntax_error(self, user_action_repr):
try:
self.apply_user_action(user_action_repr)
except ValueError as e:
if str(e) == "Unsupported syntax at 1:1":
pass
else:
raise e
raise e

def test_referred_column_renames(self):
# Rename the column "city" in table "Address" to "area". Schools.address refers to it.
self.apply_user_action_ignore_syntax_error(["RenameColumn", "Address", "city", "area"])
self.apply_user_action(["RenameColumn", "Address", "city", "area"])
# Now choice.city should become choice.area. This should also be reflected in the parsed formula.
self.assertTableData("_grist_Tables_column", cols="subset", rows="subset", data=[
["id", "parentId", "colId", "widgetOptions"],
Expand All @@ -98,7 +88,7 @@ def test_referred_column_renames(self):
def test_record_column_renames(self):
# Rename the column "name" in table "Schools" to "identifier". Schools.address refers to it in two ways -
# the dollar sign and "rec.".
self.apply_user_action_ignore_syntax_error(["RenameColumn", "Schools", "name", "identifier"])
self.apply_user_action(["RenameColumn", "Schools", "name", "identifier"])
# Now "$name" should become "$identifier" while "rec.name" should become "rec.identifier".
self.assertTableData("_grist_Tables_column", cols="subset", rows="subset", data=[
["id", "parentId", "colId", "widgetOptions"],
Expand All @@ -113,8 +103,8 @@ def test_record_column_renames(self):

def test_multiple_renames(self):
# Put all renames together.
self.apply_user_action_ignore_syntax_error(["RenameColumn", "Address", "city", "area"])
self.apply_user_action_ignore_syntax_error(["RenameColumn", "Schools", "name", "identifier"])
self.apply_user_action(["RenameColumn", "Address", "city", "area"])
self.apply_user_action(["RenameColumn", "Schools", "name", "identifier"])
self.assertTableData("_grist_Tables_column", cols="subset", rows="subset", data=[
["id", "parentId", "colId", "widgetOptions"],
[12, 2, "address", json.dumps({
Expand Down

0 comments on commit 9847b84

Please sign in to comment.