如何在odoo 10中覆盖TransientModel的fields_view_get?

时间:2017-01-27 13:47:17

标签: odoo odoo-10

我已经做到了,并且在旧的odoo版本中这种方式有效! 无法在日志文件中看到此“kecske”信号。没有错误消息。如果我在超级之前写了一些代码,它就没有任何效果。

有什么想法吗?这是正确的方式吗?

class DemoWizard(models.TransientModel):
    _name = 'demo.wizard'

    name = fields.Char(string='Name')

    @api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        log = logging.getLogger('demo.wizard.fields_view_get()')
        log.debug('kecske')
        return super(DemoWizard,self).fields_view_get(view_id, view_type, toolbar, submenu)

1 个答案:

答案 0 :(得分:1)

这是来自Odoo10的来源。该文件位于匿名插件中。 odoo/addons/anonymization/wizard/anonymize_wizard.py。注意调用super()和使用关键字参数作为位置参数。

除此之外,您的代码看起来是正确的。

在您的示例中,您使用其他技术初始化日志记录。尝试按如下方式初始化记录器。

log = logging.getLogger(__name__)
log.info("My Log Message")

或用于调试。

log.debug("My debug message")

info,debug,warning,error可用于记录日志消息的不同严重程度。

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
    state = self.env['ir.model.fields.anonymization']._get_global_state()
    step = self.env.context.get('step', 'new_window')
    res = super(IrModelFieldsAnonymizeWizard, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
    eview = etree.fromstring(res['arch'])
    placeholder = eview.xpath("group[@name='placeholder1']")
    if len(placeholder):
        placeholder = placeholder[0]
        if step == 'new_window' and state == 'clear':
            # clicked in the menu and the fields are not anonymized: warn the admin that backuping the db is very important
            placeholder.addnext(etree.Element('field', {'name': 'msg', 'colspan': '4', 'nolabel': '1'}))
            placeholder.addnext(etree.Element('newline'))
            placeholder.addnext(etree.Element('label', {'string': 'Warning'}))
            eview.remove(placeholder)
        elif step == 'new_window' and state == 'anonymized':
            # clicked in the menu and the fields are already anonymized
            placeholder.addnext(etree.Element('newline'))
            placeholder.addnext(etree.Element('field', {'name': 'file_import', 'required': "1"}))
            placeholder.addnext(etree.Element('label', {'string': 'Anonymization file'}))
            eview.remove(placeholder)
        elif step == 'just_anonymized':
            # we just ran the anonymization process, we need the file export field
            placeholder.addnext(etree.Element('newline'))
            placeholder.addnext(etree.Element('field', {'name': 'file_export'}))
            # we need to remove the button:
            buttons = eview.xpath("button")
            for button in buttons:
                eview.remove(button)
            # and add a message:
            placeholder.addnext(etree.Element('field', {'name': 'msg', 'colspan': '4', 'nolabel': '1'}))
            placeholder.addnext(etree.Element('newline'))
            placeholder.addnext(etree.Element('label', {'string': 'Result'}))
            # remove the placeholer:
            eview.remove(placeholder)
        elif step == 'just_desanonymized':
            # we just reversed the anonymization process, we don't need any field
            # we need to remove the button
            buttons = eview.xpath("button")
            for button in buttons:
                eview.remove(button)
            # and add a message
            placeholder.addnext(etree.Element('field', {'name': 'msg', 'colspan': '4', 'nolabel': '1'}))
            placeholder.addnext(etree.Element('newline'))
            placeholder.addnext(etree.Element('label', {'string': 'Result'}))
            # remove the placeholer:
            eview.remove(placeholder)
        else:
            raise UserError(_("The database anonymization is currently in an unstable state. Some fields are anonymized,"
                              " while some fields are not anonymized. You should try to solve this problem before trying to do anything else."))
        res['arch'] = etree.tostring(eview)
    return res