Odoo Mindscape. Understand Odoo faster. Explore modules, fields, dependencies and version differences in one place instead of digging through the source code.

Module: Base

Entity: res.session

Compute Method: _compute_session_info

Inputs Method Outputs
session_map = {}
for session_identifier, device_line_ids in self.env['res.device']._read_group(
            domain=[('session_identifier', 'in', self.mapped('session_identifier'))],
            groupby=['session_identifier'],
            aggregates=['id:recordset'],
        ):
            session_map[session_identifier] = device_line_ids.sorted('last_activity', reverse=True)
for session in self:
            device_ids = session_map[session.session_identifier]
            session.device_ids = device_ids

            # If a `res.session` record exists, there is at least one `res.device` record
            last_device_id = device_ids[0]  # because order is `last_activity desc`

            if session.is_current and \
                (current_device_id := device_ids.filtered('is_current')):
                # The user will see the information for the device he is
                # currently using, even if there are more recent logs for
                # another device.
                last_device_id = current_device_id

            session.ip_address = last_device_id.ip_address
            session.user_agent = last_device_id.user_agent
            session.country = last_device_id.country
            session.city = last_device_id.city
            session.first_activity = min(device_ids.mapped('first_activity'))

Back to entity