[MIG] server_environment_data_encryption: migration to 13.0 (pre-commit)

This commit is contained in:
Thomas Binsfeld 2020-10-05 14:04:48 +02:00 committed by Florian da Costa
parent e15218c1bf
commit b8dd717307
4 changed files with 31 additions and 46 deletions

View File

@ -1,8 +1,8 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Server Environment Data Encryption",
"version": "12.0.1.0.0",
"development_status": 'Alpha',
"version": "13.0.1.0.0",
"development_status": "Alpha",
"category": "Tools",
"website": "https://github.com/OCA/server-env",
"author": "Akretion, Odoo Community Association (OCA)",

View File

@ -1,12 +1,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import logging
from odoo import api, models, _
from odoo.exceptions import ValidationError
from odoo.tools.config import config
from lxml import etree
from odoo.osv.orm import setup_modifiers
from odoo import _, api, models
from odoo.exceptions import ValidationError
from odoo.tools.config import config
_logger = logging.getLogger(__name__)
@ -17,7 +18,7 @@ class ServerEnvMixin(models.AbstractModel):
def _compute_server_env_from_default(self, field_name, options):
""" First return database encrypted value then default value """
self.ensure_one()
encrypted_data_name = "%s,%s" % (self._name, self.id)
encrypted_data_name = "{},{}".format(self._name, self.id)
env = self.env.context.get("environment", None)
vals = (
self.env["encrypted.data"]
@ -27,9 +28,7 @@ class ServerEnvMixin(models.AbstractModel):
if vals.get(field_name):
self[field_name] = vals[field_name]
else:
return super()._compute_server_env_from_default(
field_name, options
)
return super()._compute_server_env_from_default(field_name, options)
def _inverse_server_env(self, field_name):
"""
@ -41,7 +40,7 @@ class ServerEnvMixin(models.AbstractModel):
env = self.env.context.get("environment", None)
for record in self:
if record[is_editable_field]:
encrypted_data_name = "%s,%s" % (record._name, record.id)
encrypted_data_name = "{},{}".format(record._name, record.id)
values = encrypted_data_obj._encrypted_read_json(
encrypted_data_name, env=env
)
@ -57,8 +56,7 @@ class ServerEnvMixin(models.AbstractModel):
# We don't know which action we are using... take default one
action = self.get_formview_action()
else:
action = self.env["ir.actions.act_window"].browse(
action_id).read()[0]
action = self.env["ir.actions.act_window"].browse(action_id).read()[0]
action["view_mode"] = "form"
action["res_id"] = self.id
views_form = []
@ -83,13 +81,9 @@ class ServerEnvMixin(models.AbstractModel):
)
button_div += "{}".format(button)
button_div += "</div>"
alert_string = _("Modify values for {} environment").format(
current_env
)
alert_string = _("Modify values for {} environment").format(current_env)
alert_type = (
current_env == config.get("running_env")
and "alert-info"
or "alert-warning"
current_env == config.get("running_env") and "alert-info" or "alert-warning"
)
elem = etree.fromstring(
"""
@ -117,20 +111,18 @@ class ServerEnvMixin(models.AbstractModel):
def _update_form_view_from_env(self, arch, view_type):
if view_type != "form":
return arch
current_env = self.env.context.get("environment") or config.get(
"running_env"
)
current_env = self.env.context.get("environment") or config.get("running_env")
# Important to keep this list sorted. It makes sure the button to
# switch environment will always be in the same order. (more user
# friendly) and the test would fail without it as the order could
# change randomly and the view would then also change randomly
other_environments = sorted([
key[15:]
for key, val in config.options.items()
if key.startswith("encryption_key_")
and val
and key[15:] != current_env
])
other_environments = sorted(
[
key[15:]
for key, val in config.options.items()
if key.startswith("encryption_key_") and val and key[15:] != current_env
]
)
if not current_env:
raise ValidationError(
@ -143,18 +135,14 @@ class ServerEnvMixin(models.AbstractModel):
node = doc.xpath("//sheet")
if node:
node = node[0]
elem = self._get_extra_environment_info_div(
current_env, other_environments
)
elem = self._get_extra_environment_info_div(current_env, other_environments)
node.insert(0, elem)
if current_env != config.get("running_env"):
self._set_readonly_form_view(doc)
arch = etree.tostring(doc, pretty_print=True, encoding="unicode")
else:
_logger.error(
"Missing sheet for form view on object {}".format(self._name)
)
_logger.error("Missing sheet for form view on object {}".format(self._name))
return arch
@api.model
@ -162,10 +150,7 @@ class ServerEnvMixin(models.AbstractModel):
self, view_id=None, view_type="form", toolbar=False, submenu=False
):
res = super().fields_view_get(
view_id=view_id,
view_type=view_type,
toolbar=toolbar,
submenu=submenu,
view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu,
)
res["arch"] = self._update_form_view_from_env(res["arch"], view_type)
return res

View File

@ -1,7 +1,7 @@
In order to use this module properly, each environment should have their own encryption key
and the production environment should have the keys of all environments.
and the production environment should have the keys of all environments.
Example :
Example :
Development environment ::
[options]
@ -21,4 +21,3 @@ Production environment ::
encryption_key_dev=XXX
encryption_key_preprod=YYY
encryption_key_prod=ZZZ

View File

@ -1,11 +1,11 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.addons.data_encryption.tests.common import CommonDataEncrypted
from pathlib import Path
from odoo.addons.data_encryption.tests.common import CommonDataEncrypted
class TestServerEnvDataEncrypted(CommonDataEncrypted):
def test_dynamic_view_current_env(self):
self.maxDiff = None
self.set_new_key_env("prod")
@ -19,14 +19,15 @@ class TestServerEnvDataEncrypted(CommonDataEncrypted):
self.assertEqual(res_xml, expected_xml)
def test_dynamic_view_other_env(self):
self.maxDiff = None
self.set_new_key_env("prod")
self.set_new_key_env("preprod")
mixin_obj = self.env["server.env.mixin"]
base_path = Path(__file__).parent / "fixtures" / "base.xml"
xml = base_path.read_text()
res_xml = mixin_obj.with_context(
environment="prod"
)._update_form_view_from_env(xml, "form")
res_xml = mixin_obj.with_context(environment="prod")._update_form_view_from_env(
xml, "form"
)
expected_xml_path = Path(__file__).parent / "fixtures" / "res2.xml"
expected_xml = expected_xml_path.read_text()
self.assertEqual(res_xml, expected_xml)