fix: naming part should be empty if field is empty (#20978)

This commit is contained in:
Ritwik Puri 2023-05-14 00:41:12 +05:30 committed by GitHub
parent 56dfa53f4b
commit 4bd32bcf04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View file

@ -336,11 +336,9 @@ def parse_naming_series(
part = str(today)
elif e == "FY":
part = frappe.defaults.get_user_default("fiscal_year")
elif e.startswith("{") and doc:
elif doc and (e.startswith("{") or hasattr(doc, e)):
e = e.replace("{", "").replace("}", "")
part = doc.get(e)
elif doc and doc.get(e):
part = doc.get(e)
else:
part = e
@ -550,9 +548,7 @@ def _format_autoname(autoname, doc):
def get_param_value_for_match(match):
param = match.group()
# trim braces
trimmed_param = param[1:-1]
return parse_naming_series([trimmed_param], doc=doc)
return parse_naming_series([param[1:-1]], doc=doc)
# Replace braced params with their parsed value
name = BRACED_PARAMS_PATTERN.sub(get_param_value_for_match, autoname_value)

View file

@ -369,6 +369,15 @@ class TestNaming(FrappeTestCase):
name.startswith("KOOH-"), f"incorrect name generated {name}, missing field value"
)
def test_naming_with_empty_field(self):
# check naming with empty field value
webhook = frappe.new_doc("Webhook")
series = "KOOH-.{request_structure}.-.request_structure.-.####"
name = parse_naming_series(series, doc=webhook)
self.assertTrue(name.startswith("KOOH---"), f"incorrect name generated {name}")
def make_invalid_todo():
frappe.get_doc({"doctype": "ToDo", "description": "Test"}).insert(set_name="ToDo")