您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

76 行
2.6 KiB

  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. # auto masters
  23. # _ + fieldname is the table
  24. # 'value' is the column name, pkey
  25. import webnotes
  26. # create masters for a doctype
  27. def create_auto_masters(dt):
  28. fl = webnotes.conn.sql("select fieldname from tabDocField where fieldtype='Data' and options='Suggest' and parent=%s", dt)
  29. for f in fl:
  30. make_auto_master(f[0])
  31. # create master table
  32. def make_auto_master(fieldname):
  33. try:
  34. webnotes.conn.sql("select `value` from `__%s` limit 1" % fieldname)
  35. except Exception, e:
  36. if e.args[0]==1146:
  37. webnotes.conn.commit()
  38. webnotes.conn.sql("create table `__%s` (`value` varchar(220), primary key (`value`))" % fieldname)
  39. webnotes.conn.begin()
  40. # get auto master fields
  41. def get_master_fields(dt):
  42. if not webnotes.session['data'].get('auto_masters'):
  43. webnotes.session['data']['auto_masters'] = {}
  44. if webnotes.session['data']['auto_masters'].get(dt, None)==None:
  45. fl = webnotes.conn.sql("select fieldname from tabDocField where fieldtype='Data' \
  46. and options='Suggest' and parent=%s", dt)
  47. webnotes.session['data']['auto_masters'][dt] = fl
  48. return webnotes.session['data']['auto_masters'][dt]
  49. # update value
  50. def update_auto_masters(doc):
  51. if not doc.doctype:
  52. return
  53. fl = get_master_fields(doc.doctype)
  54. # save masters in session cache
  55. for f in fl:
  56. if doc.fields.get(f[0]):
  57. add_to_master(f[0], doc.fields.get(f[0]))
  58. # add to master
  59. def add_to_master(fieldname, value):
  60. try:
  61. webnotes.conn.sql("insert into `__%s` (`value`) values (%s)" % (fieldname,'%s'), value)
  62. except Exception, e:
  63. # primary key
  64. pass