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