Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

134 linhas
4.6 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. query = """SELECT %(fields)s
  49. FROM `tab%(dt)s`
  50. WHERE `tab%(dt)s`.`%(key)s` LIKE '%(txt)s' AND `tab%(dt)s`.docstatus != 2
  51. ORDER BY `tab%(dt)s`.`%(key)s`
  52. ASC LIMIT %(start)s, %(len)s """ % {
  53. 'fields': fields,
  54. 'dt': dt,
  55. 'key': key,
  56. 'txt': txt + '%',
  57. 'start': start,
  58. 'len': length
  59. }
  60. return query
  61. def get_std_fields_list(dt, key):
  62. # get additional search fields
  63. sflist = webnotes.conn.sql("select search_fields from tabDocType where name = '%s'" % dt)
  64. sflist = sflist and sflist[0][0] and sflist[0][0].split(',') or []
  65. sflist = ['name'] + sflist
  66. if not key in sflist:
  67. sflist = sflist + [key]
  68. return ['`tab%s`.`%s`' % (dt, f.strip()) for f in sflist]
  69. def build_for_autosuggest(res):
  70. from webnotes.utils import cstr
  71. results = []
  72. for r in res:
  73. info = ''
  74. if len(r) > 1:
  75. info = ', '.join([cstr(t) for t in r[1:]])
  76. if len(info) > 50:
  77. info = "<span title=\"%s\">%s...</span>" % (info, info[:50])
  78. results.append({'label':r[0], 'value':r[0], 'info':info})
  79. return results
  80. def scrub_custom_query(query, key, txt):
  81. if '%(key)s' in query:
  82. query = query.replace('%(key)s', key)
  83. if '%s' in query:
  84. query = query.replace('%s', ((txt or '') + '%'))
  85. return query
  86. # this is called by the Link Field
  87. @webnotes.whitelist()
  88. def search_link():
  89. import webnotes.widgets.query_builder
  90. txt = webnotes.form_dict.get('txt')
  91. dt = webnotes.form_dict.get('dt')
  92. query = webnotes.form_dict.get('query')
  93. if query:
  94. res = webnotes.conn.sql(scrub_custom_query(query, 'name', txt))
  95. else:
  96. q = make_query(', '.join(get_std_fields_list(dt, 'name')), dt, 'name', txt, '0', '10')
  97. res = webnotes.widgets.query_builder.runquery(q, ret=1)
  98. # make output
  99. webnotes.response['results'] = build_for_autosuggest(res)
  100. # this is called by the search box
  101. @webnotes.whitelist()
  102. def search_widget():
  103. import webnotes.widgets.query_builder
  104. dt = webnotes.form_dict.get('doctype')
  105. txt = webnotes.form_dict.get('txt') or ''
  106. key = webnotes.form_dict.get('searchfield') or 'name' # key field
  107. user_query = webnotes.form_dict.get('query') or ''
  108. if user_query:
  109. query = scrub_custom_query(user_query, key, txt)
  110. else:
  111. 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)
  112. webnotes.widgets.query_builder.runquery(query)