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

View File

@ -1,7 +1,7 @@
In order to use this module properly, each environment should have their own encryption key 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 :: Development environment ::
[options] [options]
@ -21,4 +21,3 @@ Production environment ::
encryption_key_dev=XXX encryption_key_dev=XXX
encryption_key_preprod=YYY encryption_key_preprod=YYY
encryption_key_prod=ZZZ encryption_key_prod=ZZZ

View File

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