ERPNext Foundation ERPNext Cloud Chat Blog Discuss Frappé* Donate

Issue trying to call whitelisted PY function in Custom App

customization
api
custom-app

#1

I have some Doctypes in a Custom App. One of them will work as a Production Order, and I need to call the functions stop_unstop and make_stock_entry from Production Order.
How can I do this?

In the case that I have to create my own functions and then call them from my Custom Doctype, how should I do it?

First I tried this JS function:

frappe.ui.form.on("Work Order", "refresh", function (frm)
{
    if (frm.doc.docstatus === 1)
        custom_app.stop_work_order(frm, status);
});

custom_app = {
    stop_work_order: function (frm, status)
    {
        frappe.call({
            method: "custom_app.orders.doctype.work_order.work_order.stop_unstop",
            args: {
                work_order: frm.doc.name,
                status: status
            },
            callback: function (r) {
                if (r.message) {
                    frm.set_value("status", r.message);
                    frm.reload_doc();
            }}
        });
    }
}

Then I tried this:

frappe.ui.form.on("Work Order", "refresh", function (frm)
{
    if (frm.doc.docstatus === 1)
        custom_app.stop_work_order(frm, status);
});

custom_app = {
    stop_work_order: function (frm, status)
    {
        frappe.call({
            type: "POST",
            args: {
                work_order: frm.doc.name,
                status: status
            },
            url: "/api/method/work_order/POST?run_method=stop_unstop",
            callback: function (r) {
                if (r.message) {
                    frm.set_value("status", r.message);
                    frm.reload_doc();
            }}
        });
    }
}

The PY code is this:

@frappe.whitelist()
def stop_unstop(work_order, status):
    w_order = frappe.get_doc("Work Order", work_order)
    w_order.update_status(status)
    w_order.update_planned_qty()
    frappe.msgprint(_("Work Order has been {0}").format(status))
    w_order.notify_update()

    return w_order.status

Always I received the message error 404 (NOT FOUND).

P.S.: I already visited the following links without success:


#2

Check the dotted path of the whitelisted function that you write in the frappe call.
Also check indentation in the code. And make sure there’s an__init__.py file in the directory in which you are writing your .py file.
If the whitelisted function is written in doctype py file in the custom app, usually the dotted path is like below:

[custom_app].[custom_app].doctype.[doctype_name].[doctype_name].[whitelisted_function]

In you case it should this, I presume:

custom_app.custom_app.doctype.work_order.work_order.stop_unstop


#3

You can open bench console and try to import your method.
e.g.
from custom_app.orders.doctype.work_order.work_order import stop_unstop

This will give you proper result whether the above path is working or not, or you can add the proper path according to your directory structure.
REF :


#4

Thanks for you response @kartik.

I reviewed the identation and have written correctly the dotted path of the whitelisted function. Also I added some lines to the __init__.py file.

This is the content of __init__.py file:

from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document

@frappe.whitelist()
def stop_unstop(work_order, status):
        w_order = frappe.get_doc("Work Order", work_order)
        w_order.update_status(status)
        w_order.update_planned_qty()
        frappe.msgprint(_("Work Order has been {0}).format(status))
        w_order.notify_update()

        return w_order.status

The dotted path of the whitelisted function is the following:

custom_app.custom_app.doctype.work_order.work_order.stop_unstop

I have done all this and keeps returning the same error 404.


#5

Thanks for you response @abhijit_kumbhar.

I have tried to import the method and it does not return anything, I assume that since there is no problem it does not return anything.


#6

If you have written your function in__init__.py file and the location of the said file is

frappe-bench/apps/custom_app/custom_app/doctype/work_order/

then the path of whitelisted function would be

custom_app.custom_app.doctype.work_order.stop_unstop