[IMP] core: add a neutralize CLI command

When debugging a database, it's sometimes desirable to neutralize it to
avoid undesired side effects like sending emails.

This commit adds a generic method `_neutralize` on models to neutralize
the current database. This method has to be overridden by modules models
that needs to be neutralized.

A new CLI command `neutralize`is now available that will permit to
neutralize a database from the command line before starting
investigations.

Pay attention that the neutralize operation cannot be reversed and should
only be used on a database copy.

Task id: 1818795

Part-of: odoo/odoo#67825
This commit is contained in:
Christophe Monniez
2022-02-01 09:54:07 +00:00
parent 17b196d2fc
commit 152310fe26
4 changed files with 89 additions and 0 deletions
+1
View File
@@ -14,3 +14,4 @@ from . import shell
from . import start
from . import populate
from . import tsconfig
from . import neutralize
+43
View File
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
import optparse
import sys
import odoo
from pathlib import Path
from . import Command
_logger = logging.getLogger(__name__)
class Neutralize(Command):
"""neutralize a database"""
def run(self, args):
parser = odoo.tools.config.parser
group = optparse.OptionGroup(parser, "Neutralize", "Neutralize the database specified by the `-d` argument.")
parser.add_option_group(group)
odoo.tools.config.parse_config(args)
dbname = odoo.tools.config['db_name']
if not dbname:
_logger.error('Neutralize command needs a database name. Use "-d" argument')
sys.exit(1)
registry = odoo.registry(dbname)
_logger.info('Starting database neutralization')
try:
with registry.cursor() as cr:
env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
for model in env.values():
model._neutralize()
with open(Path(__file__).parent / 'neutralize_watermarks.xml', 'rb') as f:
odoo.tools.convert_xml_import(cr, "__neutralize__", f)
except Exception:
_logger.error('An exception occured during neutralization.')
raise
_logger.info('Neutralization finished')
+39
View File
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<template t-name="neutralize_banner" inherit_id="web.layout">
<xpath expr="//body" position="inside">
<div>
<span style="text-align: center;
color: #FFFFFF;
background-color: #D0442C;
position: relative;
display: block;
font-size: 16px;">
This database is neutralized.
</span>
</div>
</xpath>
</template>
<template t-name="neutralize_ribbon" inherit_id="web.layout">
<xpath expr="//body" position="inside">
<div>
<span style="width: 400px;
top: 55px;
left: -100px;
font-size: 32px;
text-align: center;
padding: 10px;
line-height: 30px;
color: #f0f0f0;
transform: rotate(-45deg);
position: fixed;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
background: #D0442C;
opacity: 0.6;
pointer-events: none;
text-transform: uppercase;
z-index:9999">Neutralized</span>
</div>
</xpath>
</template>
</odoo>