From 6a1cda64f8d31f32cfe0ad13b3da8d8c7d7eda42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Mon, 21 Jun 2021 07:49:15 +0000 Subject: [PATCH] [IMP] crm: remove duplicated field activity_deadline_my Next to the addition of a generic field adding a compute to find my next activity deadline there is no need to keep a custom field on crm.lead model. Task ID-2438822 COM PR odoo/odoo#72219 Related: odoo/upgrade#2566 Related: odoo/enterprise#19044 Signed-off-by: Thibault Delavallee (tde) --- addons/crm/models/crm_lead.py | 38 +++++---------------------- addons/crm/tests/test_crm_activity.py | 8 +++--- addons/crm/views/crm_lead_views.xml | 8 +++--- 3 files changed, 15 insertions(+), 39 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 4287766f4f2..d441953ef5b 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -139,10 +139,6 @@ class Lead(models.Model): ('red', 'Next activity late'), ('green', 'Next activity is planned')], string='Kanban State', compute='_compute_kanban_state') - activity_date_deadline_my = fields.Date( - 'My Activities Deadline', compute='_compute_activity_date_deadline_my', - search='_search_activity_date_deadline_my', compute_sudo=False, - readonly=True, store=False, groups="base.group_user") tag_ids = fields.Many2many( 'crm.tag', 'crm_tag_rel', 'lead_id', 'tag_id', string='Tags', help="Classify and analyze your lead/opportunity categories like: Training, Service") @@ -248,26 +244,6 @@ class Lead(models.Model): kanban_state = 'red' lead.kanban_state = kanban_state - @api.depends('activity_ids.date_deadline') - @api.depends_context('uid') - def _compute_activity_date_deadline_my(self): - todo_activities = [] - if self.ids: - todo_activities = self.env['mail.activity'].search([ - ('user_id', '=', self._uid), - ('res_model', '=', self._name), - ('res_id', 'in', self.ids) - ], order='date_deadline ASC') - - for record in self: - record.activity_date_deadline_my = next( - (activity.date_deadline for activity in todo_activities if activity.res_id == record.id), - False - ) - - def _search_activity_date_deadline_my(self, operator, operand): - return ['&', ('activity_ids.user_id', '=', self._uid), ('activity_ids.date_deadline', operator, operand)] - @api.depends('company_id') def _compute_user_company_ids(self): all_companies = self.env['res.company'].search([]) @@ -704,11 +680,11 @@ class Lead(models.Model): @api.model def search(self, args, offset=0, limit=None, order=None, count=False): - """ Override to support ordering on activity_date_deadline_my. + """ Override to support ordering on my_activity_date_deadline. Ordering through web client calls search_read with an order parameter set. Search_read then calls search. In this override we therefore override search - to intercept a search without count with an order on activity_date_deadline_my. + to intercept a search without count with an order on my_activity_date_deadline. In that case we do the search in two steps. First step: fill with deadline-based results @@ -733,14 +709,14 @@ class Lead(models.Model): All other search and search_read are left untouched by this override to avoid side effects. Search_count is not affected by this override. """ - if count or not order or 'activity_date_deadline_my' not in order: + if count or not order or 'my_activity_date_deadline' not in order: return super(Lead, self).search(args, offset=offset, limit=limit, order=order, count=count) order_items = [order_item.strip().lower() for order_item in (order or self._order).split(',')] # Perform a read_group on my activities to get a mapping lead_id / deadline # Remember date_deadline is required, we always have a value for it. Only # the earliest deadline per lead is kept. - activity_asc = any('activity_date_deadline_my asc' in item for item in order_items) + activity_asc = any('my_activity_date_deadline asc' in item for item in order_items) my_lead_activities = self.env['mail.activity'].read_group( [('res_model', '=', self._name), ('user_id', '=', self.env.uid)], ['res_id', 'date_deadline:min'], @@ -750,7 +726,7 @@ class Lead(models.Model): my_lead_mapping = dict((item['res_id'], item['date_deadline']) for item in my_lead_activities) my_lead_ids = list(my_lead_mapping.keys()) my_lead_domain = expression.AND([[('id', 'in', my_lead_ids)], args]) - my_lead_order = ', '.join(item for item in order_items if 'activity_date_deadline_my' not in item) + my_lead_order = ', '.join(item for item in order_items if 'my_activity_date_deadline' not in item) # Search leads linked to those activities and order them. See docstring # of this method for more details. @@ -768,13 +744,13 @@ class Lead(models.Model): # Fill with remaining leads. If a limit is given, simply remove count of # already fetched. Otherwise keep none. If an offset is set we have to # reduce it by already fetch results hereabove. Order is updated to exclude - # activity_date_deadline_my when calling super() . + # my_activity_date_deadline when calling super() . lead_limit = (limit - len(my_lead_ids_keep)) if limit else None if offset: lead_offset = max((offset - len(search_res), 0)) else: lead_offset = 0 - lead_order = ', '.join(item for item in order_items if 'activity_date_deadline_my' not in item) + lead_order = ', '.join(item for item in order_items if 'my_activity_date_deadline' not in item) other_lead_res = super(Lead, self).search( expression.AND([[('id', 'not in', my_lead_ids_skip)], args]), diff --git a/addons/crm/tests/test_crm_activity.py b/addons/crm/tests/test_crm_activity.py index f054c19f40d..9993df60e49 100644 --- a/addons/crm/tests/test_crm_activity.py +++ b/addons/crm/tests/test_crm_activity.py @@ -46,7 +46,7 @@ class TestCrmMailActivity(TestCrmCommon): # assert initial data, ensure we did not break base behavior for lead in test_leads: - self.assertFalse(lead.activity_date_deadline_my) + self.assertFalse(lead.my_activity_date_deadline) search_res = self.env['crm.lead'].search([('id', 'in', test_leads.ids)], limit=5, offset=0, order='id ASC') self.assertEqual(search_res.ids, test_leads[:5].ids) search_res = self.env['crm.lead'].search([('id', 'in', test_leads.ids)], limit=5, offset=5, order='id ASC') @@ -82,11 +82,11 @@ class TestCrmMailActivity(TestCrmCommon): expected_leads_desc = self.env['crm.lead'].browse([test_leads[lid].id for lid in expected_ids_desc]) for idx, lead in enumerate(test_leads): - self.assertEqual(lead.activity_date_deadline_my, deadlines_my[idx]) + self.assertEqual(lead.my_activity_date_deadline, deadlines_my[idx]) self.assertEqual(lead.activity_date_deadline, deadlines_gl[idx], 'Fail at %s' % idx) # Let's go for a first batch of search - _order = 'activity_date_deadline_my ASC, %s' % default_order + _order = 'my_activity_date_deadline ASC, %s' % default_order _domain = [('id', 'in', test_leads.ids)] search_res = self.env['crm.lead'].search(_domain, limit=None, offset=0, order=_order) @@ -98,7 +98,7 @@ class TestCrmMailActivity(TestCrmCommon): search_res = self.env['crm.lead'].search(_domain, limit=None, offset=3, order=_order) self.assertEqual(expected_leads_asc[3:].ids, search_res.ids) - _order = 'activity_date_deadline_my DESC, %s' % default_order + _order = 'my_activity_date_deadline DESC, %s' % default_order search_res = self.env['crm.lead'].search(_domain, limit=None, offset=0, order=_order) self.assertEqual(expected_leads_desc.ids, search_res.ids) search_res = self.env['crm.lead'].search(_domain, limit=4, offset=0, order=_order) diff --git a/addons/crm/views/crm_lead_views.xml b/addons/crm/views/crm_lead_views.xml index e0c9751a030..19c39c3fa52 100644 --- a/addons/crm/views/crm_lead_views.xml +++ b/addons/crm/views/crm_lead_views.xml @@ -711,7 +711,7 @@ - + @@ -729,10 +729,10 @@