浏览代码

Fixes naming on Amend

version-14
Anand Doshi 11 年前
父节点
当前提交
4863acd714
共有 1 个文件被更改,包括 28 次插入28 次删除
  1. +28
    -28
      frappe/model/naming.py

+ 28
- 28
frappe/model/naming.py 查看文件

@@ -1,5 +1,5 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
# MIT License. See license.txt


from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe
@@ -16,8 +16,8 @@ def set_new_name(doc):
doc.localname = doc.name # for passing back to client doc.localname = doc.name # for passing back to client


# amendments # amendments
if getattr(doc, "amended_from", None):
return doc._get_amended_name()
if getattr(doc, "amended_from", None):
return _get_amended_name(doc)
else: else:
tmp = getattr(doc, "autoname", None) tmp = getattr(doc, "autoname", None)
if tmp and not isinstance(tmp, basestring): if tmp and not isinstance(tmp, basestring):
@@ -32,62 +32,62 @@ def set_new_name(doc):
if not n: if not n:
raise Exception, 'Name is required' raise Exception, 'Name is required'
doc.name = n.strip() doc.name = n.strip()
elif autoname and autoname.startswith("naming_series:"): elif autoname and autoname.startswith("naming_series:"):
if not doc.naming_series: if not doc.naming_series:
doc.naming_series = get_default_naming_series(doc.doctype) doc.naming_series = get_default_naming_series(doc.doctype)
if not doc.naming_series: if not doc.naming_series:
frappe.msgprint(frappe._("Naming Series mandatory"), raise_exception=True) frappe.msgprint(frappe._("Naming Series mandatory"), raise_exception=True)
doc.name = make_autoname(doc.naming_series+'.#####') doc.name = make_autoname(doc.naming_series+'.#####')
# call the method! # call the method!
elif autoname and autoname!='Prompt':
elif autoname and autoname!='Prompt':
doc.name = make_autoname(autoname, doc.doctype) doc.name = make_autoname(autoname, doc.doctype)
# given # given
elif doc.get('__newname', None):
elif doc.get('__newname', None):
doc.name = doc.get('__newname') doc.name = doc.get('__newname')


# default name for table # default name for table
elif doc.meta.istable:
elif doc.meta.istable:
doc.name = make_autoname('#########', doc.doctype) doc.name = make_autoname('#########', doc.doctype)
# unable to determine a name, use global series # unable to determine a name, use global series
if not doc.name: if not doc.name:
doc.name = make_autoname('#########', doc.doctype) doc.name = make_autoname('#########', doc.doctype)
validate_name(doc.doctype, doc.name) validate_name(doc.doctype, doc.name)


def make_autoname(key, doctype=''): def make_autoname(key, doctype=''):
""" """
Creates an autoname from the given key: Creates an autoname from the given key:
**Autoname rules:** **Autoname rules:**
* The key is separated by '.' * The key is separated by '.'
* '####' represents a series. The string before this part becomes the prefix: * '####' represents a series. The string before this part becomes the prefix:
Example: ABC.#### creates a series ABC0001, ABC0002 etc Example: ABC.#### creates a series ABC0001, ABC0002 etc
* 'MM' represents the current month * 'MM' represents the current month
* 'YY' and 'YYYY' represent the current year * 'YY' and 'YYYY' represent the current year


*Example:* *Example:*
* DE/./.YY./.MM./.##### will create a series like * DE/./.YY./.MM./.##### will create a series like
DE/09/01/0001 where 09 is the year, 01 is the month and 0001 is the series DE/09/01/0001 where 09 is the year, 01 is the month and 0001 is the series
""" """
if key=="hash": if key=="hash":
return frappe.generate_hash(doctype) return frappe.generate_hash(doctype)
if not "#" in key: if not "#" in key:
key = key + ".#####" key = key + ".#####"
n = '' n = ''
l = key.split('.') l = key.split('.')
series_set = False series_set = False
today = now_datetime() today = now_datetime()
for e in l: for e in l:
en = '' en = ''
if e.startswith('#'): if e.startswith('#'):
@@ -95,14 +95,14 @@ def make_autoname(key, doctype=''):
digits = len(e) digits = len(e)
en = getseries(n, digits, doctype) en = getseries(n, digits, doctype)
series_set = True series_set = True
elif e=='YY':
elif e=='YY':
en = today.strftime('%y') en = today.strftime('%y')
elif e=='MM':
elif e=='MM':
en = today.strftime('%m') en = today.strftime('%m')
elif e=='DD': elif e=='DD':
en = today.strftime("%d") en = today.strftime("%d")
elif e=='YYYY':
en = today.strftime('%Y')
elif e=='YYYY':
en = today.strftime('%Y')
else: en = e else: en = e
n+=en n+=en
return n return n
@@ -126,11 +126,11 @@ def get_default_naming_series(doctype):
naming_series = frappe.get_meta(doctype).get_field("naming_series").options or "" naming_series = frappe.get_meta(doctype).get_field("naming_series").options or ""
if naming_series: if naming_series:
naming_series = naming_series.split("\n") naming_series = naming_series.split("\n")
return naming_series[0] or naming_series[1]
return naming_series[0] or naming_series[1]
else: else:
return None return None


def validate_name(doctype, name, case=None, merge=False):
def validate_name(doctype, name, case=None, merge=False):
if not name: return 'No Name Specified for %s' % doctype if not name: return 'No Name Specified for %s' % doctype
if name.startswith('New '+doctype): if name.startswith('New '+doctype):
raise NameError, 'There were some errors setting the name, please contact the administrator' raise NameError, 'There were some errors setting the name, please contact the administrator'
@@ -138,21 +138,21 @@ def validate_name(doctype, name, case=None, merge=False):
if case=='UPPER CASE': name = name.upper() if case=='UPPER CASE': name = name.upper()
name = name.strip() name = name.strip()
return name return name
def _get_amended_name(doc): def _get_amended_name(doc):
am_id = 1 am_id = 1
am_prefix = doc.amended_from am_prefix = doc.amended_from
if frappe.db.get_value(doc.doctype, doc.amended_from, "amended_from"): if frappe.db.get_value(doc.doctype, doc.amended_from, "amended_from"):
am_id = cint(doc.amended_from.split('-')[-1]) + 1 am_id = cint(doc.amended_from.split('-')[-1]) + 1
am_prefix = '-'.join(doc.amended_from.split('-')[:-1]) # except the last hyphen am_prefix = '-'.join(doc.amended_from.split('-')[:-1]) # except the last hyphen
doc.name = am_prefix + '-' + str(am_id) doc.name = am_prefix + '-' + str(am_id)
return doc.name return doc.name


def append_number_if_name_exists(doc): def append_number_if_name_exists(doc):
if frappe.db.exists(doc.doctype, doc.name): if frappe.db.exists(doc.doctype, doc.name):
last = frappe.db.sql("""select name from `tab{}` last = frappe.db.sql("""select name from `tab{}`
where name regexp '{}-[[:digit:]]+'
where name regexp '{}-[[:digit:]]+'
order by name desc limit 1""".format(doc.doctype, doc.name)) order by name desc limit 1""".format(doc.doctype, doc.name))


if last: if last:


正在加载...
取消
保存