Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

308 wiersze
9.0 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. from frappe import _
  6. import frappe.model
  7. import frappe.utils
  8. import json, os
  9. from six import iteritems, string_types, integer_types
  10. '''
  11. Handle RESTful requests that are mapped to the `/api/resource` route.
  12. Requests via FrappeClient are also handled here.
  13. '''
  14. @frappe.whitelist()
  15. def get_list(doctype, fields=None, filters=None, order_by=None,
  16. limit_start=None, limit_page_length=20):
  17. '''Returns a list of records by filters, fields, ordering and limit
  18. :param doctype: DocType of the data to be queried
  19. :param fields: fields to be returned. Default is `name`
  20. :param filters: filter list by this dict
  21. :param order_by: Order by this fieldname
  22. :param limit_start: Start at this index
  23. :param limit_page_length: Number of records to be returned (default 20)'''
  24. return frappe.get_list(doctype, fields=fields, filters=filters, order_by=order_by,
  25. limit_start=limit_start, limit_page_length=limit_page_length, ignore_permissions=False)
  26. @frappe.whitelist()
  27. def get(doctype, name=None, filters=None):
  28. '''Returns a document by name or filters
  29. :param doctype: DocType of the document to be returned
  30. :param name: return document of this `name`
  31. :param filters: If name is not set, filter by these values and return the first match'''
  32. if filters and not name:
  33. name = frappe.db.get_value(doctype, json.loads(filters))
  34. if not name:
  35. frappe.throw(_("No document found for given filters"))
  36. doc = frappe.get_doc(doctype, name)
  37. if not doc.has_permission("read"):
  38. raise frappe.PermissionError
  39. return frappe.get_doc(doctype, name).as_dict()
  40. @frappe.whitelist()
  41. def get_value(doctype, fieldname, filters=None, as_dict=True, debug=False):
  42. '''Returns a value form a document
  43. :param doctype: DocType to be queried
  44. :param fieldname: Field to be returned (default `name`)
  45. :param filters: dict or string for identifying the record'''
  46. if not frappe.has_permission(doctype):
  47. frappe.throw(_("No permission for {0}".format(doctype)), frappe.PermissionError)
  48. try:
  49. filters = json.loads(filters)
  50. if isinstance(filters, (integer_types, float)):
  51. filters = frappe.as_unicode(filters)
  52. except (TypeError, ValueError):
  53. # filters are not passesd, not json
  54. pass
  55. try:
  56. fieldname = json.loads(fieldname)
  57. except (TypeError, ValueError):
  58. # name passed, not json
  59. pass
  60. # check whether the used filters were really parseable and usable
  61. # and did not just result in an empty string or dict
  62. if not filters:
  63. filters = None
  64. return frappe.db.get_value(doctype, filters, fieldname, as_dict=as_dict, debug=debug)
  65. @frappe.whitelist()
  66. def set_value(doctype, name, fieldname, value=None):
  67. '''Set a value using get_doc, group of values
  68. :param doctype: DocType of the document
  69. :param name: name of the document
  70. :param fieldname: fieldname string or JSON / dict with key value pair
  71. :param value: value if fieldname is JSON / dict'''
  72. if fieldname!="idx" and fieldname in frappe.model.default_fields:
  73. frappe.throw(_("Cannot edit standard fields"))
  74. if not value:
  75. values = fieldname
  76. if isinstance(fieldname, string_types):
  77. try:
  78. values = json.loads(fieldname)
  79. except ValueError:
  80. values = {fieldname: ''}
  81. else:
  82. values = {fieldname: value}
  83. doc = frappe.db.get_value(doctype, name, ["parenttype", "parent"], as_dict=True)
  84. if doc and doc.parent and doc.parenttype:
  85. doc = frappe.get_doc(doc.parenttype, doc.parent)
  86. child = doc.getone({"doctype": doctype, "name": name})
  87. child.update(values)
  88. else:
  89. doc = frappe.get_doc(doctype, name)
  90. doc.update(values)
  91. doc.save()
  92. return doc.as_dict()
  93. @frappe.whitelist()
  94. def insert(doc=None):
  95. '''Insert a document
  96. :param doc: JSON or dict object to be inserted'''
  97. if isinstance(doc, string_types):
  98. doc = json.loads(doc)
  99. if doc.get("parent") and doc.get("parenttype"):
  100. # inserting a child record
  101. parent = frappe.get_doc(doc.get("parenttype"), doc.get("parent"))
  102. parent.append(doc.get("parentfield"), doc)
  103. parent.save()
  104. return parent.as_dict()
  105. else:
  106. doc = frappe.get_doc(doc).insert()
  107. return doc.as_dict()
  108. @frappe.whitelist()
  109. def insert_many(docs=None):
  110. '''Insert multiple documents
  111. :param docs: JSON or list of dict objects to be inserted in one request'''
  112. if isinstance(docs, string_types):
  113. docs = json.loads(docs)
  114. out = []
  115. if len(docs) > 200:
  116. frappe.throw(_('Only 200 inserts allowed in one request'))
  117. for doc in docs:
  118. if doc.get("parent") and doc.get("parenttype"):
  119. # inserting a child record
  120. parent = frappe.get_doc(doc.get("parenttype"), doc.get("parent"))
  121. parent.append(doc.get("parentfield"), doc)
  122. parent.save()
  123. out.append(parent.name)
  124. else:
  125. doc = frappe.get_doc(doc).insert()
  126. out.append(doc.name)
  127. return out
  128. @frappe.whitelist()
  129. def save(doc):
  130. '''Update (save) an existing document
  131. :param doc: JSON or dict object with the properties of the document to be updated'''
  132. if isinstance(doc, string_types):
  133. doc = json.loads(doc)
  134. doc = frappe.get_doc(doc).save()
  135. return doc.as_dict()
  136. @frappe.whitelist()
  137. def rename_doc(doctype, old_name, new_name, merge=False):
  138. '''Rename document
  139. :param doctype: DocType of the document to be renamed
  140. :param old_name: Current `name` of the document to be renamed
  141. :param new_name: New `name` to be set'''
  142. new_name = frappe.rename_doc(doctype, old_name, new_name, merge=merge)
  143. return new_name
  144. @frappe.whitelist()
  145. def submit(doc):
  146. '''Submit a document
  147. :param doc: JSON or dict object to be submitted remotely'''
  148. if isinstance(doc, string_types):
  149. doc = json.loads(doc)
  150. doc = frappe.get_doc(doc)
  151. doc.submit()
  152. return doc.as_dict()
  153. @frappe.whitelist()
  154. def cancel(doctype, name):
  155. '''Cancel a document
  156. :param doctype: DocType of the document to be cancelled
  157. :param name: name of the document to be cancelled'''
  158. wrapper = frappe.get_doc(doctype, name)
  159. wrapper.cancel()
  160. return wrapper.as_dict()
  161. @frappe.whitelist()
  162. def delete(doctype, name):
  163. '''Delete a remote document
  164. :param doctype: DocType of the document to be deleted
  165. :param name: name of the document to be deleted'''
  166. frappe.delete_doc(doctype, name, ignore_missing=False)
  167. @frappe.whitelist()
  168. def set_default(key, value, parent=None):
  169. """set a user default value"""
  170. frappe.db.set_default(key, value, parent or frappe.session.user)
  171. frappe.clear_cache(user=frappe.session.user)
  172. @frappe.whitelist()
  173. def make_width_property_setter(doc):
  174. '''Set width Property Setter
  175. :param doc: Property Setter document with `width` property'''
  176. if isinstance(doc, string_types):
  177. doc = json.loads(doc)
  178. if doc["doctype"]=="Property Setter" and doc["property"]=="width":
  179. frappe.get_doc(doc).insert(ignore_permissions = True)
  180. @frappe.whitelist()
  181. def bulk_update(docs):
  182. '''Bulk update documents
  183. :param docs: JSON list of documents to be updated remotely. Each document must have `docname` property'''
  184. docs = json.loads(docs)
  185. failed_docs = []
  186. for doc in docs:
  187. try:
  188. ddoc = {key: val for key, val in iteritems(doc) if key not in ['doctype', 'docname']}
  189. doctype = doc['doctype']
  190. docname = doc['docname']
  191. doc = frappe.get_doc(doctype, docname)
  192. doc.update(ddoc)
  193. doc.save()
  194. except:
  195. failed_docs.append({
  196. 'doc': doc,
  197. 'exc': frappe.utils.get_traceback()
  198. })
  199. return {'failed_docs': failed_docs}
  200. @frappe.whitelist()
  201. def has_permission(doctype, docname, perm_type="read"):
  202. '''Returns a JSON with data whether the document has the requested permission
  203. :param doctype: DocType of the document to be checked
  204. :param docname: `name` of the document to be checked
  205. :param perm_type: one of `read`, `write`, `create`, `submit`, `cancel`, `report`. Default is `read`'''
  206. # perm_type can be one of read, write, create, submit, cancel, report
  207. return {"has_permission": frappe.has_permission(doctype, perm_type.lower(), docname)}
  208. @frappe.whitelist()
  209. def get_password(doctype, name, fieldname):
  210. '''Return a password type property. Only applicable for System Managers
  211. :param doctype: DocType of the document that holds the password
  212. :param name: `name` of the document that holds the password
  213. :param fieldname: `fieldname` of the password property
  214. '''
  215. frappe.only_for("System Manager")
  216. return frappe.get_doc(doctype, name).get_password(fieldname)
  217. @frappe.whitelist()
  218. def get_js(items):
  219. '''Load JS code files. Will also append translations
  220. and extend `frappe._messages`
  221. :param items: JSON list of paths of the js files to be loaded.'''
  222. items = json.loads(items)
  223. out = []
  224. for src in items:
  225. src = src.strip("/").split("/")
  226. if ".." in src or src[0] != "assets":
  227. frappe.throw(_("Invalid file path: {0}").format("/".join(src)))
  228. contentpath = os.path.join(frappe.local.sites_path, *src)
  229. with open(contentpath, "r") as srcfile:
  230. code = frappe.utils.cstr(srcfile.read())
  231. if frappe.local.lang != "en":
  232. messages = frappe.get_lang_dict("jsfile", contentpath)
  233. messages = json.dumps(messages)
  234. code += "\n\n$.extend(frappe._messages, {})".format(messages)
  235. out.append(code)
  236. return out
  237. @frappe.whitelist(allow_guest=True)
  238. def get_time_zone():
  239. '''Returns default time zone'''
  240. return {"time_zone": frappe.defaults.get_defaults().get("time_zone")}