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

53 řádky
1.5 KiB

  1. # auto masters
  2. # _ + fieldname is the table
  3. # 'value' is the column name, pkey
  4. import webnotes
  5. # create masters for a doctype
  6. def create_auto_masters(dt):
  7. fl = webnotes.conn.sql("select fieldname from tabDocField where fieldtype='Data' and options='Suggest' and parent=%s", dt)
  8. for f in fl:
  9. make_auto_master(f[0])
  10. # create master table
  11. def make_auto_master(fieldname):
  12. try:
  13. webnotes.conn.sql("select `value` from `__%s` limit 1" % fieldname)
  14. except Exception, e:
  15. if e.args[0]==1146:
  16. webnotes.conn.commit()
  17. webnotes.conn.sql("create table `__%s` (`value` varchar(220), primary key (`value`))" % fieldname)
  18. webnotes.conn.begin()
  19. # get auto master fields
  20. def get_master_fields(dt):
  21. if not webnotes.session['data'].get('auto_masters'):
  22. webnotes.session['data']['auto_masters'] = {}
  23. if webnotes.session['data']['auto_masters'].get(dt, None)==None:
  24. fl = webnotes.conn.sql("select fieldname from tabDocField where fieldtype='Data' and options='Suggest' and parent=%s", dt)
  25. webnotes.session['data']['auto_masters'][dt] = fl
  26. return webnotes.session['data']['auto_masters'][dt]
  27. # update value
  28. def update_auto_masters(doc):
  29. if not doc.doctype:
  30. return
  31. fl = get_master_fields(doc.doctype)
  32. # save masters in session cache
  33. for f in fl:
  34. if doc.fields.get(f[0]):
  35. add_to_master(f[0], doc.fields.get(f[0]))
  36. # add to master
  37. def add_to_master(fieldname, value):
  38. try:
  39. webnotes.conn.sql("insert into `__%s` (`value`) values (%s)" % (fieldname,'%s'), value)
  40. except Exception, e:
  41. # primary key
  42. pass