選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

meta.py 4.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. # metadata
  23. from __future__ import unicode_literals
  24. import webnotes
  25. from webnotes.utils import cstr, cint
  26. def get_dt_values(doctype, fields, as_dict = 0):
  27. return webnotes.conn.sql('SELECT %s FROM tabDocType WHERE name="%s"' % (fields, doctype), as_dict = as_dict)
  28. def set_dt_value(doctype, field, value):
  29. return webnotes.conn.set_value('DocType', doctype, field, value)
  30. def is_single(doctype):
  31. try:
  32. return get_dt_values(doctype, 'issingle')[0][0]
  33. except IndexError, e:
  34. raise Exception, 'Cannot determine whether %s is single' % doctype
  35. def get_parent_dt(dt):
  36. parent_dt = webnotes.conn.sql("""select parent from tabDocField
  37. where fieldtype="Table" and options="%s" and (parent not like "old_parent:%%")
  38. limit 1""" % dt)
  39. return parent_dt and parent_dt[0][0] or ''
  40. def set_fieldname(field_id, fieldname):
  41. webnotes.conn.set_value('DocField', field_id, 'fieldname', fieldname)
  42. def get_link_fields(doctype):
  43. """
  44. Returns list of link fields for a doctype in tuple (fieldname, options, label)
  45. """
  46. import webnotes.model.doctype
  47. doclist = webnotes.model.doctype.get(doctype)
  48. return [
  49. (d.fields.get('fieldname'), d.fields.get('options'), d.fields.get('label'))
  50. for d in doclist
  51. if d.fields.get('doctype') == 'DocField' and d.fields.get('parent') == doctype
  52. and d.fields.get('fieldname')!='owner'
  53. and (d.fields.get('fieldtype') == 'Link' or
  54. ( d.fields.get('fieldtype') == 'Select'
  55. and (d.fields.get('options') or '').startswith('link:'))
  56. )
  57. ]
  58. def get_table_fields(doctype):
  59. child_tables = [[d[0], d[1]] for d in webnotes.conn.sql("select options, fieldname from tabDocField \
  60. where parent='%s' and fieldtype='Table'" % doctype, as_list=1)]
  61. try:
  62. custom_child_tables = [[d[0], d[1]] for d in webnotes.conn.sql("select options, fieldname from `tabCustom Field` \
  63. where dt='%s' and fieldtype='Table'" % doctype, as_list=1)]
  64. except Exception, e:
  65. if e.args[0]!=1146:
  66. raise e
  67. custom_child_tables = []
  68. return child_tables + custom_child_tables
  69. def has_field(doctype, fieldname, parent=None, parentfield=None):
  70. return get_field(doctype, fieldname, parent=None, parentfield=None) and True or False
  71. def get_field(doctype, fieldname, parent=None, parentfield=None):
  72. doclist = webnotes.get_doctype(doctype)
  73. return doclist.get_field(fieldname, parent, parentfield)
  74. def get_field_currency(df, doc):
  75. """get currency based on DocField options and fieldvalue in doc"""
  76. currency = None
  77. if ":" in cstr(df.options):
  78. split_opts = df.options.split(":")
  79. if len(split_opts)==3:
  80. currency = webnotes.conn.get_value(split_opts[0], doc.fields.get(split_opts[1]),
  81. split_opts[2])
  82. else:
  83. currency = doc.fields.get(df.options)
  84. return currency or webnotes.conn.get_default("currency")
  85. def get_field_precision(df, doc):
  86. """get precision based on DocField options and fieldvalue in doc"""
  87. from webnotes.utils import get_number_format_info
  88. number_format = None
  89. currency = get_field_currency(df, doc)
  90. if currency:
  91. number_format = webnotes.conn.get_value("Currency", currency, "number_format")
  92. decimal_str, comma_str, precision = get_number_format_info(number_format or \
  93. webnotes.conn.get_default("number_format") or "#,###.##")
  94. return precision