feat: Custom naming series parser via hooks (#21690)
* feat: Custom naming series parser via hooks * chore: use assignment operatot Co-authored-by: Ankush Menat <ankushmenat@gmail.com> * test: Unit test for custom parser * test: Unit test for custom parser --------- Co-authored-by: Ankush Menat <ankushmenat@gmail.com>
This commit is contained in:
parent
ba311a3ec8
commit
e32e74f2f1
3 changed files with 24 additions and 2 deletions
|
|
@ -428,3 +428,7 @@ after_job = [
|
|||
extend_bootinfo = [
|
||||
"frappe.utils.telemetry.add_bootinfo",
|
||||
]
|
||||
|
||||
naming_series_variables = {
|
||||
"PM": "frappe.tests.test_naming.parse_naming_series_variable",
|
||||
}
|
||||
|
|
|
|||
|
|
@ -339,11 +339,11 @@ def parse_naming_series(
|
|||
part = determine_consecutive_week_number(today)
|
||||
elif e == "timestamp":
|
||||
part = str(today)
|
||||
elif e == "FY":
|
||||
part = frappe.defaults.get_user_default("fiscal_year")
|
||||
elif doc and (e.startswith("{") or doc.get(e, _sentinel) is not _sentinel):
|
||||
e = e.replace("{", "").replace("}", "")
|
||||
part = doc.get(e)
|
||||
elif method := has_custom_parser(e):
|
||||
part = frappe.get_attr(method[0])(doc, e)
|
||||
else:
|
||||
part = e
|
||||
|
||||
|
|
@ -355,6 +355,11 @@ def parse_naming_series(
|
|||
return name
|
||||
|
||||
|
||||
def has_custom_parser(e):
|
||||
"""Returns true if the naming series part has a custom parser"""
|
||||
return frappe.get_hooks("naming_series_variables", {}).get(e)
|
||||
|
||||
|
||||
def determine_consecutive_week_number(datetime):
|
||||
"""Determines the consecutive calendar week"""
|
||||
m = datetime.month
|
||||
|
|
|
|||
|
|
@ -375,6 +375,19 @@ class TestNaming(FrappeTestCase):
|
|||
name = parse_naming_series(series, doc=webhook)
|
||||
self.assertTrue(name.startswith("KOOH---"), f"incorrect name generated {name}")
|
||||
|
||||
def test_custom_parser(self):
|
||||
# check naming with custom parser
|
||||
todo = frappe.new_doc("ToDo")
|
||||
series = "TODO-.PM.-.####"
|
||||
name = parse_naming_series(series, doc=todo)
|
||||
expected_name = "TODO-" + nowdate().split("-")[1] + "-" + "0001"
|
||||
self.assertEqual(name, expected_name)
|
||||
|
||||
|
||||
def parse_naming_series_variable(doc, variable):
|
||||
if variable == "PM":
|
||||
return nowdate().split("-")[1]
|
||||
|
||||
|
||||
def make_invalid_todo():
|
||||
frappe.get_doc({"doctype": "ToDo", "description": "Test"}).insert(set_name="ToDo")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue