-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge menu_item_rename and user_group_rename into same module (#46)
* Merge menu_item_rename and user_group_rename into same module
- Loading branch information
Showing
23 changed files
with
189 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
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. | ||
|
||
* We rapidly loose track of what was modified. | ||
* The menu names are not changed in test environments (except with replication of the prod). | ||
* The changes are lost if someone forces the override of translations. | ||
|
||
The same applies for renaming other objects such as user groups. | ||
|
||
Module Design | ||
------------- | ||
A mixin ``xml.rename.mixin`` is added. | ||
|
||
This mixin has a method ``rename`` which 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``. | ||
|
||
The mixin can be added to any model. | ||
|
||
For now, the module adds the mixin to ir.ui.menu and res.groups. | ||
|
||
Usage | ||
----- | ||
Here are 2 examples for renaming menu items and user groups. | ||
|
||
Renaming a Menu | ||
~~~~~~~~~~~~~~~ | ||
Inside an xml file: | ||
|
||
* Add a `function` node with model="ir.ui.menu". | ||
* Inside the `function` node, you must set 3 `value` nodes. | ||
|
||
These nodes must have type="char" and respectively contain the following data: | ||
|
||
1. The XML ID of the menu item | ||
2. The languge code | ||
3. The new label to set | ||
|
||
Here is an example to rename the `Settings` menu to `Administration`. | ||
|
||
.. code-block:: XML | ||
<function name="rename" model="ir.ui.menu"> | ||
<value type="char">base.menu_administration</value> | ||
<value type="char">en_US</value> | ||
<value type="char">Administration</value> | ||
</function> | ||
Here is the result after loading the module containg the XML file. | ||
|
||
.. image:: static/description/admin_menu.png | ||
|
||
Renaming a User Group | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
Here is an example of renaming a user group. This works the same way as renaming a menu item. | ||
|
||
.. code-block:: XML | ||
<function name="rename" model="res.groups"> | ||
<value type="char">account.group_account_user</value> | ||
<value type="char">fr_FR</value> | ||
<value type="char">Comptable</value> | ||
</function> | ||
Before loading the XML, the group is named ``Montrer les fonctions de comptabilité complète`` in french. | ||
|
||
.. image:: static/description/group_before.png | ||
|
||
After loading the XML, the group is named ``Comptable``. | ||
|
||
.. image:: static/description/group_after.png | ||
|
||
Known Issues | ||
~~~~~~~~~~~~ | ||
If you reload translations with ``Overwrite Existing Terms`` checked, the terms loaded with XML | ||
will not be reloaded automatically. You will need to update the modules containing these XML files). | ||
|
||
Contributors | ||
------------ | ||
* Numigi (tm) and all its contributors (https://bit.ly/numigiens) |
File renamed without changes.
File renamed without changes.
6 changes: 5 additions & 1 deletion
6
user_group_rename/__init__.py → base_xml_rename/models/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
# © 2019 Numigi (tm) and all its contributors (https://bit.ly/numigiens) | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
from . import models | ||
from . import ( | ||
ir_ui_menu, | ||
res_groups, | ||
xml_rename_mixin, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# © 2019 Numigi (tm) and all its contributors (https://bit.ly/numigiens) | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
from odoo import api, models | ||
from .xml_rename_mixin import is_lang_installed | ||
|
||
|
||
class IrUiMenu(models.Model): | ||
|
||
_inherit = ('ir.ui.menu', 'xml.rename.mixin') | ||
_name = 'ir.ui.menu' | ||
|
||
@api.model | ||
def rename(self, ref, lang, value, field='name'): | ||
super().rename(ref, lang, value, field) | ||
|
||
menu = self.env.ref(ref) | ||
should_update_action_name = ( | ||
is_lang_installed(self.env, lang) and | ||
menu.action and field == 'name' | ||
) | ||
if should_update_action_name: | ||
menu.action.with_context(lang=lang).name = value |
8 changes: 8 additions & 0 deletions
8
user_group_rename/tests/__init__.py → base_xml_rename/models/res_groups.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
# © 2019 Numigi (tm) and all its contributors (https://bit.ly/numigiens) | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
from odoo import models | ||
|
||
|
||
class ResGroups(models.Model): | ||
|
||
_inherit = ('res.groups', 'xml.rename.mixin') | ||
_name = 'res.groups' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# © 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 XMLRenameMixin(models.AbstractModel): | ||
|
||
_name = 'xml.rename.mixin' | ||
_description = 'Mixin For Renaming Records Through XML' | ||
|
||
@api.model | ||
def rename(self, ref, lang, value, field='name'): | ||
"""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}) |
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.