Generic Append number in naming (#4845)

* Generic append_number_if_name_exists

* Add test
This commit is contained in:
Faris Ansari 2018-01-17 18:45:37 +05:30 committed by Nabin Hait
parent 4ba2713874
commit aa7d93cfdb
2 changed files with 41 additions and 9 deletions

View file

@ -197,22 +197,26 @@ def _set_amended_name(doc):
doc.name = am_prefix + '-' + str(am_id)
return doc.name
def append_number_if_name_exists(doctype, name, fieldname='name', separator='-'):
if frappe.db.exists(doctype, name):
def append_number_if_name_exists(doctype, value, fieldname='name', separator='-'):
exists = frappe.db.exists(doctype,
value if fieldname == 'name' else {fieldname: value})
if exists:
# should be escaped 2 times since
# python string will parse the first escape
escaped_name = re.escape(re.escape(name))
last = frappe.db.sql("""select name from `tab{doctype}`
where {fieldname} regexp '^{name}{separator}[[:digit:]]+'
escaped_value = re.escape(re.escape(value))
last = frappe.db.sql("""select {fieldname} from `tab{doctype}`
where {fieldname} regexp '^{value}{separator}[[:digit:]]+'
order by length({fieldname}) desc,
{fieldname} desc limit 1""".format(doctype=doctype,
name=escaped_name, fieldname=fieldname, separator=separator))
value=escaped_value, fieldname=fieldname, separator=separator))
if last:
count = str(cint(last[0][0].rsplit("-", 1)[1]) + 1)
count = str(cint(last[0][0].rsplit(separator, 1)[1]) + 1)
else:
count = "1"
name = "{0}{1}{2}".format(name, separator, count)
value = "{0}{1}{2}".format(value, separator, count)
return name
return value

View file

@ -0,0 +1,28 @@
# Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import unicode_literals
import unittest
import frappe
from frappe.model.naming import append_number_if_name_exists
class TestNaming(unittest.TestCase):
def test_append_number_if_name_exists(self):
'''
Append number to name based on existing values
if Bottle exists
Bottle -> Bottle-1
if Bottle-1 exists
Bottle -> Bottle-2
'''
note = frappe.new_doc('Note')
note.title = 'Test'
note.insert()
title2 = append_number_if_name_exists('Note', 'Test')
self.assertEquals(title2, 'Test-1')
title2 = append_number_if_name_exists('Note', 'Test', 'title', '_')
self.assertEquals(title2, 'Test_1')