Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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