Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify the duplicate form in the database manager?

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?

like image 514
ChesuCR Avatar asked Nov 07 '22 17:11

ChesuCR


1 Answers

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.

like image 116
Ajmal JK Avatar answered Nov 12 '22 14:11

Ajmal JK