blob: a2e566f7ed18e91d25ae949d28f84430d49b3511 [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`"""
9 from erpnext.utilities.doctype.address.address import get_address_display
10
11 doc.get("__onload").addr_list = [a.update({"display": get_address_display(a)}) \
12 for a in frappe.get_all("Address",
13 fields="*", filters={key: doc.name},
14 order_by="is_primary_address desc, modified desc")]
15
16 if doc.doctype != "Lead":
17 doc.get("__onload").contact_list = frappe.get_all("Contact",
18 fields="*", filters={key: doc.name},
19 order_by="is_primary_contact desc, modified desc")
Anand Doshi89b8d112015-09-29 20:06:53 +053020
21def has_permission(doc, ptype, user):
22 links = get_permitted_and_not_permitted_links(doc.doctype)
23 if not links.get("not_permitted_links"):
24 # optimization: don't determine permissions based on link fields
25 return True
26
27 # True if any one is True or all are empty
28 names = []
29 for df in (links.get("permitted_links") + links.get("not_permitted_links")):
30 doctype = df.options
31 name = doc.get(df.fieldname)
32
33 names.append(name)
34
35 if name and frappe.has_permission(doctype, ptype, doc=name):
36 return True
37
38 if not any(names):
39 return True
40
41 else:
42 return False
43
44def get_permission_query_conditions_for_contact(user):
45 return get_permission_query_conditions("Contact")
46
47def get_permission_query_conditions_for_address(user):
48 return get_permission_query_conditions("Address")
49
50def get_permission_query_conditions(doctype):
51 links = get_permitted_and_not_permitted_links(doctype)
52
53 if not links.get("not_permitted_links"):
54 # when everything is permitted, don't add additional condition
55 return ""
Nabin Haitdace2b62015-10-01 13:54:46 +053056
57 elif not links.get("permitted_links"):
58 conditions = []
59
60 # when everything is not permitted
61 for df in links.get("not_permitted_links"):
62 # like ifnull(customer, '')='' and ifnull(supplier, '')=''
63 conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')=''".format(doctype=doctype, fieldname=df.fieldname))
64
65 return "( " + " and ".join(conditions) + " )"
Anand Doshi89b8d112015-09-29 20:06:53 +053066
67 else:
68 conditions = []
69
70 for df in links.get("permitted_links"):
71 # like ifnull(customer, '')!='' or ifnull(supplier, '')!=''
Nabin Haitdace2b62015-10-01 13:54:46 +053072 conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname))
Anand Doshi89b8d112015-09-29 20:06:53 +053073
74 return "( " + " or ".join(conditions) + " )"
75
76def get_permitted_and_not_permitted_links(doctype):
77 permitted_links = []
78 not_permitted_links = []
79
80 meta = frappe.get_meta(doctype)
81
82 for df in meta.get_link_fields():
RobertSchoutenf28a5da2016-06-07 17:03:41 +080083 if df.options not in ("Customer", "Supplier", "Company", "Sales Partner"):
Anand Doshi89b8d112015-09-29 20:06:53 +053084 continue
85
86 if frappe.has_permission(df.options):
87 permitted_links.append(df)
88 else:
89 not_permitted_links.append(df)
90
91 return {
92 "permitted_links": permitted_links,
93 "not_permitted_links": not_permitted_links
94 }