25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

215 satır
6.4 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, json
  5. import frappe.utils
  6. import frappe.share
  7. import frappe.defaults
  8. import frappe.desk.form.meta
  9. from frappe.model.utils.user_settings import get_user_settings
  10. from frappe.permissions import get_doc_permissions
  11. from frappe import _
  12. @frappe.whitelist()
  13. def getdoc(doctype, name, user=None):
  14. """
  15. Loads a doclist for a given document. This method is called directly from the client.
  16. Requries "doctype", "name" as form variables.
  17. Will also call the "onload" method on the document.
  18. """
  19. if not (doctype and name):
  20. raise Exception('doctype and name required!')
  21. if not name:
  22. name = doctype
  23. if not frappe.db.exists(doctype, name):
  24. return []
  25. try:
  26. doc = frappe.get_doc(doctype, name)
  27. run_onload(doc)
  28. if not doc.has_permission("read"):
  29. frappe.flags.error_message = _('Insufficient Permission for {0}').format(frappe.bold(doctype + ' ' + name))
  30. raise frappe.PermissionError(("read", doctype, name))
  31. doc.apply_fieldlevel_read_permissions()
  32. # add file list
  33. get_docinfo(doc)
  34. except Exception:
  35. frappe.errprint(frappe.utils.get_traceback())
  36. frappe.msgprint(_('Did not load'))
  37. raise
  38. if doc and not name.startswith('_'):
  39. frappe.get_user().update_recent(doctype, name)
  40. doc.add_seen()
  41. frappe.response.docs.append(doc)
  42. @frappe.whitelist()
  43. def getdoctype(doctype, with_parent=False, cached_timestamp=None):
  44. """load doctype"""
  45. docs = []
  46. parent_dt = None
  47. # with parent (called from report builder)
  48. if with_parent:
  49. parent_dt = frappe.model.meta.get_parent_dt(doctype)
  50. if parent_dt:
  51. docs = get_meta_bundle(parent_dt)
  52. frappe.response['parent_dt'] = parent_dt
  53. if not docs:
  54. docs = get_meta_bundle(doctype)
  55. frappe.response['user_settings'] = get_user_settings(parent_dt or doctype)
  56. if cached_timestamp and docs[0].modified==cached_timestamp:
  57. return "use_cache"
  58. frappe.response.docs.extend(docs)
  59. def get_meta_bundle(doctype):
  60. bundle = [frappe.desk.form.meta.get_meta(doctype)]
  61. for df in bundle[0].fields:
  62. if df.fieldtype=="Table":
  63. bundle.append(frappe.desk.form.meta.get_meta(df.options, not frappe.conf.developer_mode))
  64. return bundle
  65. @frappe.whitelist()
  66. def get_docinfo(doc=None, doctype=None, name=None):
  67. if not doc:
  68. doc = frappe.get_doc(doctype, name)
  69. if not doc.has_permission("read"):
  70. raise frappe.PermissionError
  71. frappe.response["docinfo"] = {
  72. "attachments": get_attachments(doc.doctype, doc.name),
  73. "communications": _get_communications(doc.doctype, doc.name),
  74. 'versions': get_versions(doc),
  75. "assignments": get_assignments(doc.doctype, doc.name),
  76. "permissions": get_doc_permissions(doc),
  77. "shared": frappe.share.get_users(doc.doctype, doc.name),
  78. "rating": get_feedback_rating(doc.doctype, doc.name)
  79. }
  80. def get_attachments(dt, dn):
  81. return frappe.get_all("File", fields=["name", "file_name", "file_url", "is_private"],
  82. filters = {"attached_to_name": dn, "attached_to_doctype": dt})
  83. def get_versions(doc):
  84. return frappe.get_all('Version', filters=dict(ref_doctype=doc.doctype, docname=doc.name),
  85. fields=['name', 'owner', 'creation', 'data'], limit=10, order_by='creation desc')
  86. @frappe.whitelist()
  87. def get_communications(doctype, name, start=0, limit=20):
  88. doc = frappe.get_doc(doctype, name)
  89. if not doc.has_permission("read"):
  90. raise frappe.PermissionError
  91. return _get_communications(doctype, name, start, limit)
  92. def _get_communications(doctype, name, start=0, limit=20):
  93. communications = get_communication_data(doctype, name, start, limit)
  94. for c in communications:
  95. if c.communication_type=="Communication":
  96. c.attachments = json.dumps(frappe.get_all("File",
  97. fields=["file_url", "is_private"],
  98. filters={"attached_to_doctype": "Communication",
  99. "attached_to_name": c.name}
  100. ))
  101. elif c.communication_type=="Comment" and c.comment_type=="Comment":
  102. c.content = frappe.utils.markdown(c.content)
  103. return communications
  104. def get_communication_data(doctype, name, start=0, limit=20, after=None, fields=None,
  105. group_by=None, as_dict=True):
  106. '''Returns list of communications for a given document'''
  107. if not fields:
  108. fields = '''name, communication_type,
  109. communication_medium, comment_type,
  110. content, sender, sender_full_name, creation, subject, delivery_status, _liked_by,
  111. timeline_doctype, timeline_name,
  112. reference_doctype, reference_name,
  113. link_doctype, link_name,
  114. rating, "Communication" as doctype'''
  115. conditions = '''communication_type in ("Communication", "Comment", "Feedback")
  116. and (
  117. (reference_doctype=%(doctype)s and reference_name=%(name)s)
  118. or (
  119. (timeline_doctype=%(doctype)s and timeline_name=%(name)s)
  120. and (
  121. communication_type="Communication"
  122. or (
  123. communication_type="Comment"
  124. and comment_type in ("Created", "Updated", "Submitted", "Cancelled", "Deleted")
  125. )))
  126. )'''
  127. if after:
  128. # find after a particular date
  129. conditions+= ' and creation > {0}'.format(after)
  130. if doctype=='User':
  131. conditions+= ' and not (reference_doctype="User" and communication_type="Communication")'
  132. communications = frappe.db.sql("""select {fields}
  133. from tabCommunication
  134. where {conditions} {group_by}
  135. order by creation desc limit %(start)s, %(limit)s""".format(
  136. fields = fields, conditions=conditions, group_by=group_by or ""),
  137. { "doctype": doctype, "name": name, "start": frappe.utils.cint(start), "limit": limit },
  138. as_dict=as_dict)
  139. return communications
  140. def get_assignments(dt, dn):
  141. cl = frappe.db.sql("""select name, owner, description from `tabToDo`
  142. where reference_type=%(doctype)s and reference_name=%(name)s and status="Open"
  143. order by modified desc limit 5""", {
  144. "doctype": dt,
  145. "name": dn
  146. }, as_dict=True)
  147. return cl
  148. @frappe.whitelist()
  149. def get_badge_info(doctypes, filters):
  150. filters = json.loads(filters)
  151. doctypes = json.loads(doctypes)
  152. filters["docstatus"] = ["!=", 2]
  153. out = {}
  154. for doctype in doctypes:
  155. out[doctype] = frappe.db.get_value(doctype, filters, "count(*)")
  156. return out
  157. def run_onload(doc):
  158. doc.set("__onload", frappe._dict())
  159. doc.run_method("onload")
  160. def get_feedback_rating(doctype, docname):
  161. """ get and return the latest feedback rating if available """
  162. rating= frappe.get_all("Communication", filters={
  163. "reference_doctype": doctype,
  164. "reference_name": docname,
  165. "communication_type": "Feedback"
  166. }, fields=["rating"], order_by="creation desc", as_list=True)
  167. if not rating:
  168. return 0
  169. else:
  170. return rating[0][0]