1
votes

in odoo9 I override the search_read method. The super method works ok. With the data returned I want to make a filter, the filter is on the context, the value was asigned on the click of the button comming from the view.

    <button name="status_instalacion" string="Instalación" type="action" icon="fa-wrench  fa-2x"  context="{'stage_id' : 1, 'current_id': active_id}"/> 

The problem occurs when I query the context in the search_read method. It exists but doesn't have the values I placed

context on click of button:

self._context {u'lang': u'en_US', u'stage_id': 1, u'tz': False, u'uid': 1, u'current_id': 40, u'tipo_validacion': u'Sistemas Cr\xedticos', u'sistema_critico': u'AGUA'}

the stage_id is the value I want

context on read_search:

self._context {u'lang': u'en_US', u'bin_size': True, u'tipo_validacion': u'Sistemas Cr\xedticos', u'tz': False, u'uid': 1, u'active_test': False, u'sistema_critico': u'AGUA'}

as you can see the 'stage_id' value is missing

Tried also assigning the value to a property of the class, but the value never changes it is always the initial value.

from logging import getLogger
from openerp import api, fields, models

_logger = getLogger(__name__)


class MgmtsystemSistemasEquipos(models.Model):
    """ Equipos."""

    _name = 'mgmtsystem.sistemas.equipos'
    dmy = 99  # ---> this value never changes

    def dummy(self):       # ---> tried calling a function. not work
        return self.dmy

    def set_dummy(self, id):      # ----> set the value
        self.dmy = id or self.dmy

    codigo = fields.Char(
        string=u'Código',
        help=u"Código equipo",
        required=True,
        size=30)
    name = fields.Char(
        string=u'Nombre equipo',
        required=True,
        readonly=False,
        index=True,
        help="Nombre corto equipo",
        size=30)
    stage_id = fields.Many2one(
        'mgmtsystem.action.stage',
        'Fase',
        default=_default_stage,
        readonly=True)


    @api.multi
    def status_instalacion(self):

        import pudb
        pu.db

        # save value to variable dmy to retrieve later
        id = self._context.get('stage_id')
        self.set_dummy(id)



    @api.model
    def search_read(
            self, domain=None, fields=None, offset=0,
            limit=None, order=None):

        import pudb
        pu.db

        # here the variable allways has the original value (99)
        current_stage_id = self.dmy
        current_stage_id = self.dummy()
        current_stage_id = getattr(self, dmy)


        res = super(MgmtsystemSistemasEquipos, self).search_read(
            domain, fields, offset, limit, order)

        current_id = res[0]['id']

        valid_protocols_ids = self._get_ids(
            current_stage_id, current_id,
            'mgmtsystem_equipos_protocolos',
            'mgmtsystem_equipos_protocolos_rel',
            'protocolo_id')

        # # remove ids
        res[0]['protocolos_ids'] = valid_protocols_ids
        res[0]['informes_ids'] = valid_informes_ids
        res[0]['anexos_ids'] = valid_anexos_ids

        return res


    # @api.multi
    def _get_ids(self, current_stage_id, current_id, model, model_rel, field_rel):

        import pudb
        pu.db

        # in this method the value of the variable is allways the original
        current_stage_id = self.dummy()

        sql = """ select a.id from
            %s as a
            join %s as b
            on a.id = b.%s where b.equipo_id = %s
            and a.stage_id = %s; """ % (model, model_rel, field_rel,
                                        current_id, current_stage_id)

        import psycopg2

        try:
            self.env.cr.execute(sql)
        except psycopg2.ProgrammingError, ex:
            message = 'Error trying to download data from server. \n {0} \n {1}'.format(ex.pgerror, sql)
            _logger.info(message)
            return False

        rows = self.env.cr.fetchall()
        list_of_ids = []

        for row in rows:
            list_of_ids.append(row[0])

        return list_of_ids

I don't know Python very well, and thats the cause of my misunderstanding of how to read the value of the variable.

But then again, Why is the context modified in the search_read method?.

Thank you.

1

1 Answers

0
votes

You should try following.

@api.model
def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None):

    import pudb
    pu.db

    # Here you need to get the value from the context.
    current_stage_id = self._context.get('stage_id', getattr(self, dmy))

    res = super(MgmtsystemSistemasEquipos, self).search_read(domain=domain, fields=fields, offset=offset, limit=limit, order=order)

    current_id = res[0]['id']

    valid_protocols_ids = self._get_ids(
        current_stage_id, current_id,
        'mgmtsystem_equipos_protocolos',
        'mgmtsystem_equipos_protocolos_rel',
        'protocolo_id')

    # # remove ids
    res[0]['protocolos_ids'] = valid_protocols_ids
    res[0]['informes_ids'] = valid_informes_ids
    res[0]['anexos_ids'] = valid_anexos_ids

    return res

In your code those lines won't work just because there is no recordset available in self (it's correct behaviour search_read must have @api.model decorator).

    # here the variable allways has the original value (99)
    current_stage_id = self.dmy
    current_stage_id = self.dummy()
    current_stage_id = getattr(self, dmy)

So just remove those and lines and apply some other logic to get data.