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.
 
 
 
 
 
 

103 lines
3.8 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. import webnotes.widgets.reportview
  26. import webnotes.widgets.query_builder
  27. from webnotes.utils import cstr
  28. from startup.query_handlers import standard_queries
  29. # this is called by the Link Field
  30. @webnotes.whitelist()
  31. def search_link(dt, txt, query=None, filters=None):
  32. search_widget(dt, txt, query, page_len=10, filters=filters)
  33. webnotes.response['results'] = build_for_autosuggest(webnotes.response["values"])
  34. # this is called by the search box
  35. @webnotes.whitelist()
  36. def search_widget(doctype, txt, query=None, searchfield="name", start=0,
  37. page_len=50, filters=None):
  38. if isinstance(filters, basestring):
  39. import json
  40. filters = json.loads(filters)
  41. if isinstance(filters, dict):
  42. filters = map(lambda f: [doctype, f[0], "=", f[1]], filters.items())
  43. if filters==None:
  44. filters = []
  45. meta = webnotes.get_doctype(doctype)
  46. if query and query.split()[0].lower()!="select":
  47. # by method
  48. webnotes.response["values"] = webnotes.get_method(query)(doctype, txt,
  49. searchfield, start, page_len, filters)
  50. elif not query and doctype in standard_queries:
  51. # from standard queries
  52. search_widget(doctype, txt, standard_queries[doctype],
  53. searchfield, start, page_len, filters)
  54. else:
  55. if query:
  56. # custom query
  57. webnotes.response["values"] = webnotes.conn.sql(scrub_custom_query(query,
  58. searchfield, txt))
  59. else:
  60. # build from doctype
  61. if txt:
  62. filters.append([doctype, searchfield, "like", txt + "%"])
  63. if meta.get({"parent":doctype, "fieldname":"enabled", "fieldtype":"Check"}):
  64. filters.append([doctype, "enabled", "=", 1])
  65. if meta.get({"parent":doctype, "fieldname":"disabled", "fieldtype":"Check"}):
  66. filters.append([doctype, "disabled", "!=", 1])
  67. webnotes.response["values"] = webnotes.widgets.reportview.execute(doctype,
  68. filters=filters, fields = get_std_fields_list(meta, searchfield),
  69. limit_start = start, limit_page_length=page_len, as_list=True)
  70. def get_std_fields_list(meta, key):
  71. # get additional search fields
  72. sflist = meta[0].search_fields and meta[0].search_fields.split(",") or []
  73. sflist = ['name'] + sflist
  74. if not key in sflist:
  75. sflist = sflist + [key]
  76. return ['`tab%s`.`%s`' % (meta[0].name, f.strip()) for f in sflist]
  77. def build_for_autosuggest(res):
  78. results = []
  79. for r in res:
  80. info = ''
  81. if len(r) > 1:
  82. info = ', '.join([cstr(t) for t in r[1:]])
  83. if len(info) > 50:
  84. info = "<span title=\"%s\">%s...</span>" % (info, info[:50])
  85. results.append({'label':r[0], 'value':r[0], 'info':info})
  86. return results
  87. def scrub_custom_query(query, key, txt):
  88. if '%(key)s' in query:
  89. query = query.replace('%(key)s', key)
  90. if '%s' in query:
  91. query = query.replace('%s', ((txt or '') + '%'))
  92. return query