4
votes

I want to filter a domain with a function instead of a variable in order to show only some registers in the view. I have done this:

class SaleOrderExt(models.Model):
    _inherit = ['sale.order']

    @api.multi
    def custom_funct_date(self):
        my_date = ... (some stuff)
        return my_date

Then, in the view, I have filtered the domain:

<?xml version="1.0"?>
<openerp>
  <data>
    <record id='action_menu_custom_date' model='ir.actions.act_window'>
      <field name="name">This is a test</field>
      <field name="res_model">sale.order</field>
      <field name="view_type">form</field>
      <field name="view_mode">tree,form</field>
      <field name="domain">[('date_order','>=',custom_funct_date)]</field>
    </record>
  </data>
</openerp>

But this is giving me an error:

ValueError: "name 'custom_funct_date' is not defined" while evaluating
u"[('date_order','>=',custom_funct_date)]" 
3

3 Answers

7
votes

instead of using ir.actions.act_window use ir.actions.server using server action you can call your function and return a action to open your model using your computed domain.

    <record id="action_menu_custom_dat" model="ir.actions.server">
        <field name="name">Your Action</field>
        <field name="model_id" ref="sale.model_sale_order"/>
        <field name="state">code</field>
        <field name="code">action = self.custom_funct_date(cr, uid, context=context)</field>
        <field eval="True" name="condition"/>
    </record>

and in your model change the method to:

     def custom_funct_date(self, cr, uid, context=None):
            # print "make sure that this action is called from th server action "
            # compute you date
            my_date = ... (some stuff)
            tree_id = self.env.ref("modul_name.view_tree_id")
            form_id = self.env.ref("modul_name.view_form_id")
            return {
                'type': 'ir.actions.act_window',
                'name': 'This is a test',
                'view_type': 'form',
                'view_mode': 'tree,form',
                'res_model': 'sale.order',
                'domain': [('date_order','>=',my_date)],
                # if you don't want to specify form for example
                # (False, 'form') just pass False 
                'views': [(tree_id.id, 'tree'), (form_id.id, 'form')],
                'target': 'current',
                'context': context,
            }
2
votes

In the domain, you need to pass a field not a name of a function.

To do this you can create a compute field. Assign your method to the computed field.

After you specified a search attribute for you compute field.

Odoo Example:

upper_name = field.Char(compute='_compute_upper', search='_search_upper')

def _search_upper(self, operator, value):
    if operator == 'like':
        operator = 'ilike'
    return [('name', operator, value)]

For you case:

class SaleOrderExt(models.Model):
    _inherit = ['sale.order']

    your_date = fields.Datetime(string="Computed date", compute="compute_your_date", search="_search_date")

    @api.depends('date')
    def compute_your_date(self):
        my_date = ... (some stuff)
        return my_date


    def _search_date(self, operator, value):
        return [('date', operator, value)]

Edit:

Your xml code must be like this

<?xml version="1.0"?>
<openerp>
  <data>
    <record id='action_menu_custom_date' model='ir.actions.act_window'>
      <field name="name">This is a test</field>
      <field name="res_model">sale.order</field>
      <field name="view_type">form</field>
      <field name="view_mode">tree,form</field>
      <field name="domain">[('date_order','>=',your_date)]</field>
    </record>
  </data>
</openerp>
0
votes

Many time since the last answer, but it can be usefull to other ones.

I'd have similar problem with res.partner inherited view and this is how I resolved it. In this case, I need the user to be the partner who accesses their data or the admin to show the partner's data in the notebook section.

Model:

class Partner(models.Model):
    _inherit = 'res.partner'

 @api.multi
    def _get_domain(self):
    # Remember that self returns a recordset, so we need to iterate over that
    # dominio (domain in english) -boolean- store if the user is the partner or the admin
        for rec in self:
            rec.dominio =  True if self.id == self.env.user.partner_id.id or self.env.user.has_group('cnae.group_cnae_manager') else False

    dominio = fields.Boolean(string='Dominio', compute=_get_domain)

XML:

... ir.ui.view header record stuff ...

<!--  Hide notebook to other users -->
<xpath expr="//notebook" position="attributes">
    <attribute name="attrs">{'invisible':[('dominio','=',False)]}</attribute>
</xpath>