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.
 
 
 
 
 
 

117 lines
3.2 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes
  5. from webnotes import _
  6. import webnotes.model
  7. import webnotes.utils
  8. import json
  9. @webnotes.whitelist()
  10. def get(doctype, name=None, filters=None):
  11. if filters and not name:
  12. name = webnotes.conn.get_value(doctype, json.loads(filters))
  13. if not name:
  14. raise Exception, "No document found for given filters"
  15. return [d.fields for d in webnotes.bean(doctype, name).doclist]
  16. @webnotes.whitelist()
  17. def get_value(doctype, fieldname, filters=None, as_dict=True, debug=False):
  18. if not webnotes.has_permission(doctype):
  19. webnotes.msgprint("No Permission", raise_exception=True)
  20. if fieldname and fieldname.startswith("["):
  21. fieldname = json.loads(fieldname)
  22. return webnotes.conn.get_value(doctype, json.loads(filters), fieldname, as_dict=as_dict, debug=debug)
  23. @webnotes.whitelist()
  24. def set_value(doctype, name, fieldname, value):
  25. if fieldname in webnotes.model.default_fields:
  26. webnotes.throw(_("Cannot edit standard fields"))
  27. doc = webnotes.conn.get_value(doctype, name, ["parenttype", "parent"], as_dict=True)
  28. if doc and doc.parent:
  29. bean = webnotes.bean(doc.parenttype, doc.parent)
  30. child = bean.doclist.getone({"doctype": doctype, "name": docname})
  31. child.fields[fieldname] = value
  32. else:
  33. bean = webnotes.bean(doctype, name)
  34. bean.doc.fields[fieldname] = value
  35. bean.save()
  36. return [d.fields for d in bean.doclist]
  37. @webnotes.whitelist()
  38. def insert(doclist):
  39. if isinstance(doclist, basestring):
  40. doclist = json.loads(doclist)
  41. doclist[0]["__islocal"] = 1
  42. return save(doclist)
  43. @webnotes.whitelist()
  44. def save(doclist):
  45. if isinstance(doclist, basestring):
  46. doclist = json.loads(doclist)
  47. doclistobj = webnotes.bean(doclist)
  48. doclistobj.save()
  49. return [d.fields for d in doclist]
  50. @webnotes.whitelist()
  51. def submit(doclist):
  52. if isinstance(doclist, basestring):
  53. doclist = json.loads(doclist)
  54. doclistobj = webnotes.bean(doclist)
  55. doclistobj.submit()
  56. return [d.fields for d in doclist]
  57. @webnotes.whitelist()
  58. def cancel(doctype, name):
  59. wrapper = webnotes.bean(doctype, name)
  60. wrapper.cancel()
  61. return [d.fields for d in wrapper.doclist]
  62. @webnotes.whitelist()
  63. def delete(doctype, name):
  64. webnotes.delete_doc(doctype, name)
  65. @webnotes.whitelist()
  66. def set_default(key, value, parent=None):
  67. """set a user default value"""
  68. webnotes.conn.set_default(key, value, parent or webnotes.session.user)
  69. webnotes.clear_cache(user=webnotes.session.user)
  70. @webnotes.whitelist()
  71. def make_width_property_setter():
  72. doclist = json.loads(webnotes.form_dict.doclist)
  73. if doclist[0]["doctype"]=="Property Setter" and doclist[0]["property"]=="width":
  74. bean = webnotes.bean(doclist)
  75. bean.ignore_permissions = True
  76. bean.insert()
  77. @webnotes.whitelist()
  78. def bulk_update(docs):
  79. docs = json.loads(docs)
  80. failed_docs = []
  81. for doc in docs:
  82. try:
  83. ddoc = {key: val for key, val in doc.iteritems() if key not in ['doctype', 'docname']}
  84. doctype = doc['doctype']
  85. docname = doc['docname']
  86. bean = webnotes.bean(doctype, docname)
  87. bean.doc.update(ddoc)
  88. bean.save()
  89. except:
  90. failed_docs.append({
  91. 'doc': doc,
  92. 'exc': webnotes.utils.getTraceback()
  93. })
  94. return {'failed_docs': failed_docs}