blob: fddb1f7cb5e24008016bd346213550b2e69a8a9c [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehta5eeef7f2014-11-24 14:16:51 +05302# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5import frappe
6
7def load_address_and_contact(doc, key):
8 """Loads address list and contact list in `__onload`"""
Rushabh Mehtab92087c2017-01-13 18:53:11 +05309 from frappe.geo.doctype.address.address import get_address_display
Rushabh Mehta5eeef7f2014-11-24 14:16:51 +053010
Rushabh Mehtab92087c2017-01-13 18:53:11 +053011 address_list = [frappe.get_value('Address', a.parent, '*')
12 for a in frappe.get_all('Dynamic Link', fields='parent',
13 filters=dict(parenttype='Address', link_doctype=doc.doctype, link_name=doc.name))]
14
15 address_list = [a.update({"display": get_address_display(a)})
16 for a in address_list]
17
18 address_list = sorted(address_list,
19 lambda a, b:
20 (int(a.is_primary_address - b.is_primary_address)) or
21 (1 if a.modified - b.modified else 0))
22
23 doc.set_onload('addr_list', address_list)
Rushabh Mehta5eeef7f2014-11-24 14:16:51 +053024
25 if doc.doctype != "Lead":
Rushabh Mehtab92087c2017-01-13 18:53:11 +053026 contact_list = [frappe.get_value('Contact', a.parent, '*')
27 for a in frappe.get_all('Dynamic Link', fields='parent',
28 filters=dict(parenttype='Contact', link_doctype=doc.doctype, link_name=doc.name))]
29
30 contact_list = sorted(contact_list,
31 lambda a, b:
32 (int(a.is_primary_contact - b.is_primary_contact)) or
33 (1 if a.modified - b.modified else 0))
34
35 doc.set_onload('contact_list', contact_list)
Anand Doshi89b8d112015-09-29 20:06:53 +053036
37def has_permission(doc, ptype, user):
38 links = get_permitted_and_not_permitted_links(doc.doctype)
39 if not links.get("not_permitted_links"):
40 # optimization: don't determine permissions based on link fields
41 return True
42
43 # True if any one is True or all are empty
44 names = []
45 for df in (links.get("permitted_links") + links.get("not_permitted_links")):
46 doctype = df.options
47 name = doc.get(df.fieldname)
Anand Doshi89b8d112015-09-29 20:06:53 +053048 names.append(name)
49
50 if name and frappe.has_permission(doctype, ptype, doc=name):
51 return True
52
53 if not any(names):
54 return True
Serpent Consulting Services Pvt Ltd8b231f72016-10-18 17:30:24 +053055 return False
Anand Doshi89b8d112015-09-29 20:06:53 +053056
57def get_permission_query_conditions_for_contact(user):
58 return get_permission_query_conditions("Contact")
59
60def get_permission_query_conditions_for_address(user):
61 return get_permission_query_conditions("Address")
62
63def get_permission_query_conditions(doctype):
64 links = get_permitted_and_not_permitted_links(doctype)
65
66 if not links.get("not_permitted_links"):
67 # when everything is permitted, don't add additional condition
68 return ""
Rushabh Mehtab92087c2017-01-13 18:53:11 +053069
Nabin Haitdace2b62015-10-01 13:54:46 +053070 elif not links.get("permitted_links"):
71 conditions = []
Rushabh Mehtab92087c2017-01-13 18:53:11 +053072
Nabin Haitdace2b62015-10-01 13:54:46 +053073 # when everything is not permitted
74 for df in links.get("not_permitted_links"):
75 # like ifnull(customer, '')='' and ifnull(supplier, '')=''
76 conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')=''".format(doctype=doctype, fieldname=df.fieldname))
Rushabh Mehtab92087c2017-01-13 18:53:11 +053077
Nabin Haitdace2b62015-10-01 13:54:46 +053078 return "( " + " and ".join(conditions) + " )"
Anand Doshi89b8d112015-09-29 20:06:53 +053079
80 else:
81 conditions = []
82
83 for df in links.get("permitted_links"):
84 # like ifnull(customer, '')!='' or ifnull(supplier, '')!=''
Rushabh Mehtab92087c2017-01-13 18:53:11 +053085 conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname))
Anand Doshi89b8d112015-09-29 20:06:53 +053086
87 return "( " + " or ".join(conditions) + " )"
88
89def get_permitted_and_not_permitted_links(doctype):
90 permitted_links = []
91 not_permitted_links = []
92
93 meta = frappe.get_meta(doctype)
94
95 for df in meta.get_link_fields():
RobertSchoutenf28a5da2016-06-07 17:03:41 +080096 if df.options not in ("Customer", "Supplier", "Company", "Sales Partner"):
Anand Doshi89b8d112015-09-29 20:06:53 +053097 continue
98
99 if frappe.has_permission(df.options):
100 permitted_links.append(df)
101 else:
102 not_permitted_links.append(df)
103
104 return {
105 "permitted_links": permitted_links,
106 "not_permitted_links": not_permitted_links
107 }