5

I want to add some other field to this form that can be accessed from the Database Manager in Odoo

enter image description here

The data is sent to this controller:

@http.route('/web/database/duplicate', type='http', auth="none", methods=['POST'], csrf=False)
def duplicate(self, master_pwd, name, new_name):
    try:
        if not re.match(DBNAME_PATTERN, new_name):
            raise Exception(_('Invalid database name. Only alphanumerical characters, underscore, hyphen and dot are allowed.'))
        dispatch_rpc('db', 'duplicate_database', [master_pwd, name, new_name])
        return http.local_redirect('/web/database/manager')
    except Exception as e:
        error = "Database duplication error: %s" % (str(e) or repr(e))
        return self._render_template(error=error)

But the form is plain html, so I cannot inherit and modify any template:

<!-- Duplicate DB -->
<div class="modal fade o_database_duplicate" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
        <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Duplicate Database</h4>
        </div>
        <form id="form-duplicate-db" role="form" action="/web/database/duplicate" method="post">
            <div class="modal-body">
            {{ master_input() }}
            <div class="form-group">
                <label for="name" class="control-label">Database Name</label>
                <input id="name" type="text" name="name" class="form-control" required="required" readonly="readonly"/>
            </div>
            <div class="form-group">
                <label for="new_name" class="control-label">New Name</label>
                <input id="new_name" type="text" name="new_name" class="form-control" required="required" pattern="{{ pattern }}" title="Only alphanumerical characters, underscore, hyphen and dot are allowed"/>
            </div>
            </div>
            <div class="modal-footer">
            <input type="submit" value="Continue" class="btn btn-primary pull-right"/>
            </div>
        </form>
        </div>
    </div>
</div>

I found a python code where this html is rendered

class Database(http.Controller):

    def _render_template(self, **d):
        d.setdefault('manage',True)
        d['insecure'] = odoo.tools.config.verify_admin_password('admin')
        d['list_db'] = odoo.tools.config['list_db']
        d['langs'] = odoo.service.db.exp_list_lang()
        d['countries'] = odoo.service.db.exp_list_countries()
        d['pattern'] = DBNAME_PATTERN
        # databases list
        d['databases'] = []
        try:
            d['databases'] = http.db_list()
            d['incompatible_databases'] = odoo.service.db.list_db_incompatible(d['databases'])
        except odoo.exceptions.AccessDenied:
            monodb = db_monodb()
            if monodb:
                d['databases'] = [monodb]
        return env.get_template("database_manager.html").render(d)

Should I modify this code in order to use another template? Or is there another more appropriate solution?

Is modifying the original code directly in the web module the only way to achieve this? Is there a way to change all this behaviour by inheriting the web module?

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
  • 1
    At least it is calling/rendering the template in the controller. So you could try to write your own html page and load it as your template instead the original one. And overriding a controller is possible without changing the original code. Thanks by the way, i never looked into this and now im wondering why this page is not written as QWeb template. – CZoellner May 28 '19 at 08:33
  • @ChesuCR take a look at this application it's not stable for version 10.0 but you will have an idea. the problem is there is some code that work normal inheritance and there there some method that you need to monkey patch them. https://apps.odoo.com/apps/modules/10.0/db_description/ – Charif DZ May 28 '19 at 10:31

2 Answers2

1

Sure, you can done it by a custom module.First You have to define your own html template as per your need , and then have to rewrite the controller which render the html file.

You can refer the following code to rewrite the controller.

from odoo.addons.web.controllers.main import Database  # importing base controller class

class MyDatabase(Database):
    def _render_template(self, **d):
       d.setdefault('manage',True)
       d['insecure'] = odoo.tools.config.verify_admin_password('admin')
       d['list_db'] = odoo.tools.config['list_db']
       d['langs'] = odoo.service.db.exp_list_lang()
       d['countries'] = odoo.service.db.exp_list_countries()
       d['pattern'] = DBNAME_PATTERN
       #additional parameters if any
       d['databases'] = []
       try:
           d['databases'] = http.db_list()
           d['incompatible_databases'] = odoo.service.db.list_db_incompatible(d['databases'])
       except odoo.exceptions.AccessDenied:
           monodb = db_monodb()
           if monodb:
               d['databases'] = [monodb]

       return env.get_template("your_modified_template.html").render(d)  #render your modified html template.

If you have any doubts have a look in to the code of This module.

Ajmal JK
  • 813
  • 4
  • 14
0

Yes, you would need to overwrite the _render_template method of the web module. And also you would need to create your own HTML template for that and pass it for the rendering.

from odoo.addons.web.controllers.main import db_monodb, Database as DB, env
import jinja2

loader = jinja2.PackageLoader('odoo.addons.your_custom_module_name', "views")
env = jinja2.Environment(loader=loader, autoescape=True)
env.filters["json"] = json.dumps

class Database(DB):

def _render_template(self, **d):
    d.setdefault('manage',True)
    d['insecure'] = odoo.tools.config['admin_passwd'] == 'admin'
    d['list_db'] = odoo.tools.config['list_db']
    d['langs'] = odoo.service.db.exp_list_lang()
    d['countries'] = odoo.service.db.exp_list_countries()
    d['pattern'] = DBNAME_PATTERN
        # databases list
        d['databases'] = []
        try:
            d['databases'] = http.db_list()
        except odoo.exceptions.AccessDenied:
            monodb = db_monodb()
            if monodb:
                d['databases'] = [monodb]
        return env.get_template("your_html_template_file_name.html").render(d)

This is an example from v10. You can just copy and paste the method from the Odoo module of the version you are working on.

Sudhir Arya
  • 3,703
  • 3
  • 30
  • 43