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

Merge menu_item_rename and user_group_rename into same module #46

Merged
merged 6 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion base_xml_rename/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Base XML Rename
===============
This module allows to rename UI elements using XML data files in modules.

.. contents:: Table of Contents

Context
-------
Renaming a menu in Odoo directly through the web interface is not a good idea.
Expand All @@ -12,8 +14,23 @@ Renaming a menu in Odoo directly through the web interface is not a good idea.

The same applies for renaming other objects such as user groups.

Module Design
-------------
A method ``rename`` is added to every model.

This method takes 3 mandatory parameters:

* ``ref``: the xml reference of the record.
* ``lang``: the language of the term.
* ``value``: the new term.

Optionaly, a ``field`` parameter can be supplied, in case the field is not ``name``.

Usage
-----
The same syntax can be used in XML to rename any kind of object.

Here are 2 examples for renaming menu items and user groups.

Renaming a Menu
~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -65,7 +82,7 @@ After loading the XML, the group is named ``Comptable``.
Known Issues
~~~~~~~~~~~~
If you reload translations with ``Overwrite Existing Terms`` checked, the terms loaded with XML
will not be reloaded automatically.
will not be reloaded automatically. You will need to update the modules containing these XML files).

Contributors
------------
Expand Down
2 changes: 1 addition & 1 deletion base_xml_rename/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from . import (
base,
ir_ui_menu,
res_groups,
)
49 changes: 49 additions & 0 deletions base_xml_rename/models/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# © 2019 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import logging
from odoo import api, models
from odoo.exceptions import ValidationError


_logger = logging.getLogger(__name__)


def is_lang_installed(env: 'Environment', lang: str):
return lang in dict(env['res.lang'].get_installed())


class Base(models.AbstractModel):

_inherit = 'base'

@api.model
def rename(self, ref, lang, value, field='name'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ça me parait limite de rajouter ça dans base. En fait, c'est le nom de la fonction qui me parait assez générique pour que potentiellement un autre module implement la même fonction.
Qu'est ce que tu en penses ?
Potentiellement, faire de la composition plutôt que de l'héritage. Ça pourrait réduire les risques.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

en effet, ça pourrait être un mixin

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""Rename the record to rename.

:param ref: the XML ID of the record to rename
:param lang: the language of the term
:param value: the new name for the record
"""
if not is_lang_installed(self.env, lang):
_logger.debug(
'Skip renaming the record {ref} for the language {lang}. '
'The language is not installed.'
.format(ref=ref, lang=lang)
)
return

_logger.info(
'Renaming the record {ref} with the label `{value}` '
'for the language {lang}.'
.format(ref=ref, lang=lang, value=value)
)
record = self.env.ref(ref)
if record._name != self._name:
raise ValidationError(
'The XML ID {ref} does not reference a record of model {model}. '
'It references a record of model {record_model}'
.format(ref=ref, model=self._name, record_model=record._name)
)

record.with_context(lang=lang).write({field: value})
10 changes: 0 additions & 10 deletions base_xml_rename/models/common.py

This file was deleted.

44 changes: 9 additions & 35 deletions base_xml_rename/models/ir_ui_menu.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,22 @@
# © 2019 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import logging
from odoo import api, models
from odoo.exceptions import ValidationError
from .common import is_lang_installed, rename_record


_logger = logging.getLogger(__name__)
from .base import is_lang_installed


class IrUiMenu(models.Model):

_inherit = 'ir.ui.menu'

@api.model
def rename(self, menu_ref, lang, value):
"""Rename the menu item.

:param menu_ref: the XML ID of the menu item
:param lang: the language of the menu entry
:param value: the new name for the menu entry
"""
if not is_lang_installed(self.env, lang):
_logger.debug(
'Skip renaming menu item {menu_ref} for the language {lang}. '
'The language is not installed.'
.format(menu_ref=menu_ref, lang=lang)
)
return
def rename(self, ref, lang, value, field='name'):
super().rename(ref, lang, value, field)

_logger.info(
'Renaming menu item {menu_ref} with the label `{value}` '
'for the language {lang}.'
.format(menu_ref=menu_ref, lang=lang, value=value)
menu = self.env.ref(ref)
should_update_action_name = (
is_lang_installed(self.env, lang) and
menu.action and field == 'name'
)
menu = self.env.ref(menu_ref)
if menu._name != self._name:
raise ValidationError(
'The XML ID {} does not reference a menu item.'
.format(menu_ref)
)

rename_record(menu, lang, value)

if menu.action:
rename_record(menu.action, lang, value)
if should_update_action_name:
menu.action.with_context(lang=lang).name = value
45 changes: 0 additions & 45 deletions base_xml_rename/models/res_groups.py

This file was deleted.