kartik
April 9, 2019, 11:14am
1
Hello all,
I am trying to override the below whitelisted function.
"title": self.name,
"products_as_list": cint(frappe.db.get_single_value('Products Settings', 'products_as_list'))
})
if self.slideshow:
context.update(get_slideshow(self))
return context
@frappe.whitelist(allow_guest=True)
def get_product_list_for_group(product_group=None, start=0, limit=10, search=None):
if product_group:
item_group = frappe.get_cached_doc('Item Group', product_group)
if item_group.is_group:
# return child item groups if the type is of "Is Group"
return get_child_groups_for_list_in_html(item_group, start, limit, search)
child_groups = ", ".join(['"' + frappe.db.escape(i[0]) + '"' for i in get_child_groups(product_group)])
# base query
query = """select I.name, I.item_name, I.item_code, I.route, I.image, I.website_image, I.thumbnail, I.item_group,
I have added it in hooks in override_whitelisted_methods also. But it still seems to run the core function still instead of the custom one. I have overriden others which are working fine, only having problem with this one. Checked the path of both the functions and it is also correct.
Any pointers what might be the issue?
Nimrod
April 11, 2019, 12:57pm
2
I’m having the same problem. I’m trying to override the following whitelisted function:
# For license information, please see license.txt
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
class PartyType(Document):
pass
@frappe.whitelist()
def get_party_type(doctype, txt, searchfield, start, page_len, filters):
cond = ''
if filters and filters.get('account'):
account_type = frappe.db.get_value('Account', filters.get('account'), 'account_type')
cond = "and account_type = '%s'" % account_type
return frappe.db.sql("""select name from `tabParty Type`
where `{key}` LIKE %(txt)s {cond}
order by name limit %(start)s, %(page_len)s"""
.format(key=searchfield, cond=cond), {
'txt': '%' + txt + '%',
I’ve added the following to my custom app’s hooks.py
:
override_whitelisted_methods = {
"erpnext.setup.doctype.party_type.party_type.get_party_type": "my_app.my_module.utils.get_party_type"
}
The path to the method has already been tested to be correct. So it cannot just be a wrong path.
Not sure if same thing happens to all other whitelisted methods elsewhere in the ERPNext or Frappe apps. Hopefully someone has an idea what’s going on. Or at least an idea where to check for the problem.
kartik
April 11, 2019, 3:06pm
3
Other whitelisted methods are working as mentioned in the screenshot above.
Regarding your issue, I believe this function is being called by a link field.
If so you can write a function in your custom app and set use set_query for that link field.
Nimrod
April 11, 2019, 3:38pm
4
Yes, that’s right - the method is called by a link field really. I’ll try your suggestion. Thanks.
vijaywm
November 5, 2019, 12:11pm
5
Did the first override get_product_info_for_website
work for you when loading product in website? Checking frappe/frappe/handler.py seems like the hooks are never called for website methods as there is no “cmd” in the request
@kartik , does override works for you in v12?
kartik
November 8, 2019, 9:23am
7
Sorry. No idea. I faced this issue on v11.
Is this work on versio12?
If I’m not mistaken.
This will work on version 12 but it will work only if method get call directly with url such as
http://site1.local:8000/api/method/erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_receiptttp://site1.local:8000/api/method
or
frappe.call({
'method': 'erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_receipt'
})
But it won’t work with method that got call indirectly such as.
make_purchase_receipt: function() {
frappe.model.open_mapped_doc({
method: "erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_receipt",
frm: cur_frm
})
},
Which got call through frappe.model.open_mapped_doc
method.
Overriding happen in execute_cmd
function which you can find it here.
def execute_cmd(cmd, from_async=False):
"""execute a request as python module"""
for hook in frappe.get_hooks("override_whitelisted_methods", {}).get(cmd, []):
# override using the first hook
cmd = hook
break
# via server script
if run_server_script_api(cmd):
return None
try:
method = get_attr(cmd)
except Exception as e:
if frappe.local.conf.developer_mode:
raise e
else:
frappe.respond_as_web_page(title='Invalid Method', html='Method not found',
indicator_color='red', http_status_code=404)
return
This file has been truncated. show original
@pipech Thank you so much
here is my used case . can you help me to solve ?
create_opportunity: function () {
frappe.model.open_mapped_doc({
method: "erpnext.crm.doctype.lead.lead.make_opportunity",
frm: cur_frm
})
},
whitelisted function is
@frappe.whitelist ()
def make_opportunity(source_name, target_doc=None):
def set_missing_values(source, target):
_set_missing_values(source, target)
target_doc = get_mapped_doc("Lead", source_name,
{"Lead": {
"doctype": "Opportunity",
"field_map": {
"campaign_name": "campaign",
"doctype": "opportunity_from",
"name": "party_name",
"lead_name": "contact_display",
"company_name": "customer_name",
"email_id": "contact_email",
"mobile_no": "contact_mobile"
//i want to add one line here "customer":"customer"
}
}}, target_doc, set_missing_values)
return target_doc
pipech
May 14, 2020, 10:06am
11
I think you need to override JS method then redirect it to your custom python method.
// TaxesAndTotalsExtend is just variable name
// erpnext.taxes_and_totals is function contain function you want to override
const TaxesAndTotalsExtend = erpnext.taxes_and_totals.extend({
// calculate_item_values is function you want to override
calculate_item_values: () => {
console.log('Hello from extend taxes and total extend !!');
},
});
// this tell current form to use this override script
$.extend(
cur_frm.cscript,
new TaxesAndTotalsExtend({frm: cur_frm}),
);
1 Like
In order to override server side whitelisted method, i did the following.
This works for v11.
In hooks.py add this
override_whitelisted_methods = {
"frappe.abc.xyz.main_method": "custom_app.xyz.custom_method_name"
}
In you custom_app/xyz file, add this method as whitelisted method
@frappe.whitelist()
def custom_method_name():
main_method_code()
add_custom_logic_here()
2 Likes
May I know how to add in hooks.py whether to override new frappe.whitelist function in sales invoice item.
Hi all,
What if the frappe whitelist method in class.
how to override that?
For example:
you can override the doctype class itself
1 Like
Thank you for your response.
I tried override from doctype_js = {
}
in hooks.
But is not reflected, What I am trying is child table, name crm_note.
CRMNote is a class that is inherited by other doctypes. Eg: https://github.com/frappe/erpnext/blob/f09e2130a1a3ca999247f89f76c20efcc003b450/erpnext/crm/doctype/prospect/prospect.py#L14
Lets say you want to override the add_note function for prospect.
You can override the doctype class of Prospect and add a function with the same name and your desired function and whitelist it.
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
1 Like