-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle invalid fetch from split
Some users have manged to write fetch expr with 2 dots 🤷 (cherry picked from commit 166d28a)
- Loading branch information
1 parent
8a54b7e
commit fa131ca
Showing
1 changed file
with
14 additions
and
12 deletions.
There are no files selected for viewing
26 changes: 14 additions & 12 deletions
26
frappe/custom/doctype/property_setter/patches/remove_invalid_fetch_from_expressions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
from contextlib import suppress | ||
|
||
import frappe | ||
|
||
|
||
def execute(): | ||
"""Remove invalid fetch from expressions""" | ||
with suppress(Exception): | ||
property_setters = frappe.get_all( | ||
"Property Setter", {"doctype_or_field": "DocField", "property": "fetch_from"}, ["name", "value"] | ||
) | ||
for ps in property_setters: | ||
if not is_valid_expression(ps.value): | ||
frappe.db.delete("Property Setter", {"name": ps.name}) | ||
|
||
property_setters = frappe.get_all( | ||
"Property Setter", {"doctype_or_field": "DocField", "property": "fetch_from"}, ["name", "value"] | ||
) | ||
for ps in property_setters: | ||
if not is_valid_expression(ps.value): | ||
frappe.db.delete("Property Setter", {"name": ps.name}) | ||
|
||
custom_fields = frappe.get_all("Custom Field", {"fetch_from": ("is", "set")}, ["name", "fetch_from"]) | ||
for cf in custom_fields: | ||
if not is_valid_expression(cf.fetch_from): | ||
frappe.db.set_value("Custom Field", cf.name, "fetch_from", "") | ||
custom_fields = frappe.get_all("Custom Field", {"fetch_from": ("is", "set")}, ["name", "fetch_from"]) | ||
for cf in custom_fields: | ||
if not is_valid_expression(cf.fetch_from): | ||
frappe.db.set_value("Custom Field", cf.name, "fetch_from", "") | ||
|
||
|
||
def is_valid_expression(expr) -> bool: | ||
if not expr or "." not in expr: | ||
return False | ||
source_field, target_field = expr.split(".") | ||
source_field, target_field = expr.split(".", maxsplit=1) | ||
if not source_field or not target_field: | ||
return False | ||
return True |