Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | 5eeef7f | 2014-11-24 14:16:51 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import frappe |
| 6 | |
| 7 | def load_address_and_contact(doc, key): |
| 8 | """Loads address list and contact list in `__onload`""" |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame^] | 9 | from frappe.geo.doctype.address.address import get_address_display |
Rushabh Mehta | 5eeef7f | 2014-11-24 14:16:51 +0530 | [diff] [blame] | 10 | |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame^] | 11 | 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 Mehta | 5eeef7f | 2014-11-24 14:16:51 +0530 | [diff] [blame] | 24 | |
| 25 | if doc.doctype != "Lead": |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame^] | 26 | 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 Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 36 | |
| 37 | def 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 Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 48 | 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 Ltd | 8b231f7 | 2016-10-18 17:30:24 +0530 | [diff] [blame] | 55 | return False |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 56 | |
| 57 | def get_permission_query_conditions_for_contact(user): |
| 58 | return get_permission_query_conditions("Contact") |
| 59 | |
| 60 | def get_permission_query_conditions_for_address(user): |
| 61 | return get_permission_query_conditions("Address") |
| 62 | |
| 63 | def 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 Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame^] | 69 | |
Nabin Hait | dace2b6 | 2015-10-01 13:54:46 +0530 | [diff] [blame] | 70 | elif not links.get("permitted_links"): |
| 71 | conditions = [] |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame^] | 72 | |
Nabin Hait | dace2b6 | 2015-10-01 13:54:46 +0530 | [diff] [blame] | 73 | # 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 Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame^] | 77 | |
Nabin Hait | dace2b6 | 2015-10-01 13:54:46 +0530 | [diff] [blame] | 78 | return "( " + " and ".join(conditions) + " )" |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 79 | |
| 80 | else: |
| 81 | conditions = [] |
| 82 | |
| 83 | for df in links.get("permitted_links"): |
| 84 | # like ifnull(customer, '')!='' or ifnull(supplier, '')!='' |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame^] | 85 | conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname)) |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 86 | |
| 87 | return "( " + " or ".join(conditions) + " )" |
| 88 | |
| 89 | def 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(): |
RobertSchouten | f28a5da | 2016-06-07 17:03:41 +0800 | [diff] [blame] | 96 | if df.options not in ("Customer", "Supplier", "Company", "Sales Partner"): |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 97 | 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 | } |