Skip to content

Commit

Permalink
[IMP] website_slides: clean partner_all_ids field
Browse files Browse the repository at this point in the history
Follow up of odoo#139104

After fixing the performance of ir rules, previously using
partner_all_ids on slide.channel, that field is not used
anymore except for tests. Adapt tests and remove it as well
as its search method.

Also adapt a search domain to use is_member field.

UPG PR: odoo/upgrade#5218
Task-3444633

Part-of: odoo#130030
  • Loading branch information
Odoonan authored and tde-banana-odoo committed Oct 19, 2023
1 parent cd0d313 commit f2a2881
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
32 changes: 8 additions & 24 deletions addons/website_slides/models/slide_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,11 @@ def _get_default_enroll_msg(self):
compute='_compute_members_counts')
members_completed_count = fields.Integer('# Completed Attendees', compute='_compute_members_counts')
members_invited_count = fields.Integer('# Invited Attendees', compute='_compute_members_counts')
# partner_ids and partner_all_ids implemented as compute/search instead of specifying the relation table
# directly, this is done because we want to exclude active=False records on the joining table
# partner_ids is implemented as compute/search instead of specifying the relation table
# directly because we want to exclude active=False records on the joining table
partner_ids = fields.Many2many(
'res.partner', string='Attendees', help="Enrolled partners in the course",
compute="_compute_partners", search="_search_partner_ids")
partner_all_ids = fields.Many2many(
'res.partner', string='All Attendees', help="Partners in the course (both enrolled and invited)",
compute="_compute_partners", search="_search_partner_all_ids")
# not stored access fields, depending on each user
completed = fields.Boolean('Done', compute='_compute_user_statistics', compute_sudo=False)
completion = fields.Integer('Completion', compute='_compute_user_statistics', compute_sudo=False)
Expand Down Expand Up @@ -468,18 +465,15 @@ def _compute_enroll(self):
@api.depends('channel_partner_all_ids', 'channel_partner_all_ids.member_status', 'channel_partner_all_ids.active')
def _compute_partners(self):
data = {
(slide_channel, member_status): partner_ids
for slide_channel, member_status, partner_ids in self.env['slide.channel.partner'].sudo()._read_group(
[('channel_id', 'in', self.ids)],
['channel_id', 'member_status'],
slide_channel: partner_ids
for slide_channel, partner_ids in self.env['slide.channel.partner'].sudo()._read_group(
[('channel_id', 'in', self.ids), ('member_status', '!=', 'invited')],
['channel_id'],
aggregates=['partner_id:array_agg']
)
}

for slide_channel in self:
partner_ids = data.get((slide_channel, 'joined'), []) + data.get((slide_channel, 'ongoing'), []) + data.get((slide_channel, 'completed'), [])
slide_channel.partner_ids = partner_ids
slide_channel.partner_all_ids = partner_ids + data.get((slide_channel, 'invited'), [])
slide_channel.partner_ids = data.get(slide_channel, [])

def _search_partner_ids(self, operator, value):
if isinstance(value, int) and operator == 'in':
Expand All @@ -492,16 +486,6 @@ def _search_partner_ids(self, operator, value):
)
)]

def _search_partner_all_ids(self, operator, value):
if isinstance(value, int) and operator == 'in':
value = [value]
return [(
'channel_partner_all_ids', '=', self.env['slide.channel.partner']._search(
[('partner_id', operator, value),
('active', '=', True)],
)
)]

@api.depends('slide_ids.is_published')
def _compute_slide_last_update(self):
for record in self:
Expand Down Expand Up @@ -1244,7 +1228,7 @@ def _search_get_detail(self, website, order, options):
slide_category = options.get('slide_category')
domain = [website.website_domain()]
if my:
domain.append([('partner_ids', '=', self.env.user.partner_id.id)])
domain.append([('is_member', '=', True)])
if search_tags:
ChannelTag = self.env['slide.channel.tag']
try:
Expand Down
28 changes: 16 additions & 12 deletions addons/website_slides/tests/test_attendee.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_join_invite_enroll_channel(self):
'member_status': 'invited'
})

self.assertTrue(user_portal_partner.id in self.channel.partner_all_ids.ids)
self.assertTrue(user_portal_partner in self.channel.channel_partner_all_ids.partner_id)
self.assertFalse(user_portal_partner.id in self.channel.partner_ids.ids)
# Invited partner can join the course and enroll itself. Sudo is used in controller if invited.
self.assertTrue(self.channel.with_user(self.user_portal).is_member_invited)
Expand All @@ -192,7 +192,7 @@ def test_member_default_create(self):

@users('user_officer')
def test_partners_and_search_on_slide_channel(self):
''' Check that partner_ids contains enrolled partners and partner_all_ids contains both invited and enrolled partners'''
''' Check that partner_ids contains (only) active enrolled partners '''
invited_cp, joined_cp = self.env['slide.channel.partner'].create([{
'channel_id': self.channel.id,
'partner_id': self.user_portal.partner_id.id,
Expand All @@ -209,21 +209,25 @@ def test_partners_and_search_on_slide_channel(self):
joined_cp_channel_ids = self.env['slide.channel'].search([('partner_ids', '=', joined_cp.partner_id.id)])
self.assertTrue(self.channel in joined_cp_channel_ids)

# Search partner_all_ids on model
invited_cp_channel_ids = self.env['slide.channel'].search([('partner_all_ids', '=', invited_cp.partner_id.id)])
self.assertTrue(self.channel in invited_cp_channel_ids)
joined_cp_channel_ids = self.env['slide.channel'].search([('partner_all_ids', '=', joined_cp.partner_id.id)])
self.assertTrue(self.channel in joined_cp_channel_ids)
partner_ids = self.channel.partner_ids
self.assertFalse(invited_cp.partner_id in partner_ids)
self.assertTrue(joined_cp.partner_id in partner_ids)

# partner_ids should only contain enrolled channel partners
partner_ids = self.channel.partner_ids
self.assertFalse(invited_cp.partner_id in partner_ids)
self.assertTrue(joined_cp.partner_id in partner_ids)

# partner_all_ids also contains invited channel partners.
partner_all_ids = self.channel.partner_all_ids
self.assertTrue(joined_cp.partner_id in partner_all_ids)
self.assertTrue(invited_cp.partner_id in partner_all_ids)
invited_cp.action_archive()
joined_cp.action_archive()

partner_ids = self.channel.partner_ids
self.assertFalse(invited_cp.partner_id in partner_ids)
self.assertFalse(joined_cp.partner_id in partner_ids)

invited_cp_channel_ids = self.env['slide.channel'].search([('partner_ids', '=', invited_cp.partner_id.id)])
self.assertFalse(self.channel in invited_cp_channel_ids)
joined_cp_channel_ids = self.env['slide.channel'].search([('partner_ids', '=', joined_cp.partner_id.id)])
self.assertFalse(self.channel in joined_cp_channel_ids)

def test_copy_partner_not_course_member(self):
""" To check members of the channel after duplication of contact """
Expand Down

0 comments on commit f2a2881

Please sign in to comment.