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ů.

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