You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

106 lines
3.4 KiB

  1. # Search
  2. import webnotes
  3. # this is called when a new doctype is setup for search - to set the filters
  4. def getsearchfields():
  5. sf = webnotes.conn.sql("""\
  6. SELECT value FROM `tabProperty Setter`
  7. WHERE doc_type=%s AND doc_name=%s AND property='search_fields'""", \
  8. (webnotes.form.getvalue("doctype"), webnotes.form.getvalue("doctype")))
  9. if not (sf and len(sf)>0 and sf[0][0]):
  10. sf = webnotes.conn.sql("select search_fields from tabDocType where name=%s", webnotes.form.getvalue("doctype"))
  11. sf = sf and sf[0][0] or ''
  12. sf = [s.strip() for s in sf.split(',')]
  13. if sf and sf[0]:
  14. res = webnotes.conn.sql("select fieldname, label, fieldtype, options from tabDocField where parent='%s' and fieldname in (%s)" % (webnotes.form.getvalue("doctype","_NA"), '"'+'","'.join(sf)+'"'))
  15. else:
  16. res = []
  17. res = [[c or '' for c in r] for r in res]
  18. for r in res:
  19. if r[2]=='Select' and r[3] and r[3].startswith('link:'):
  20. dt = r[3][5:]
  21. ol = webnotes.conn.sql("select name from `tab%s` where docstatus!=2 order by name asc" % dt)
  22. r[3] = '\n'.join([''] + [o[0] for o in ol])
  23. webnotes.response['searchfields'] = [['name', 'ID', 'Data', '']] + res
  24. def make_query(fields, dt, key, txt, start, length):
  25. return """SELECT %(fields)s
  26. FROM `tab%(dt)s`
  27. WHERE `tab%(dt)s`.`%(key)s` LIKE '%(txt)s' AND `tab%(dt)s`.docstatus != 2
  28. ORDER BY `tab%(dt)s`.`%(key)s`
  29. DESC LIMIT %(start)s, %(len)s """ % {
  30. 'fields': fields,
  31. 'dt': dt,
  32. 'key': key,
  33. 'txt': txt + '%',
  34. 'start': start,
  35. 'len': length
  36. }
  37. def get_std_fields_list(dt, key):
  38. # get additional search fields
  39. sflist = webnotes.conn.sql("select search_fields from tabDocType where name = '%s'" % dt)
  40. sflist = sflist and sflist[0][0] and sflist[0][0].split(',') or []
  41. sflist = ['name'] + sflist
  42. if not key in sflist:
  43. sflist = sflist + [key]
  44. return ['`tab%s`.`%s`' % (dt, f.strip()) for f in sflist]
  45. def build_for_autosuggest(res):
  46. from webnotes.utils import cstr
  47. results = []
  48. for r in res:
  49. info = ''
  50. if len(r) > 1:
  51. info = ','.join([cstr(t) for t in r[1:]])
  52. if len(info) > 30:
  53. info = info[:30] + '...'
  54. results.append({'id':r[0], 'value':r[0], 'info':info})
  55. return results
  56. def scrub_custom_query(query, key, txt):
  57. if '%(key)s' in query:
  58. query = query.replace('%(key)s', key)
  59. if '%s' in query:
  60. query = query.replace('%s', ((txt or '') + '%'))
  61. return query
  62. # this is called by the Link Field
  63. def search_link():
  64. import webnotes.widgets.query_builder
  65. txt = webnotes.form.getvalue('txt')
  66. dt = webnotes.form.getvalue('dt')
  67. query = webnotes.form.getvalue('query')
  68. if query:
  69. res = webnotes.conn.sql(scrub_custom_query(query, 'name', txt))
  70. else:
  71. q = make_query(', '.join(get_std_fields_list(dt, 'name')), dt, 'name', txt, '0', '10')
  72. res = webnotes.widgets.query_builder.runquery(q, ret=1)
  73. # make output
  74. webnotes.response['results'] = build_for_autosuggest(res)
  75. # this is called by the search box
  76. def search_widget():
  77. import webnotes.widgets.query_builder
  78. dt = webnotes.form.getvalue('doctype')
  79. txt = webnotes.form.getvalue('txt') or ''
  80. key = webnotes.form.getvalue('searchfield') or 'name' # key field
  81. user_query = webnotes.form.getvalue('query') or ''
  82. if user_query:
  83. query = scrub_custom_query(user_query, key, txt)
  84. else:
  85. query = make_query(', '.join(get_std_fields_list(dt, key)), dt, key, txt, webnotes.form.getvalue('start') or 0, webnotes.form.getvalue('page_len') or 50)
  86. webnotes.widgets.query_builder.runquery(query)