odoo-ide / pycharm-odoo

PyCharm plugin for Odoo
https://plugins.jetbrains.com/plugin/13499-odoo
Other
41 stars 5 forks source link

Type hint for XML-ID strings #452

Open jcfernandez-890825 opened 5 months ago

jcfernandez-890825 commented 5 months ago

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 commented 5 months ago

Hi @jcfernandez-890825,

We already have:

Here are my ideas for external ids:

jcfernandez-890825 commented 5 months ago

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 commented 5 months ago

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:

The implementation will be more challenging, though.

jcfernandez-890825 commented 5 months ago

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.