Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type hint for XML-ID strings #452

Open
jcfernandez-890825 opened this issue Jan 23, 2024 · 4 comments
Open

Type hint for XML-ID strings #452

jcfernandez-890825 opened this issue Jan 23, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@jcfernandez-890825
Copy link

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?

@trinhanhngoc trinhanhngoc added the enhancement New feature or request label Jan 23, 2024
@trinhanhngoc
Copy link
Member

Hi @jcfernandez-890825,

We already have:

  • odoo.model.xxx for recordsets
  • odoo.values.xxx for recordset dict values

Here are my ideas for external ids:

  • odoo.xid for external ids
  • odoo.xid.xxx for external ids of model xxx

@jcfernandez-890825
Copy link
Author

Here are my ideas for external ids:

  • odoo.xid for external ids
  • odoo.xid.xxx for external ids of model xxx

Awesome, this feature will be great !!!

Could we have another one for field iterators? set, list, tuple, ...

@trinhanhngoc
Copy link
Member

Here are my ideas for external ids:

  • odoo.xid for external ids
  • odoo.xid.xxx for external ids of model xxx

Awesome, this feature will be great !!!

Could we have another one for field iterators? set, list, tuple, ...

I also want to have a flexible type hints for field names. Here are my ideas:

  • odoo.fname.xxx for field name of model xxx
  • list[odoo.fname.xxx] for list of field names
  • ...

The implementation will be more challenging, though.

@jcfernandez-890825
Copy link
Author

Here are my ideas for external ids:

  • odoo.xid for external ids
  • odoo.xid.xxx for external ids of model xxx

Awesome, this feature will be great !!!
Could we have another one for field iterators? set, list, tuple, ...

I also want to have a flexible type hints for field names. Here are my ideas:

  • odoo.fname.xxx for field name of model xxx
  • list[odoo.fname.xxx] for list of field names
  • ...

The implementation will be more challenging, though.

This will be great too. Can't wait to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants