Skip to content

Type hint for XML-ID strings #452

@jcfernandez-890825

Description

@jcfernandez-890825

I'm making this helper module

# -*- coding: utf-8 -*-
from ast import literal_eval

from odoo import api, models
from odoo.osv import expression


class CustomHelper(models.AbstractModel):
    # Private attributes
    _name = 'custom.helper'
    _description = 'Custom Helper'

    # Default methods

    # Fields declaration

    # Compute and search fields, in the same order of fields declaration

    # Constraints and onchanges

    # CRUD methods (and name_get, name_search, ...) overrides

    # Action methods

    # Business methods
    @api.model
    def str2dct(self, dct, key):
        """
        :type key: str
        :type dct: dict
        """
        val = dct.get(key)
        if isinstance(val, str):
            val = literal_eval(val)  # type: dict or list
            dct[key] = val

    @api.model
    def list_recordset(self, rs, action):
        """
        :type action: str
        :type rs: models.BaseModel
        """
        act_dct = rs.env["ir.actions.actions"]._for_xml_id(action)  # type: dict

        if len(rs) > 1:
            self.str2dct(act_dct, 'domain')
            domain = [act_dct.get('domain', []), [('id', 'in', rs.ids)]]
            act_dct['domain'] = expression.AND(domain)
        elif len(rs) == 1:
            act_dct['res_id'] = rs.id
            views = act_dct['views']  # type: list[tuple[int, str]]
            for i, view_tpl in enumerate(views):
                view_id, view_mode = view_tpl
                if view_mode == 'form':
                    views.insert(0, views.pop(i))
                    break
        else:
            act_dct = {'type': 'ir.actions.act_window_close'}

        self.str2dct(act_dct, 'context')

        return act_dct

Could we add a new type hint when we want to specify that a parament/variable must have an Odoo XML-ID string?

self.env['custom.helper'].list_recordset(self, 'account.action_move_journal_line')

So if the XML-ID is not correctly typed, account.action_move_journal_linesssss should be account.action_move_journal_line we get a warning?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions