[FIX] website_crm: fix compute on default

Now, compute's methods are executed on default, which leads to a crash
when these methods try to use the actual records but there isn't one
yet (e.g. at the creation of a record).

- Fix the traceback raised when creating a lead because of the
visitor_page_count method when self.ids is empty on default compute.

- Since the default_get() and the first_onchange() are also now combined,
we need to adapt the syntax of the default get for the field opportunity_ids,
replacing the array of ids by the command syntax '[(6, 0, ids)]'.

task-2325629

X-original-commit: acbc97bfe290ef3c7b8f7a1160266e49485099ae
This commit is contained in:
Jérémy Hennecart
2020-09-02 09:32:14 +00:00
parent 3eb72a6d00
commit a7906b282c
2 changed files with 14 additions and 12 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ class MergeOpportunity(models.TransientModel):
if record_ids:
if 'opportunity_ids' in fields:
opp_ids = self.env['crm.lead'].browse(record_ids).filtered(lambda opp: opp.probability < 100).ids
result['opportunity_ids'] = opp_ids
result['opportunity_ids'] = [(6, 0, opp_ids)]
return result
+13 -11
View File
@@ -12,17 +12,19 @@ class Lead(models.Model):
@api.depends('visitor_ids.page_ids')
def _compute_visitor_page_count(self):
self.flush(['visitor_ids'])
sql = """ SELECT l.id as lead_id, count(*) as page_view_count
FROM crm_lead l
JOIN crm_lead_website_visitor_rel lv ON l.id = lv.crm_lead_id
JOIN website_visitor v ON v.id = lv.website_visitor_id
JOIN website_track p ON p.visitor_id = v.id
WHERE l.id in %s
GROUP BY l.id"""
self.env.cr.execute(sql, (tuple(self.ids),))
page_data = self.env.cr.dictfetchall()
mapped_data = {data['lead_id']: data['page_view_count'] for data in page_data}
mapped_data = {}
if self.ids:
self.flush(['visitor_ids'])
sql = """ SELECT l.id as lead_id, count(*) as page_view_count
FROM crm_lead l
JOIN crm_lead_website_visitor_rel lv ON l.id = lv.crm_lead_id
JOIN website_visitor v ON v.id = lv.website_visitor_id
JOIN website_track p ON p.visitor_id = v.id
WHERE l.id in %s
GROUP BY l.id"""
self.env.cr.execute(sql, (tuple(self.ids),))
page_data = self.env.cr.dictfetchall()
mapped_data = {data['lead_id']: data['page_view_count'] for data in page_data}
for lead in self:
lead.visitor_page_count = mapped_data.get(lead.id, 0)