pycharm-odoo
pycharm-odoo copied to clipboard
Type hint for XML-ID strings
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?
Hi @jcfernandez-890825,
We already have:
odoo.model.xxxfor recordsetsodoo.values.xxxfor recordset dict values
Here are my ideas for external ids:
odoo.xidfor external idsodoo.xid.xxxfor external ids of modelxxx
Here are my ideas for external ids:
odoo.xidfor external idsodoo.xid.xxxfor external ids of modelxxx
Awesome, this feature will be great !!!
Could we have another one for field iterators? set, list, tuple, ...
Here are my ideas for external ids:
odoo.xidfor external idsodoo.xid.xxxfor external ids of modelxxxAwesome, 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.xxxfor field name of modelxxxlist[odoo.fname.xxx]for list of field names- ...
The implementation will be more challenging, though.
Here are my ideas for external ids:
odoo.xidfor external idsodoo.xid.xxxfor external ids of modelxxxAwesome, 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.xxxfor field name of modelxxxlist[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.