I have for some time tried to find out how to do conditional view inheritance, that is, modify a view only if a condition is satisfied.
The inherited view was the following:
<record id="invoice_tree_inherit" model="ir.ui.view">
<field name="name">account.invoice.tree.inherit</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_tree" />
<field name="arch" type="xml">
<data>
<xpath expr="//tree" position="replace">
<tree create="true"
colors="blue:state == 'draft';black:state in ('proforma','proforma2','open');gray:state == 'cancel'"
string="Invoice" visible="false">
<field name="number" />
<field name="date_invoice" />
<field name="partner_id" groups="base.group_user" />
<field name="commercial_partner_id" invisible="0" />
<field name="reference" invisible="1" />
<field name="name" invisible="1" />
<field name="journal_id" invisible="1" />
<field name="period_id" invisible="1"
groups="account.group_account_user" />
<field name="currency_id" groups="base.group_multi_currency" />
<field name="amount_total" sum="Total Amount" />
<field name="amount_tax" sum="IVA" />
<field name="state" />
<field name="comment" />
<field name="type" invisible="context.get('type',True)" />
</tree>
</xpath>
</data>
</field>
</record>
but I couldn't find a satisfying way to make this inherit the view, only if a condition was satisfied. So I instead opted to take the route described (among other sites) here.
The code i made in my models.py was the following:
class Modulotest(models.Model):
_inherit = 'account.invoice'
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context={}, toolbar=False):
result = super(Modulotest, self).fields_view_get(cr, uid, view_id, view_type, context=context, toolbar=toolbar)
if view_type=='tree' and result['name']=='account.invoice.tree':
currUtente=self.pool.get('res.users').browse(cr,uid,uid,context)
if currUtente.alt_view==True:
result['arch']=self.getAltView()
return result
def getAltView(self):
return '''
<tree create="true" colors="blue:state == 'draft';black:state in ('proforma','proforma2','open');gray:state == 'cancel'" string="Fattura" visible="false">
<field name="number" modifiers="{"readonly": true}"/>
<field name="date_invoice" modifiers="{"readonly": [["state", "not in", ["draft"]]]}"/>
<field name="partner_id" on_change="1" modifiers="{"readonly": [["state", "not in", ["draft"]]], "required": true}" string="Cliente"/>
<field name="commercial_partner_id" invisible="1" modifiers="{"readonly": true, "tree_invisible": true}"/>
<field name="reference" invisible="1" modifiers="{"tree_invisible": true}"/>
<field name="name" invisible="1" modifiers="{"readonly": [["state", "not in", ["draft"]]], "tree_invisible": true}"/>
<field name="journal_id" invisible="1" modifiers="{"readonly": [["state", "not in", ["draft"]]], "required": true, "tree_invisible": true}"/>
<field name="period_id" invisible="1" modifiers="{"invisible": true, "readonly": [["state", "not in", ["draft"]]], "tree_invisible": true}"/>
<field name="currency_id" invisible="1" modifiers="{"invisible": true, "readonly": [["state", "not in", ["draft"]]], "required": true, "tree_invisible": true}"/>
<field name="amount_total" sum="Importo Totale" modifiers="{"readonly": true}"/>
<field name="amount_tax" sum="IVA" modifiers="{"readonly": true}"/>
<field name="state" modifiers="{"readonly": true}"/>
<field name="comment" modifiers="{}"/>
<field name="type" invisible="context.get('type',True)" modifiers="{"readonly": true, "tree_invisible": true}"/>
</tree>
''';
class UtenteEsteso(models.Model):
_inherit = 'res.users'
alt_view=fields.Boolean("Vista alternativa", default=False)
And this solution works just fine, but the fact that elements of the view are given in the model, is not something that I like at all. It breaks the MVC architectural pattern.
What I would like, is to retrived this string somehow from an xml file in the views folder, but so far, I have found no good way to import xml files (or files in general) from within the odoo API.
If someone knows how to do this, or knows of some alternative way I could achieve the same effect, without having elements of the view inside the model, I would be very grateful!