You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

95 lines
3.1 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. # metadata
  4. from __future__ import unicode_literals
  5. import webnotes
  6. from webnotes.utils import cstr, cint
  7. def is_single(doctype):
  8. try:
  9. return webnotes.conn.get_value("DocType", doctype, "issingle")
  10. except IndexError, e:
  11. raise Exception, 'Cannot determine whether %s is single' % doctype
  12. def get_parent_dt(dt):
  13. parent_dt = webnotes.conn.sql("""select parent from tabDocField
  14. where fieldtype="Table" and options="%s" and (parent not like "old_parent:%%")
  15. limit 1""" % dt)
  16. return parent_dt and parent_dt[0][0] or ''
  17. def set_fieldname(field_id, fieldname):
  18. webnotes.conn.set_value('DocField', field_id, 'fieldname', fieldname)
  19. def get_link_fields(doctype):
  20. """
  21. Returns list of link fields for a doctype in tuple (fieldname, options, label)
  22. """
  23. import webnotes.model.doctype
  24. doclist = webnotes.model.doctype.get(doctype)
  25. return [
  26. (d.fields.get('fieldname'), d.fields.get('options'), d.fields.get('label'))
  27. for d in doclist
  28. if d.fields.get('doctype') == 'DocField' and d.fields.get('parent') == doctype
  29. and d.fields.get('fieldname')!='owner'
  30. and (d.fields.get('fieldtype') == 'Link' or
  31. ( d.fields.get('fieldtype') == 'Select'
  32. and (d.fields.get('options') or '').startswith('link:'))
  33. )
  34. ]
  35. def get_table_fields(doctype):
  36. child_tables = [[d[0], d[1]] for d in webnotes.conn.sql("select options, fieldname from tabDocField \
  37. where parent='%s' and fieldtype='Table'" % doctype, as_list=1)]
  38. try:
  39. custom_child_tables = [[d[0], d[1]] for d in webnotes.conn.sql("select options, fieldname from `tabCustom Field` \
  40. where dt='%s' and fieldtype='Table'" % doctype, as_list=1)]
  41. except Exception, e:
  42. if e.args[0]!=1146:
  43. raise e
  44. custom_child_tables = []
  45. return child_tables + custom_child_tables
  46. def has_field(doctype, fieldname, parent=None, parentfield=None):
  47. return get_field(doctype, fieldname, parent=None, parentfield=None) and True or False
  48. def get_field(doctype, fieldname, parent=None, parentfield=None):
  49. doclist = webnotes.get_doctype(doctype)
  50. return doclist.get_field(fieldname, parent, parentfield)
  51. def get_field_currency(df, doc):
  52. """get currency based on DocField options and fieldvalue in doc"""
  53. currency = None
  54. if ":" in cstr(df.options):
  55. split_opts = df.options.split(":")
  56. if len(split_opts)==3:
  57. currency = webnotes.conn.get_value(split_opts[0], doc.fields.get(split_opts[1]),
  58. split_opts[2])
  59. else:
  60. currency = doc.fields.get(df.options)
  61. return currency
  62. def get_field_precision(df, doc):
  63. """get precision based on DocField options and fieldvalue in doc"""
  64. from webnotes.utils import get_number_format_info
  65. number_format = None
  66. if df.fieldtype == "Currency":
  67. currency = get_field_currency(df, doc)
  68. if currency:
  69. number_format = webnotes.conn.get_value("Currency", currency, "number_format")
  70. if not number_format:
  71. number_format = webnotes.conn.get_default("number_format") or "#,###.##"
  72. decimal_str, comma_str, precision = get_number_format_info(number_format)
  73. if df.fieldtype == "Float":
  74. precision = cint(webnotes.conn.get_default("float_precision")) or 3
  75. return precision