25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

68 lines
1.9 KiB

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