Skip to content

Commit

Permalink
Fix duplicate computation of contacts to archive
Browse files Browse the repository at this point in the history
  • Loading branch information
nlachat-compassion committed Oct 15, 2024
1 parent 86ca799 commit 797bafc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
>Reminder: Archive invalid partners</field>
<field name="body_html" type="html">
<div>
<b>The following partners meet the criteria and are considered invalid : </b>
% set partner_info = object.partner_id.get_archive_contact()
<b>The following partners meet the criteria (see <a href="/web#action=521&amp;active_id=895&amp;cids=1&amp;id=600&amp;menu_id=2029&amp;model=project.task&amp;view_type=form">T0024</a>) and are considered invalid : </b>
<span style="font-family: 'miller';">
% for partner in partner_info:
% for partner in ctx.get('extra_email_data'):
<p><a href="/web#id=${partner.id}&amp;model=res.partner&amp;view_type=form">Ref ${partner.ref} - ${partner.name}</a></p>
% endfor
</span>
Expand Down
18 changes: 15 additions & 3 deletions partner_communication_switzerland/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#
##############################################################################
import logging
from typing import List
import uuid
from datetime import date
from datetime import datetime
Expand Down Expand Up @@ -288,8 +289,13 @@ def _compute_plural(self):
)
)

def get_archive_contact(self):
config = self.env.ref("partner_communication_switzerland.auto_reminder_archive_contact_config")
def find_potential_partners_to_archive(self) -> List['ResPartner']:
"""Finds the list of partners which meet certain criteria and could be archived.
This is used to generate the `auto_reminder_archive_contact.xml` email.
Returns:
List['ResPartner']: partners which meet the criteria for potential archiving.
"""
# Search for partners who do not have a full address (missing street, city, state, zip, and country)
# and who have invalid email addresses, no active sponsorships, but are still marked as active partners.
partners = self.search([("street", "=", False),
Expand Down Expand Up @@ -339,12 +345,15 @@ def get_archive_contact(self):

@api.model
def cron_auto_reminder_archive_contact(self):
"""Function called by a cron job in order to remind SDS to archive invalid contacts.
"""
reminder_receiver = (
self.env["res.partner"].sudo().search([("email", "=", "[email protected]")])
)
config = self.env.ref("partner_communication_switzerland.auto_reminder_archive_contact_config")

if reminder_receiver.get_archive_contact():
partners_to_archive = reminder_receiver.find_potential_partners_to_archive()
if len(partners_to_archive) > 0:
comm_vals = {
"config_id": config.id,
"partner_id": reminder_receiver.id,
Expand All @@ -353,9 +362,12 @@ def cron_auto_reminder_archive_contact(self):
"auto_send": True,
"send_mode": "digital" # Force sending by email
}
# Add the contacts to archive to the context to avoid recomputing it in the template
self = self.with_context({"extra_email_data": partners_to_archive})
self.env["partner.communication.job"].create(comm_vals)

return True

@api.model
def generate_tax_receipts(self):
"""
Expand Down

0 comments on commit 797bafc

Please sign in to comment.