Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

150 righe
4.5 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # License: GNU General Public License v3. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. def load_address_and_contact(doc, key):
  6. """Loads address list and contact list in `__onload`"""
  7. from frappe.geo.doctype.address.address import get_address_display
  8. filters = [
  9. ["Dynamic Link", "link_doctype", "=", doc.doctype],
  10. ["Dynamic Link", "link_name", "=", doc.name],
  11. ["Dynamic Link", "parenttype", "=", "Address"],
  12. ]
  13. address_list = frappe.get_all("Address", filters=filters, fields=["*"])
  14. address_list = [a.update({"display": get_address_display(a)})
  15. for a in address_list]
  16. address_list = sorted(address_list,
  17. lambda a, b:
  18. (int(a.is_primary_address - b.is_primary_address)) or
  19. (1 if a.modified - b.modified else 0), reverse=True)
  20. doc.set_onload('addr_list', address_list)
  21. contact_list = []
  22. if doc.doctype != "Lead":
  23. filters = [
  24. ["Dynamic Link", "link_doctype", "=", doc.doctype],
  25. ["Dynamic Link", "link_name", "=", doc.name],
  26. ["Dynamic Link", "parenttype", "=", "Contact"],
  27. ]
  28. contact_list = frappe.get_all("Contact", filters=filters, fields=["*"])
  29. contact_list = sorted(contact_list,
  30. lambda a, b:
  31. (int(a.is_primary_contact - b.is_primary_contact)) or
  32. (1 if a.modified - b.modified else 0), reverse=True)
  33. doc.set_onload('contact_list', contact_list)
  34. def has_permission(doc, ptype, user):
  35. links = get_permitted_and_not_permitted_links(doc.doctype)
  36. if not links.get("not_permitted_links"):
  37. # optimization: don't determine permissions based on link fields
  38. return True
  39. # True if any one is True or all are empty
  40. names = []
  41. for df in (links.get("permitted_links") + links.get("not_permitted_links")):
  42. doctype = df.options
  43. name = doc.get(df.fieldname)
  44. names.append(name)
  45. if name and frappe.has_permission(doctype, ptype, doc=name):
  46. return True
  47. if not any(names):
  48. return True
  49. return False
  50. def get_permission_query_conditions_for_contact(user):
  51. return get_permission_query_conditions("Contact")
  52. def get_permission_query_conditions_for_address(user):
  53. return get_permission_query_conditions("Address")
  54. def get_permission_query_conditions(doctype):
  55. links = get_permitted_and_not_permitted_links(doctype)
  56. if not links.get("not_permitted_links"):
  57. # when everything is permitted, don't add additional condition
  58. return ""
  59. elif not links.get("permitted_links"):
  60. conditions = []
  61. # when everything is not permitted
  62. for df in links.get("not_permitted_links"):
  63. # like ifnull(customer, '')='' and ifnull(supplier, '')=''
  64. conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')=''".format(doctype=doctype, fieldname=df.fieldname))
  65. return "( " + " and ".join(conditions) + " )"
  66. else:
  67. conditions = []
  68. for df in links.get("permitted_links"):
  69. # like ifnull(customer, '')!='' or ifnull(supplier, '')!=''
  70. conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname))
  71. return "( " + " or ".join(conditions) + " )"
  72. def get_permitted_and_not_permitted_links(doctype):
  73. permitted_links = []
  74. not_permitted_links = []
  75. meta = frappe.get_meta(doctype)
  76. for df in meta.get_link_fields():
  77. if df.options not in ("Customer", "Supplier", "Company", "Sales Partner"):
  78. continue
  79. if frappe.has_permission(df.options):
  80. permitted_links.append(df)
  81. else:
  82. not_permitted_links.append(df)
  83. return {
  84. "permitted_links": permitted_links,
  85. "not_permitted_links": not_permitted_links
  86. }
  87. def delete_contact_and_address(doctype, docname):
  88. for parenttype in ('Contact', 'Address'):
  89. items = frappe.db.sql_list("""select parent from `tabDynamic Link`
  90. where parenttype=%s and link_doctype=%s and link_name=%s""",
  91. (parenttype, doctype, docname))
  92. for name in items:
  93. doc = frappe.get_doc(parenttype, name)
  94. if len(doc.links)==1:
  95. doc.delete()
  96. def filter_dynamic_link_doctypes(doctype, txt, searchfield, start, page_len, filters):
  97. if not txt: txt = ""
  98. txt = txt.lower()
  99. txt = "%%%s%%" % (txt)
  100. filters.update({
  101. "parent": ("like", txt)
  102. })
  103. doctypes = frappe.db.get_all("DocField", filters=filters, fields=["parent"],
  104. order_by="parent asc", distinct=True, as_list=True)
  105. filters.pop("parent")
  106. filters.update({
  107. "dt": ("not in", [d[0] for d in doctypes]),
  108. "dt": ("like", txt),
  109. })
  110. _doctypes = frappe.db.get_all("Custom Field", filters=filters, fields=["dt"],
  111. order_by="dt asc", as_list=True)
  112. all_doctypes = doctypes + _doctypes
  113. return sorted(all_doctypes, key=lambda item: item[0])