Skip to content

Commit

Permalink
[FIX] website: fix visitor _compute_time_statistics
Browse files Browse the repository at this point in the history
This commit fixes the visitor's time_statistics computation. When creating a
new visitor, last connection datetime is not set, so it's not retrieved in the
search_read and crash at search_read_result[visitor.id].

This fix also speeds up and simplifies the time_statistics computation.
As time_connection_datetime is always set (for already created visitor) and in
the depends, no need to read values before looping, the data is already fetched
in memory. We can then use directly the value for each visitor in self.

Task ID: 2120464
PR odoo#40199

closes odoo#43758

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
  • Loading branch information
dbeguin committed Jan 22, 2020
1 parent b2d7080 commit 1f0c9f2
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions addons/website/models/website_visitor.py
Expand Up @@ -117,13 +117,9 @@ def _compute_last_visited_page_id(self):

@api.depends('last_connection_datetime')
def _compute_time_statistics(self):
results = self.env['website.visitor'].with_context(active_test=False).search_read([('id', 'in', self.ids)], ['id', 'last_connection_datetime'])
mapped_data = {result['id']: result['last_connection_datetime'] for result in results}

for visitor in self:
last_connection_date = mapped_data[visitor.id]
visitor.time_since_last_action = _format_time_ago(self.env, (datetime.now() - last_connection_date))
visitor.is_connected = (datetime.now() - last_connection_date) < timedelta(minutes=5)
visitor.time_since_last_action = _format_time_ago(self.env, (datetime.now() - visitor.last_connection_datetime))
visitor.is_connected = (datetime.now() - visitor.last_connection_datetime) < timedelta(minutes=5)

def _prepare_visitor_send_mail_values(self):
if self.partner_id.email:
Expand Down

0 comments on commit 1f0c9f2

Please sign in to comment.