Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

62 rader
1.7 KiB

  1. from __future__ import unicode_literals
  2. """
  3. Create a new document with defaults set
  4. """
  5. import webnotes
  6. from webnotes.utils import nowdate, nowtime, cint, flt
  7. import webnotes.defaults
  8. def get_new_doc(doctype, parent_doc = None, parentfield = None):
  9. doc = webnotes.doc({
  10. "doctype": doctype,
  11. "__islocal": 1,
  12. "owner": webnotes.session.user,
  13. "docstatus": 0
  14. })
  15. meta = webnotes.get_doctype(doctype)
  16. if parent_doc:
  17. doc.parent = parent_doc.name
  18. doc.parenttype = parent_doc.doctype
  19. if parentfield:
  20. doc.parentfield = parentfield
  21. for d in meta.get({"doctype":"DocField", "parent": doctype}):
  22. default = webnotes.defaults.get_user_default(d.fieldname)
  23. if default:
  24. doc.fields[d.fieldname] = default
  25. elif d.fields.get("default"):
  26. if d.default == "__user":
  27. doc.fields[d.fieldname] = webnotes.session.user
  28. elif d.default == "Today":
  29. doc.fields[d.fieldname] = nowdate()
  30. elif d.default.startswith(":"):
  31. ref_fieldname = d.default[1:].lower().replace(" ", "_")
  32. if parent_doc:
  33. ref_docname = parent_doc.fields[ref_fieldname]
  34. else:
  35. ref_docname = webnotes.conn.get_default(ref_fieldname)
  36. doc.fields[d.fieldname] = webnotes.conn.get_value(d.default[1:],
  37. ref_docname, d.fieldname)
  38. else:
  39. doc.fields[d.fieldname] = d.default
  40. # convert type of default
  41. if d.fieldtype in ("Int", "Check"):
  42. doc.fields[d.fieldname] = cint(doc.fields[d.fieldname])
  43. elif d.fieldtype in ("Float", "Currency"):
  44. doc.fields[d.fieldname] = flt(doc.fields[d.fieldname])
  45. elif d.fieldtype == "Time":
  46. doc.fields[d.fieldname] = nowtime()
  47. if meta.get_field("naming_series") and not doc.naming_series:
  48. doc.set_naming_series()
  49. return doc