Skip to content

Commit

Permalink
fix: handle invalid fetch from split
Browse files Browse the repository at this point in the history
Some users have manged to write fetch expr with 2 dots 🤷

(cherry picked from commit 166d28a)
  • Loading branch information
ankush authored and mergify[bot] committed Mar 12, 2024
1 parent 8a54b7e commit fa131ca
Showing 1 changed file with 14 additions and 12 deletions.
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

0 comments on commit fa131ca

Please sign in to comment.