您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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