Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

54 rindas
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' \
  25. and options='Suggest' and parent=%s", dt)
  26. webnotes.session['data']['auto_masters'][dt] = fl
  27. return webnotes.session['data']['auto_masters'][dt]
  28. # update value
  29. def update_auto_masters(doc):
  30. if not doc.doctype:
  31. return
  32. fl = get_master_fields(doc.doctype)
  33. # save masters in session cache
  34. for f in fl:
  35. if doc.fields.get(f[0]):
  36. add_to_master(f[0], doc.fields.get(f[0]))
  37. # add to master
  38. def add_to_master(fieldname, value):
  39. try:
  40. webnotes.conn.sql("insert into `__%s` (`value`) values (%s)" % (fieldname,'%s'), value)
  41. except Exception, e:
  42. # primary key
  43. pass