odoo/odoo#190218

Created by JavaScript, Jorge Pinna Puissant (jpp)
Merged at a5bb53d1a5717183d2b01726519033e1ca66ba74

Statuses:

Linked pull requests
label
odoo-dev:master-add_current_company_country_code-jpp
head
dca22fad8b10427301326cb7904ad60ad906f6db
merged
10 months ago by JavaScript, Aaron Bohy (aab)
odoo/odoo odoo/enterprise odoo/upgrade
master #190218 #75836 #75436 #6917

[IMP] web: add companies infos to the evaluation context

This commit adds a new object with the information of the companies, to
the evaluation context of Python expressions.

The object contains:
- multi_company: A boolean indicating whether the user has access to multiple companies.
- allowed_ids : The list of company IDs the user is allowed to connect to.
- active_ids: The list of company IDs the user is connected to (selected in the company switcher dropdown).
- active_id: The ID of the main company selected (the one highlighted in the company switcher dropdown and displayed in the navbar of the webclient).
- has(id|ids, 'property', value) : returns a boolean indicating whether there's a company with id in ids for which field matches the given value. Note that the properties of the companies are those sent by the server in the session info. If a new property is needed, the company function _get_session_info can be overridden. For example, the following code will add the company_code property:
py def _get_session_info(self, allowed_company_ids): res = super()._get_session_info(allowed_company_ids) res.update({ 'country_code': self.country_id.code }) return res

Some usage examples:

  • In this case, the field 'foo' will be visible if the user has access
    to more than one company:
    xml <field name="foo" invisible="not companies.multi_company"/>
  • In this case, the filed 'foo' will be visible if the user has
    activated one company with the country code 'PE':
    xml <field name="foo" invisible="not companies.has(companies.active_ids, 'country_code', 'PE')"/>
  • In this case, the field 'float_field' will be visible if the main
    company (the one show on the top bar), has the country code 'PE':
    xml <field name="float_field" invisible="not companies.has(companies.active_id, 'country_code', 'PE')"/>

  • In this case, the field 'child_ids' will be visible if the company of
    the current record has the country code 'PE'. Note that, to
    evaluate correctly this expression, the 'company_id' field
    should be in the view:
    xml <field name="child_ids" invisible="not companies.has(company_id, 'country_code', 'PE')"/>

part-of task-4250356