[rename] [validation] validate new name given on rename and strip leading and trailing spaces
This commit is contained in:
parent
5944c84b97
commit
c345ecca5f
2 changed files with 33 additions and 24 deletions
|
|
@ -252,34 +252,13 @@ class Document:
|
|||
if not self.name:
|
||||
self.name = make_autoname('#########', self.doctype)
|
||||
|
||||
def _validate_name(self, case):
|
||||
if webnotes.conn.sql('select name from `tab%s` where name=%s' % (self.doctype,'%s'), self.name):
|
||||
raise NameError, 'Name %s already exists' % self.name
|
||||
|
||||
# no name
|
||||
if not self.name: return 'No Name Specified for %s' % self.doctype
|
||||
|
||||
# new..
|
||||
if self.name.startswith('New '+self.doctype):
|
||||
raise NameError, 'There were some errors setting the name, please contact the administrator'
|
||||
|
||||
if case=='Title Case': self.name = self.name.title()
|
||||
if case=='UPPER CASE': self.name = self.name.upper()
|
||||
|
||||
self.name = self.name.strip() # no leading and trailing blanks
|
||||
|
||||
forbidden = ['%', "'", '"', '#', '*', '?', '`']
|
||||
for f in forbidden:
|
||||
if f in self.name:
|
||||
webnotes.msgprint('%s not allowed in ID (name)' % f, raise_exception =1)
|
||||
|
||||
def _insert(self, autoname, istable, case='', make_autoname=1, keep_timestamps=False):
|
||||
# set name
|
||||
if make_autoname:
|
||||
self._set_name(autoname, istable)
|
||||
|
||||
# validate name
|
||||
self._validate_name(case)
|
||||
validate_name(self.doctype, self.name, case)
|
||||
|
||||
# insert!
|
||||
if not keep_timestamps:
|
||||
|
|
@ -686,4 +665,27 @@ def copy_common_fields(from_doc, to_doc):
|
|||
continue
|
||||
|
||||
if doctype_list.get_field(fieldname) and to_doc.fields[fieldname] != value:
|
||||
to_doc.fields[fieldname] = value
|
||||
to_doc.fields[fieldname] = value
|
||||
|
||||
def validate_name(doctype, name, case=None):
|
||||
if webnotes.conn.sql('select name from `tab%s` where name=%s' % (doctype,'%s'), name):
|
||||
raise NameError, 'Name %s already exists' % name
|
||||
|
||||
# no name
|
||||
if not name: return 'No Name Specified for %s' % doctype
|
||||
|
||||
# new..
|
||||
if name.startswith('New '+doctype):
|
||||
raise NameError, 'There were some errors setting the name, please contact the administrator'
|
||||
|
||||
if case=='Title Case': name = name.title()
|
||||
if case=='UPPER CASE': name = name.upper()
|
||||
|
||||
name = name.strip() # no leading and trailing blanks
|
||||
|
||||
forbidden = ['%', "'", '"', '#', '*', '?', '`']
|
||||
for f in forbidden:
|
||||
if f in name:
|
||||
webnotes.msgprint('%s not allowed in ID (name)' % f, raise_exception =1)
|
||||
|
||||
return name
|
||||
|
|
@ -3,6 +3,7 @@ import webnotes
|
|||
from webnotes import _
|
||||
import webnotes.utils
|
||||
import webnotes.model.doctype
|
||||
from webnotes.model.doc import validate_name
|
||||
|
||||
@webnotes.whitelist()
|
||||
def rename_doc(doctype, old, new, force=False, merge=False):
|
||||
|
|
@ -16,7 +17,7 @@ def rename_doc(doctype, old, new, force=False, merge=False):
|
|||
# get doclist of given doctype
|
||||
doclist = webnotes.model.doctype.get(doctype)
|
||||
|
||||
validate_rename(doctype, new, doclist, merge, force)
|
||||
new = validate_rename(doctype, new, doclist, merge, force)
|
||||
|
||||
# call on_rename
|
||||
obj = webnotes.get_obj(doctype, old)
|
||||
|
|
@ -47,6 +48,7 @@ def rename_parent_and_child(doctype, old, new, doclist):
|
|||
|
||||
def validate_rename(doctype, new, doclist, merge, force):
|
||||
exists = webnotes.conn.exists(doctype, new)
|
||||
|
||||
if merge and not exists:
|
||||
webnotes.msgprint("%s: %s does not exist, select a new target to merge." % (doctype, new), raise_exception=1)
|
||||
|
||||
|
|
@ -58,6 +60,11 @@ def validate_rename(doctype, new, doclist, merge, force):
|
|||
|
||||
if not force and not doclist[0].allow_rename:
|
||||
webnotes.msgprint("%s cannot be renamed" % doctype, raise_exception=1)
|
||||
|
||||
# validate naming like it's done in doc.py
|
||||
new = validate_name(doctype, new)
|
||||
|
||||
return new
|
||||
|
||||
def rename_doctype(doctype, old, new, force=False):
|
||||
# change options for fieldtype Table
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue