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 | |
Rushabh Mehta | 95439db | 2017-01-14 00:25:22 +0530 | [diff] [blame] | 37 | def set_default_role(doc, method): |
| 38 | '''Set customer, supplier, student based on email''' |
Rushabh Mehta | d14a6c0 | 2017-01-17 18:46:37 +0530 | [diff] [blame] | 39 | if frappe.flags.setting_role: |
| 40 | return |
Rushabh Mehta | 95439db | 2017-01-14 00:25:22 +0530 | [diff] [blame] | 41 | contact_name = frappe.get_value('Contact', dict(email_id=doc.email)) |
| 42 | if contact_name: |
| 43 | contact = frappe.get_doc('Contact', contact_name) |
| 44 | for link in contact.links: |
Rushabh Mehta | d14a6c0 | 2017-01-17 18:46:37 +0530 | [diff] [blame] | 45 | frappe.flags.setting_role = True |
Rushabh Mehta | 95439db | 2017-01-14 00:25:22 +0530 | [diff] [blame] | 46 | if link.link_doctype=='Customer': |
| 47 | doc.add_roles('Customer') |
| 48 | elif link.link_doctype=='Supplier': |
| 49 | doc.add_roles('Supplier') |
| 50 | elif frappe.get_value('Student', dict(student_email_id=doc.email)): |
| 51 | doc.add_roles('Student') |
| 52 | |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 53 | def has_permission(doc, ptype, user): |
| 54 | links = get_permitted_and_not_permitted_links(doc.doctype) |
| 55 | if not links.get("not_permitted_links"): |
| 56 | # optimization: don't determine permissions based on link fields |
| 57 | return True |
| 58 | |
| 59 | # True if any one is True or all are empty |
| 60 | names = [] |
| 61 | for df in (links.get("permitted_links") + links.get("not_permitted_links")): |
| 62 | doctype = df.options |
| 63 | name = doc.get(df.fieldname) |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 64 | names.append(name) |
| 65 | |
| 66 | if name and frappe.has_permission(doctype, ptype, doc=name): |
| 67 | return True |
| 68 | |
| 69 | if not any(names): |
| 70 | return True |
Serpent Consulting Services Pvt Ltd | 8b231f7 | 2016-10-18 17:30:24 +0530 | [diff] [blame] | 71 | return False |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 72 | |
| 73 | def get_permission_query_conditions_for_contact(user): |
| 74 | return get_permission_query_conditions("Contact") |
| 75 | |
| 76 | def get_permission_query_conditions_for_address(user): |
| 77 | return get_permission_query_conditions("Address") |
| 78 | |
| 79 | def get_permission_query_conditions(doctype): |
| 80 | links = get_permitted_and_not_permitted_links(doctype) |
| 81 | |
| 82 | if not links.get("not_permitted_links"): |
| 83 | # when everything is permitted, don't add additional condition |
| 84 | return "" |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 85 | |
Nabin Hait | dace2b6 | 2015-10-01 13:54:46 +0530 | [diff] [blame] | 86 | elif not links.get("permitted_links"): |
| 87 | conditions = [] |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 88 | |
Nabin Hait | dace2b6 | 2015-10-01 13:54:46 +0530 | [diff] [blame] | 89 | # when everything is not permitted |
| 90 | for df in links.get("not_permitted_links"): |
| 91 | # like ifnull(customer, '')='' and ifnull(supplier, '')='' |
| 92 | conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')=''".format(doctype=doctype, fieldname=df.fieldname)) |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 93 | |
Nabin Hait | dace2b6 | 2015-10-01 13:54:46 +0530 | [diff] [blame] | 94 | return "( " + " and ".join(conditions) + " )" |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 95 | |
| 96 | else: |
| 97 | conditions = [] |
| 98 | |
| 99 | for df in links.get("permitted_links"): |
| 100 | # like ifnull(customer, '')!='' or ifnull(supplier, '')!='' |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 101 | conditions.append("ifnull(`tab{doctype}`.`{fieldname}`, '')!=''".format(doctype=doctype, fieldname=df.fieldname)) |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 102 | |
| 103 | return "( " + " or ".join(conditions) + " )" |
| 104 | |
| 105 | def get_permitted_and_not_permitted_links(doctype): |
| 106 | permitted_links = [] |
| 107 | not_permitted_links = [] |
| 108 | |
| 109 | meta = frappe.get_meta(doctype) |
| 110 | |
| 111 | for df in meta.get_link_fields(): |
RobertSchouten | f28a5da | 2016-06-07 17:03:41 +0800 | [diff] [blame] | 112 | if df.options not in ("Customer", "Supplier", "Company", "Sales Partner"): |
Anand Doshi | 89b8d11 | 2015-09-29 20:06:53 +0530 | [diff] [blame] | 113 | continue |
| 114 | |
| 115 | if frappe.has_permission(df.options): |
| 116 | permitted_links.append(df) |
| 117 | else: |
| 118 | not_permitted_links.append(df) |
| 119 | |
| 120 | return { |
| 121 | "permitted_links": permitted_links, |
| 122 | "not_permitted_links": not_permitted_links |
| 123 | } |
Rushabh Mehta | 95439db | 2017-01-14 00:25:22 +0530 | [diff] [blame] | 124 | |
| 125 | def delete_contact_and_address(doctype, name): |
| 126 | for parenttype in ('Contact', 'Address'): |
| 127 | items = frappe.db.sql("""select parent from `tabDynamic Link` |
Rushabh Mehta | 8d39fd9 | 2017-01-16 13:06:07 +0530 | [diff] [blame] | 128 | where parenttype=%s and link_doctype=%s and link_name=%s""", |
Rushabh Mehta | 95439db | 2017-01-14 00:25:22 +0530 | [diff] [blame] | 129 | (parenttype, doctype, name)) |
| 130 | |
| 131 | for name in items: |
| 132 | doc = frappe.get_doc(parenttype, name) |
| 133 | if len(doc.links)==1: |
| 134 | doc.delete() |