From 70b8794792b8be7bd19dcfa56b4023c54b042bf2 Mon Sep 17 00:00:00 2001 From: Nicolas Praz Date: Thu, 12 Sep 2024 16:12:24 +0200 Subject: [PATCH 1/5] found bug and solution, need to clean it up and test some more --- my_compassion/controllers/my_account.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/my_compassion/controllers/my_account.py b/my_compassion/controllers/my_account.py index 997564e..6fa5c69 100644 --- a/my_compassion/controllers/my_account.py +++ b/my_compassion/controllers/my_account.py @@ -498,9 +498,16 @@ def my_donations(self, invoice_page=1, invoice_per_page=12, **kw): ] ) + end_reason_child_depart = request.env.ref("sponsorship_compassion.end_reason_depart") + sponsorships = partner.sponsorship_ids.filtered( lambda s: s.state in ["waiting", "active", "mandate"] and partner == s.mapped("partner_id") + or + s.state == "terminated" + and s.can_write_letter + and s.end_reason_id == end_reason_child_depart + and partner == s.mapped("partner_id") ) currency = sponsorships.mapped("pricelist_id.currency_id")[:1].name @@ -509,7 +516,7 @@ def my_donations(self, invoice_page=1, invoice_per_page=12, **kw): sponsorships_by_group = { g: ( sponsorships.filtered(lambda s, g=g: s.group_id == g), - f"{int(g.total_amount):,d} {currency}", + f"{int(sum(sponsorships.filtered(lambda s, g=g: s.group_id == g).mapped('total_amount'))):,d} {currency}", ) for g in sponsorships.mapped("group_id") } From 1025efa99631aa58ba701b68a6eb8d924989cdbf Mon Sep 17 00:00:00 2001 From: Nicolas Praz Date: Fri, 13 Sep 2024 13:48:09 +0200 Subject: [PATCH 2/5] small refactoring --- my_compassion/controllers/my_account.py | 27 +++++++++---------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/my_compassion/controllers/my_account.py b/my_compassion/controllers/my_account.py index 6fa5c69..87bf2bf 100644 --- a/my_compassion/controllers/my_account.py +++ b/my_compassion/controllers/my_account.py @@ -32,15 +32,20 @@ def _get_user_children(state=None): + env = request.env + partner = env.user.partner_id + return _get_sponsorships(partner, state).mapped("child_id").sorted("preferred_name") + + +def _get_sponsorships(partner, state=None): """ - Find all the children for which the connected user has a contract for. - There is the possibility to fetch either only active children or only those + Find all the sponsorships of the given user. + There is the possibility to fetch either only active sponsorships or only those that are terminated / cancelled. By default, all sponsorships are returned - :return: a recordset of child.compassion which the connected user sponsors + :return: a recordset of recurring.contract of the given user """ env = request.env - partner = env.user.partner_id end_reason_child_depart = env.ref("sponsorship_compassion.end_reason_depart") def filter_sponsorships(sponsorship): @@ -72,8 +77,6 @@ def filter_sponsorships(sponsorship): partner.get_portal_sponsorships() .with_context(allow_during_suspension=True) .filtered(filter_sponsorships) - .mapped("child_id") - .sorted("preferred_name") ) @@ -498,17 +501,7 @@ def my_donations(self, invoice_page=1, invoice_per_page=12, **kw): ] ) - end_reason_child_depart = request.env.ref("sponsorship_compassion.end_reason_depart") - - sponsorships = partner.sponsorship_ids.filtered( - lambda s: s.state in ["waiting", "active", "mandate"] - and partner == s.mapped("partner_id") - or - s.state == "terminated" - and s.can_write_letter - and s.end_reason_id == end_reason_child_depart - and partner == s.mapped("partner_id") - ) + sponsorships = _get_sponsorships(partner, state="active") currency = sponsorships.mapped("pricelist_id.currency_id")[:1].name # Dict of groups mapped to their sponsorships, and total amount From 0b86070ba1a0e060d9d80140dcbaa91240497d0b Mon Sep 17 00:00:00 2001 From: Nicolas Praz Date: Fri, 13 Sep 2024 14:24:41 +0200 Subject: [PATCH 3/5] code cleanup --- my_compassion/controllers/my_account.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/my_compassion/controllers/my_account.py b/my_compassion/controllers/my_account.py index 87bf2bf..2d05bba 100644 --- a/my_compassion/controllers/my_account.py +++ b/my_compassion/controllers/my_account.py @@ -506,13 +506,11 @@ def my_donations(self, invoice_page=1, invoice_per_page=12, **kw): # Dict of groups mapped to their sponsorships, and total amount # {group: (, total_amount string), ...} - sponsorships_by_group = { - g: ( - sponsorships.filtered(lambda s, g=g: s.group_id == g), - f"{int(sum(sponsorships.filtered(lambda s, g=g: s.group_id == g).mapped('total_amount'))):,d} {currency}", - ) - for g in sponsorships.mapped("group_id") - } + sponsorships_by_group = {} + for g in sponsorships.mapped("group_id"): + filtered_sponsorships = sponsorships.filtered(lambda s: s.group_id == g) + total = int(sum(filtered_sponsorships.mapped('total_amount'))) + sponsorships_by_group[g] = (filtered_sponsorships, f"{total:,d} {currency}") values = self._prepare_portal_layout_values() pager = request.website.pager( From b0f333797e018fe57a3f21f4ea58b2b1b45fd37b Mon Sep 17 00:00:00 2001 From: Nicolas Praz Date: Fri, 13 Sep 2024 14:36:03 +0200 Subject: [PATCH 4/5] ruff-format --- my_compassion/controllers/my_account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/my_compassion/controllers/my_account.py b/my_compassion/controllers/my_account.py index 2d05bba..f1cd194 100644 --- a/my_compassion/controllers/my_account.py +++ b/my_compassion/controllers/my_account.py @@ -509,7 +509,7 @@ def my_donations(self, invoice_page=1, invoice_per_page=12, **kw): sponsorships_by_group = {} for g in sponsorships.mapped("group_id"): filtered_sponsorships = sponsorships.filtered(lambda s: s.group_id == g) - total = int(sum(filtered_sponsorships.mapped('total_amount'))) + total = int(sum(filtered_sponsorships.mapped("total_amount"))) sponsorships_by_group[g] = (filtered_sponsorships, f"{total:,d} {currency}") values = self._prepare_portal_layout_values() From 77458c630ee90f2741de8aa78e44468377328bf6 Mon Sep 17 00:00:00 2001 From: Nicolas Praz Date: Fri, 13 Sep 2024 14:45:40 +0200 Subject: [PATCH 5/5] code smell... apparently... --- my_compassion/controllers/my_account.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/my_compassion/controllers/my_account.py b/my_compassion/controllers/my_account.py index f1cd194..1c900b1 100644 --- a/my_compassion/controllers/my_account.py +++ b/my_compassion/controllers/my_account.py @@ -501,16 +501,16 @@ def my_donations(self, invoice_page=1, invoice_per_page=12, **kw): ] ) - sponsorships = _get_sponsorships(partner, state="active") - currency = sponsorships.mapped("pricelist_id.currency_id")[:1].name + active_sponsorships = _get_sponsorships(partner, state="active") + currency = active_sponsorships.mapped("pricelist_id.currency_id")[:1].name # Dict of groups mapped to their sponsorships, and total amount # {group: (, total_amount string), ...} sponsorships_by_group = {} - for g in sponsorships.mapped("group_id"): - filtered_sponsorships = sponsorships.filtered(lambda s: s.group_id == g) - total = int(sum(filtered_sponsorships.mapped("total_amount"))) - sponsorships_by_group[g] = (filtered_sponsorships, f"{total:,d} {currency}") + for g in active_sponsorships.mapped("group_id"): + sponsorships = active_sponsorships.filtered(lambda s, g=g: s.group_id == g) + total = int(sum(sponsorships.mapped("total_amount"))) + sponsorships_by_group[g] = (sponsorships, f"{total:,d} {currency}") values = self._prepare_portal_layout_values() pager = request.website.pager(