Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

359 řádky
10 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. from frappe import _
  6. from frappe.utils import now_datetime, cint, cstr
  7. import re
  8. from six import string_types
  9. from frappe.model import log_types
  10. def set_new_name(doc):
  11. """
  12. Sets the `name` property for the document based on various rules.
  13. 1. If amended doc, set suffix.
  14. 2. If `autoname` method is declared, then call it.
  15. 3. If `autoname` property is set in the DocType (`meta`), then build it using the `autoname` property.
  16. 4. If no rule defined, use hash.
  17. :param doc: Document to be named.
  18. """
  19. doc.run_method("before_naming")
  20. autoname = frappe.get_meta(doc.doctype).autoname or ""
  21. if autoname.lower() != "prompt" and not frappe.flags.in_import:
  22. doc.name = None
  23. if getattr(doc, "amended_from", None):
  24. _set_amended_name(doc)
  25. return
  26. elif getattr(doc.meta, "issingle", False):
  27. doc.name = doc.doctype
  28. elif getattr(doc.meta, "istable", False):
  29. doc.name = make_autoname("hash", doc.doctype)
  30. if not doc.name:
  31. set_naming_from_document_naming_rule(doc)
  32. if not doc.name:
  33. doc.run_method("autoname")
  34. if not doc.name and autoname:
  35. set_name_from_naming_options(autoname, doc)
  36. # if the autoname option is 'field:' and no name was derived, we need to
  37. # notify
  38. if not doc.name and autoname.startswith("field:"):
  39. fieldname = autoname[6:]
  40. frappe.throw(_("{0} is required").format(doc.meta.get_label(fieldname)))
  41. # at this point, we fall back to name generation with the hash option
  42. if not doc.name and autoname == "hash":
  43. doc.name = make_autoname("hash", doc.doctype)
  44. if not doc.name:
  45. doc.name = make_autoname("hash", doc.doctype)
  46. doc.name = validate_name(
  47. doc.doctype,
  48. doc.name,
  49. frappe.get_meta(doc.doctype).get_field("name_case")
  50. )
  51. def set_name_from_naming_options(autoname, doc):
  52. """
  53. Get a name based on the autoname field option
  54. """
  55. _autoname = autoname.lower()
  56. if _autoname.startswith("field:"):
  57. doc.name = _field_autoname(autoname, doc)
  58. elif _autoname.startswith("naming_series:"):
  59. set_name_by_naming_series(doc)
  60. elif _autoname.startswith("prompt"):
  61. _prompt_autoname(autoname, doc)
  62. elif _autoname.startswith("format:"):
  63. doc.name = _format_autoname(autoname, doc)
  64. elif "#" in autoname:
  65. doc.name = make_autoname(autoname, doc=doc)
  66. def set_naming_from_document_naming_rule(doc):
  67. '''
  68. Evaluate rules based on "Document Naming Series" doctype
  69. '''
  70. if doc.doctype in log_types:
  71. return
  72. # ignore_ddl if naming is not yet bootstrapped
  73. for d in frappe.get_all('Document Naming Rule',
  74. dict(document_type=doc.doctype, disabled=0), order_by='priority desc', ignore_ddl=True):
  75. frappe.get_cached_doc('Document Naming Rule', d.name).apply(doc)
  76. if doc.name:
  77. break
  78. def set_name_by_naming_series(doc):
  79. """Sets name by the `naming_series` property"""
  80. if not doc.naming_series:
  81. doc.naming_series = get_default_naming_series(doc.doctype)
  82. if not doc.naming_series:
  83. frappe.throw(frappe._("Naming Series mandatory"))
  84. doc.name = make_autoname(doc.naming_series+".#####", "", doc)
  85. def make_autoname(key="", doctype="", doc=""):
  86. """
  87. Creates an autoname from the given key:
  88. **Autoname rules:**
  89. * The key is separated by '.'
  90. * '####' represents a series. The string before this part becomes the prefix:
  91. Example: ABC.#### creates a series ABC0001, ABC0002 etc
  92. * 'MM' represents the current month
  93. * 'YY' and 'YYYY' represent the current year
  94. *Example:*
  95. * DE/./.YY./.MM./.##### will create a series like
  96. DE/09/01/0001 where 09 is the year, 01 is the month and 0001 is the series
  97. """
  98. if key == "hash":
  99. return frappe.generate_hash(doctype, 10)
  100. if "#" not in key:
  101. key = key + ".#####"
  102. elif "." not in key:
  103. error_message = _("Invalid naming series (. missing)")
  104. if doctype:
  105. error_message = _("Invalid naming series (. missing) for {0}").format(doctype)
  106. frappe.throw(error_message)
  107. parts = key.split('.')
  108. n = parse_naming_series(parts, doctype, doc)
  109. return n
  110. def parse_naming_series(parts, doctype='', doc=''):
  111. n = ''
  112. if isinstance(parts, string_types):
  113. parts = parts.split('.')
  114. series_set = False
  115. today = now_datetime()
  116. for e in parts:
  117. part = ''
  118. if e.startswith('#'):
  119. if not series_set:
  120. digits = len(e)
  121. part = getseries(n, digits)
  122. series_set = True
  123. elif e == 'YY':
  124. part = today.strftime('%y')
  125. elif e == 'MM':
  126. part = today.strftime('%m')
  127. elif e == 'DD':
  128. part = today.strftime("%d")
  129. elif e == 'YYYY':
  130. part = today.strftime('%Y')
  131. elif e == 'timestamp':
  132. part = str(today)
  133. elif e == 'FY':
  134. part = frappe.defaults.get_user_default("fiscal_year")
  135. elif e.startswith('{') and doc:
  136. e = e.replace('{', '').replace('}', '')
  137. part = doc.get(e)
  138. elif doc and doc.get(e):
  139. part = doc.get(e)
  140. else:
  141. part = e
  142. if isinstance(part, string_types):
  143. n += part
  144. return n
  145. def getseries(key, digits):
  146. # series created ?
  147. current = frappe.db.sql("SELECT `current` FROM `tabSeries` WHERE `name`=%s FOR UPDATE", (key,))
  148. if current and current[0][0] is not None:
  149. current = current[0][0]
  150. # yes, update it
  151. frappe.db.sql("UPDATE `tabSeries` SET `current` = `current` + 1 WHERE `name`=%s", (key,))
  152. current = cint(current) + 1
  153. else:
  154. # no, create it
  155. frappe.db.sql("INSERT INTO `tabSeries` (`name`, `current`) VALUES (%s, 1)", (key,))
  156. current = 1
  157. return ('%0'+str(digits)+'d') % current
  158. def revert_series_if_last(key, name, doc=None):
  159. """
  160. Reverts the series for particular naming series:
  161. * key is naming series - SINV-.YYYY-.####
  162. * name is actual name - SINV-2021-0001
  163. 1. This function split the key into two parts prefix (SINV-YYYY) & hashes (####).
  164. 2. Use prefix to get the current index of that naming series from Series table
  165. 3. Then revert the current index.
  166. *For custom naming series:*
  167. 1. hash can exist anywhere, if it exist in hashes then it take normal flow.
  168. 2. If hash doesn't exit in hashes, we get the hash from prefix, then update name and prefix accordingly.
  169. *Example:*
  170. 1. key = SINV-.YYYY.-
  171. * If key doesn't have hash it will add hash at the end
  172. * prefix will be SINV-YYYY based on this will get current index from Series table.
  173. 2. key = SINV-.####.-2021
  174. * now prefix = SINV-#### and hashes = 2021 (hash doesn't exist)
  175. * will search hash in key then accordingly get prefix = SINV-
  176. 3. key = ####.-2021
  177. * prefix = #### and hashes = 2021 (hash doesn't exist)
  178. * will search hash in key then accordingly get prefix = ""
  179. """
  180. if ".#" in key:
  181. prefix, hashes = key.rsplit(".", 1)
  182. if "#" not in hashes:
  183. # get the hash part from the key
  184. hash = re.search("#+", key)
  185. if not hash:
  186. return
  187. name = name.replace(hashes, "")
  188. prefix = prefix.replace(hash.group(), "")
  189. else:
  190. prefix = key
  191. if '.' in prefix:
  192. prefix = parse_naming_series(prefix.split('.'), doc=doc)
  193. count = cint(name.replace(prefix, ""))
  194. current = frappe.db.sql("SELECT `current` FROM `tabSeries` WHERE `name`=%s FOR UPDATE", (prefix,))
  195. if current and current[0][0]==count:
  196. frappe.db.sql("UPDATE `tabSeries` SET `current` = `current` - 1 WHERE `name`=%s", prefix)
  197. def get_default_naming_series(doctype):
  198. """get default value for `naming_series` property"""
  199. naming_series = frappe.get_meta(doctype).get_field("naming_series").options or ""
  200. if naming_series:
  201. naming_series = naming_series.split("\n")
  202. return naming_series[0] or naming_series[1]
  203. else:
  204. return None
  205. def validate_name(doctype, name, case=None, merge=False):
  206. if not name:
  207. frappe.throw(_("No Name Specified for {0}").format(doctype))
  208. if name.startswith("New "+doctype):
  209. frappe.throw(_("There were some errors setting the name, please contact the administrator"), frappe.NameError)
  210. if case == "Title Case":
  211. name = name.title()
  212. if case == "UPPER CASE":
  213. name = name.upper()
  214. name = name.strip()
  215. if not frappe.get_meta(doctype).get("issingle") and (doctype == name) and (name != "DocType"):
  216. frappe.throw(_("Name of {0} cannot be {1}").format(doctype, name), frappe.NameError)
  217. special_characters = "<>"
  218. if re.findall("[{0}]+".format(special_characters), name):
  219. message = ", ".join("'{0}'".format(c) for c in special_characters)
  220. frappe.throw(_("Name cannot contain special characters like {0}").format(message), frappe.NameError)
  221. return name
  222. def append_number_if_name_exists(doctype, value, fieldname="name", separator="-", filters=None):
  223. if not filters:
  224. filters = dict()
  225. filters.update({fieldname: value})
  226. exists = frappe.db.exists(doctype, filters)
  227. regex = "^{value}{separator}\d+$".format(value=re.escape(value), separator=separator)
  228. if exists:
  229. last = frappe.db.sql("""SELECT `{fieldname}` FROM `tab{doctype}`
  230. WHERE `{fieldname}` {regex_character} %s
  231. ORDER BY length({fieldname}) DESC,
  232. `{fieldname}` DESC LIMIT 1""".format(
  233. doctype=doctype,
  234. fieldname=fieldname,
  235. regex_character=frappe.db.REGEX_CHARACTER),
  236. regex)
  237. if last:
  238. count = str(cint(last[0][0].rsplit(separator, 1)[1]) + 1)
  239. else:
  240. count = "1"
  241. value = "{0}{1}{2}".format(value, separator, count)
  242. return value
  243. def _set_amended_name(doc):
  244. am_id = 1
  245. am_prefix = doc.amended_from
  246. if frappe.db.get_value(doc.doctype, doc.amended_from, "amended_from"):
  247. am_id = cint(doc.amended_from.split("-")[-1]) + 1
  248. am_prefix = "-".join(doc.amended_from.split("-")[:-1]) # except the last hyphen
  249. doc.name = am_prefix + "-" + str(am_id)
  250. return doc.name
  251. def _field_autoname(autoname, doc, skip_slicing=None):
  252. """
  253. Generate a name using `DocType` field. This is called when the doctype's
  254. `autoname` field starts with 'field:'
  255. """
  256. fieldname = autoname if skip_slicing else autoname[6:]
  257. name = (cstr(doc.get(fieldname)) or "").strip()
  258. return name
  259. def _prompt_autoname(autoname, doc):
  260. """
  261. Generate a name using Prompt option. This simply means the user will have to set the name manually.
  262. This is called when the doctype's `autoname` field starts with 'prompt'.
  263. """
  264. # set from __newname in save.py
  265. if not doc.name:
  266. frappe.throw(_("Name not set via prompt"))
  267. def _format_autoname(autoname, doc):
  268. """
  269. Generate autoname by replacing all instances of braced params (fields, date params ('DD', 'MM', 'YY'), series)
  270. Independent of remaining string or separators.
  271. Example pattern: 'format:LOG-{MM}-{fieldname1}-{fieldname2}-{#####}'
  272. """
  273. first_colon_index = autoname.find(":")
  274. autoname_value = autoname[first_colon_index + 1:]
  275. def get_param_value_for_match(match):
  276. param = match.group()
  277. # trim braces
  278. trimmed_param = param[1:-1]
  279. return parse_naming_series([trimmed_param], doc=doc)
  280. # Replace braced params with their parsed value
  281. name = re.sub(r"(\{[\w | #]+\})", get_param_value_for_match, autoname_value)
  282. return name