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.
 
 
 
 
 
 

144 line
5.1 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. # Search
  23. import webnotes
  24. # this is called when a new doctype is setup for search - to set the filters
  25. @webnotes.whitelist()
  26. def getsearchfields():
  27. sf = webnotes.conn.sql("""\
  28. SELECT value FROM `tabProperty Setter`
  29. WHERE doc_type=%s AND property='search_fields'""", \
  30. (webnotes.form.getvalue("doctype")))
  31. if not (sf and len(sf)>0 and sf[0][0]):
  32. sf = webnotes.conn.sql("select search_fields from tabDocType where name=%s", webnotes.form.getvalue("doctype"))
  33. sf = sf and sf[0][0] or ''
  34. sf = [s.strip() for s in sf.split(',')]
  35. if sf and sf[0]:
  36. 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)+'"'))
  37. else:
  38. res = []
  39. res = [[c or '' for c in r] for r in res]
  40. for r in res:
  41. if r[2]=='Select' and r[3] and r[3].startswith('link:'):
  42. dt = r[3][5:]
  43. ol = webnotes.conn.sql("select name from `tab%s` where docstatus!=2 order by name asc" % dt)
  44. r[3] = '\n'.join([''] + [o[0] for o in ol])
  45. webnotes.response['searchfields'] = [['name', 'ID', 'Data', '']] + res
  46. def make_query(fields, dt, key, txt, start, length):
  47. query = """SELECT %(fields)s
  48. FROM `tab%(dt)s`
  49. WHERE `tab%(dt)s`.`%(key)s` LIKE '%(txt)s' AND `tab%(dt)s`.docstatus != 2
  50. ORDER BY `tab%(dt)s`.`%(key)s`
  51. ASC LIMIT %(start)s, %(len)s """ % {
  52. 'fields': fields,
  53. 'dt': dt,
  54. 'key': key,
  55. 'txt': txt + '%',
  56. 'start': start,
  57. 'len': length
  58. }
  59. return query
  60. def get_std_fields_list(dt, key):
  61. # get additional search fields
  62. sflist = webnotes.conn.sql("select search_fields from tabDocType where name = '%s'" % dt)
  63. sflist = sflist and sflist[0][0] and sflist[0][0].split(',') or []
  64. sflist = ['name'] + sflist
  65. if not key in sflist:
  66. sflist = sflist + [key]
  67. return ['`tab%s`.`%s`' % (dt, f.strip()) for f in sflist]
  68. def build_for_autosuggest(res):
  69. from webnotes.utils import cstr
  70. results = []
  71. for r in res:
  72. info = ''
  73. if len(r) > 1:
  74. info = ','.join([cstr(t) for t in r[1:]])
  75. if len(info) > 30:
  76. info = info[:30] + '...'
  77. results.append({'label':r[0], 'value':r[0], 'info':info})
  78. return results
  79. def scrub_custom_query(query, key, txt):
  80. if '%(key)s' in query:
  81. query = query.replace('%(key)s', key)
  82. if '%s' in query:
  83. query = query.replace('%s', ((txt or '') + '%'))
  84. return query
  85. # this is called by the Link Field
  86. @webnotes.whitelist()
  87. def search_link():
  88. import webnotes.widgets.query_builder
  89. txt = webnotes.form.getvalue('txt')
  90. dt = webnotes.form.getvalue('dt')
  91. query = webnotes.form.getvalue('query')
  92. # txt - decode it to utf-8. why to do this?
  93. # "%(something_unicode)s %(something ascii encoded with utf-8)s"
  94. # tries to decode ascii string using ascii codec and not utf-8
  95. # since web pages are encoded in utf-8, we can force decode to utf-8
  96. txt = txt.decode('utf-8')
  97. if query:
  98. res = webnotes.conn.sql(scrub_custom_query(query, 'name', txt))
  99. else:
  100. q = make_query(', '.join(get_std_fields_list(dt, 'name')), dt, 'name', txt, '0', '10')
  101. res = webnotes.widgets.query_builder.runquery(q, ret=1)
  102. # make output
  103. webnotes.response['results'] = build_for_autosuggest(res)
  104. # this is called by the search box
  105. @webnotes.whitelist()
  106. def search_widget():
  107. import webnotes.widgets.query_builder
  108. dt = webnotes.form.getvalue('doctype')
  109. txt = webnotes.form.getvalue('txt') or ''
  110. key = webnotes.form.getvalue('searchfield') or 'name' # key field
  111. user_query = webnotes.form.getvalue('query') or ''
  112. # txt - decode it to utf-8. why to do this?
  113. # "%(something_unicode)s %(something ascii encoded with utf-8)s"
  114. # tries to decode ascii string using ascii codec and not utf-8
  115. # since web pages are encoded in utf-8, we can force decode to utf-8
  116. txt = txt.decode('utf-8')
  117. if user_query:
  118. query = scrub_custom_query(user_query, key, txt)
  119. else:
  120. 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)
  121. webnotes.widgets.query_builder.runquery(query)