Route_options in list view

I am trying to use route_options in list view for custom doctype (QTC Manufacturing Logging). The purpose is to only show the specific docs for login by the recorder field.

The code in qtc_manufacturing_logging_list.js. …copied from

frappe.listview_settings['QTC Manufacturing Logging'] = {
    onload: function (me) {
        // this is not true as frappe.route_options is empty dict
        // if (!frappe.route_options) {
        if (!frappe.route_options || !frappe.route_options.keys) {
            frappe.route_options = {
                "recorder": frappe.session.user,
            };
        }
    },
};

However, there are something look not right. Could anyone give me some help…

  • if (!frappe.route_options) was never executed as frappe.route_options is an empty dict when debugging in browser.

  • the route_options seems not working right. It is shown as a pre-loaded filter rather than directly filter out the data in route.
    see the pictures below, a doc created by Administrator (as recorder) in the first one. In the 2nd one, a common user (zh…) logged in and the doc is not shown. But if the filter for recorder is cleared, the doc could still be shown. Is that right?

User: Administrator

User: zh… normal user

Okay, going through the example in todo.py. I understood now it is the permission defined todo.py and invoked in hooks.py that takes effects.

todo.py

    # NOTE: todo is viewable if either owner or assigned_to or System Manager in roles
    def on_doctype_update():
        frappe.db.add_index("ToDo", ["reference_type", "reference_name"])

    def get_permission_query_conditions(user):
        if not user: user = frappe.session.user

        if "System Manager" in frappe.get_roles(user):
            return None
        else:
            return """(tabToDo.owner = {user} or tabToDo.assigned_by = {user})"""\
                .format(user=frappe.db.escape(user))

    def has_permission(doc, user):
        if "System Manager" in frappe.get_roles(user):
            return True
        else:
            return doc.owner==user or doc.assigned_by==user

hooks.py

    permission_query_conditions = {
        "Event": "frappe.desk.doctype.event.event.get_permission_query_conditions",
        "ToDo": "frappe.desk.doctype.todo.todo.get_permission_query_conditions",
        "User": "frappe.core.doctype.user.user.get_permission_query_conditions",
        "Note": "frappe.desk.doctype.note.note.get_permission_query_conditions",
        "Kanban Board": "frappe.desk.doctype.kanban_board.kanban_board.get_permission_query_conditions",
        "Contact": "frappe.contacts.address_and_contact.get_permission_query_conditions_for_contact",
        "Address": "frappe.contacts.address_and_contact.get_permission_query_conditions_for_address",
        "Communication": "frappe.core.doctype.communication.communication.get_permission_query_conditions_for_communication",
        "Workflow Action": "frappe.workflow.doctype.workflow_action.workflow_action.get_permission_query_conditions"
    }

    has_permission = {
        "Event": "frappe.desk.doctype.event.event.has_permission",
        "ToDo": "frappe.desk.doctype.todo.todo.has_permission",
        "User": "frappe.core.doctype.user.user.has_permission",
        "Note": "frappe.desk.doctype.note.note.has_permission",
        "Kanban Board": "frappe.desk.doctype.kanban_board.kanban_board.has_permission",
        "Contact": "frappe.contacts.address_and_contact.has_permission",
        "Address": "frappe.contacts.address_and_contact.has_permission",
        "Communication": "frappe.core.doctype.communication.communication.has_permission",
        "Workflow Action": "frappe.workflow.doctype.workflow_action.workflow_action.has_permission",
        "File": "frappe.core.doctype.file.file.has_permission"
    }

Reply to my own question in case of someone else has similar doubt.