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.
 
 
 
 
 
 

93 line
3.1 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. # Search
  4. from __future__ import unicode_literals
  5. import webnotes
  6. import webnotes.widgets.reportview
  7. from webnotes.utils import cstr
  8. try:
  9. from startup.query_handlers import standard_queries
  10. startup_standard_queries = True
  11. except ImportError:
  12. startup_standard_queries = False
  13. # this is called by the Link Field
  14. @webnotes.whitelist()
  15. def search_link(doctype, txt, query=None, filters=None, page_len=20, searchfield="name"):
  16. search_widget(doctype, txt, query, searchfield=searchfield, page_len=page_len, filters=filters)
  17. webnotes.response['results'] = build_for_autosuggest(webnotes.response["values"])
  18. del webnotes.response["values"]
  19. # this is called by the search box
  20. @webnotes.whitelist()
  21. def search_widget(doctype, txt, query=None, searchfield="name", start=0,
  22. page_len=50, filters=None):
  23. if isinstance(filters, basestring):
  24. import json
  25. filters = json.loads(filters)
  26. meta = webnotes.get_doctype(doctype)
  27. if query and query.split()[0].lower()!="select":
  28. # by method
  29. webnotes.response["values"] = webnotes.get_method(query)(doctype, txt,
  30. searchfield, start, page_len, filters)
  31. elif startup_standard_queries and not query and doctype in standard_queries:
  32. # from standard queries
  33. search_widget(doctype, txt, standard_queries[doctype],
  34. searchfield, start, page_len, filters)
  35. else:
  36. if query:
  37. # custom query
  38. webnotes.response["values"] = webnotes.conn.sql(scrub_custom_query(query,
  39. searchfield, txt))
  40. else:
  41. if isinstance(filters, dict):
  42. filters_items = filters.items()
  43. filters = []
  44. for f in filters_items:
  45. if isinstance(f[1], (list, tuple)):
  46. filters.append([doctype, f[0], f[1][0], f[1][1]])
  47. else:
  48. filters.append([doctype, f[0], "=", f[1]])
  49. if filters==None:
  50. filters = []
  51. # build from doctype
  52. if txt:
  53. filters.append([doctype, searchfield or "name", "like", txt + "%"])
  54. if meta.get({"parent":doctype, "fieldname":"enabled", "fieldtype":"Check"}):
  55. filters.append([doctype, "enabled", "=", 1])
  56. if meta.get({"parent":doctype, "fieldname":"disabled", "fieldtype":"Check"}):
  57. filters.append([doctype, "disabled", "!=", 1])
  58. webnotes.response["values"] = webnotes.widgets.reportview.execute(doctype,
  59. filters=filters, fields = get_std_fields_list(meta, searchfield or "name"),
  60. limit_start = start, limit_page_length=page_len, as_list=True)
  61. def get_std_fields_list(meta, key):
  62. # get additional search fields
  63. sflist = meta[0].search_fields and meta[0].search_fields.split(",") or []
  64. sflist = ['name'] + sflist
  65. if not key in sflist:
  66. sflist = sflist + [key]
  67. return ['`tab%s`.`%s`' % (meta[0].name, f.strip()) for f in sflist]
  68. def build_for_autosuggest(res):
  69. results = []
  70. for r in res:
  71. out = {"value": r[0], "description": ", ".join([cstr(d) for d in r[1:]])}
  72. results.append(out)
  73. return results
  74. def scrub_custom_query(query, key, txt):
  75. if '%(key)s' in query:
  76. query = query.replace('%(key)s', key)
  77. if '%s' in query:
  78. query = query.replace('%s', ((txt or '') + '%'))
  79. return query