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.
 
 
 
 
 
 

119 rivejä
3.5 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes, json
  5. from webnotes import _
  6. @webnotes.whitelist()
  7. def remove_attach():
  8. """remove attachment"""
  9. import webnotes.utils.file_manager
  10. fid = webnotes.form_dict.get('fid')
  11. webnotes.utils.file_manager.remove_file(fid)
  12. @webnotes.whitelist()
  13. def get_fields():
  14. """get fields"""
  15. r = {}
  16. args = {
  17. 'select':webnotes.form_dict.get('select')
  18. ,'from':webnotes.form_dict.get('from')
  19. ,'where':webnotes.form_dict.get('where')
  20. }
  21. ret = webnotes.conn.sql("select %(select)s from `%(from)s` where %(where)s limit 1" % args)
  22. if ret:
  23. fl, i = webnotes.form_dict.get('fields').split(','), 0
  24. for f in fl:
  25. r[f], i = ret[0][i], i+1
  26. webnotes.response['message']=r
  27. @webnotes.whitelist()
  28. def validate_link():
  29. """validate link when updated by user"""
  30. import webnotes
  31. import webnotes.utils
  32. value, options, fetch = webnotes.form_dict.get('value'), webnotes.form_dict.get('options'), webnotes.form_dict.get('fetch')
  33. # no options, don't validate
  34. if not options or options=='null' or options=='undefined':
  35. webnotes.response['message'] = 'Ok'
  36. return
  37. if webnotes.conn.sql("select name from `tab%s` where name=%s" % (options, '%s'), (value,)):
  38. # get fetch values
  39. if fetch:
  40. webnotes.response['fetch_values'] = [webnotes.utils.parse_val(c) \
  41. for c in webnotes.conn.sql("select %s from `tab%s` where name=%s" \
  42. % (fetch, options, '%s'), (value,))[0]]
  43. webnotes.response['message'] = 'Ok'
  44. @webnotes.whitelist()
  45. def add_comment(doclist):
  46. """allow any logged user to post a comment"""
  47. doclist = json.loads(doclist)
  48. doclist[0]["__islocal"] = 1
  49. doclistobj = webnotes.bean(doclist)
  50. doclistobj.ignore_permissions = True
  51. doclistobj.save()
  52. return [d.fields for d in doclist]
  53. return save(doclist)
  54. @webnotes.whitelist()
  55. def get_next(doctype, name, prev):
  56. import webnotes.widgets.reportview
  57. prev = int(prev)
  58. field = "`tab%s`.name" % doctype
  59. res = webnotes.widgets.reportview.execute(doctype,
  60. fields = [field],
  61. filters = [[doctype, "name", "<" if prev else ">", name]],
  62. order_by = field + " " + ("desc" if prev else "asc"),
  63. limit_start=0, limit_page_length=1, as_list=True)
  64. if not res:
  65. webnotes.msgprint(_("No further records"))
  66. return None
  67. else:
  68. return res[0][0]
  69. @webnotes.whitelist()
  70. def get_linked_docs(doctype, name, metadata_loaded=None):
  71. if not metadata_loaded: metadata_loaded = []
  72. meta = webnotes.get_doctype(doctype, True)
  73. linkinfo = meta[0].get("__linked_with")
  74. results = {}
  75. for dt, link in linkinfo.items():
  76. link["doctype"] = dt
  77. linkmeta = webnotes.get_doctype(dt, True)
  78. if not linkmeta[0].get("issingle"):
  79. fields = [d.fieldname for d in linkmeta.get({"parent":dt, "in_list_view":1,
  80. "fieldtype": ["not in", ["Image", "HTML", "Button", "Table"]]})] \
  81. + ["name", "modified", "docstatus"]
  82. fields = ["`tab{dt}`.`{fn}`".format(dt=dt, fn=sf.strip()) for sf in fields if sf]
  83. if link.get("child_doctype"):
  84. ret = webnotes.get_list(doctype=dt, fields=fields,
  85. filters=[[link.get('child_doctype'), link.get("fieldname"), '=', name]])
  86. else:
  87. ret = webnotes.get_list(doctype=dt, fields=fields,
  88. filters=[[dt, link.get("fieldname"), '=', name]])
  89. if ret:
  90. results[dt] = ret
  91. if not dt in metadata_loaded:
  92. if not "docs" in webnotes.local.response:
  93. webnotes.local.response.docs = []
  94. webnotes.local.response.docs += linkmeta
  95. return results