Skip to content

Commit af9f4da

Browse files
T3059 fcp lifecycle events (#2084)
* [T3059] FIX: last lifecycle event now takes date into account - FIX: take all events, sort by date descending, take first date and calculate state according to this event * [T3059] FIX: added post-migration script - FIX: post-migration script should now update the last event date on all projects - FIX: changed the sorting of the lifecycle events in the projects view - FIX: updated module version * [T3059] FIX: GCA and pre-commit
1 parent 7d92184 commit af9f4da

4 files changed

Lines changed: 45 additions & 6 deletions

File tree

child_compassion/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# pylint: disable=C8101
3030
{
3131
"name": "Compassion Children",
32-
"version": "14.0.1.5.0",
32+
"version": "14.0.1.6.0",
3333
"category": "Compassion",
3434
"author": "Compassion CH",
3535
"license": "AGPL-3",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import logging
2+
3+
from odoo import SUPERUSER_ID, api
4+
5+
_logger = logging.getLogger(__name__)
6+
7+
8+
def migrate(cr, version):
9+
"""
10+
Recompute the lifecycle state & date of an FCP project
11+
"""
12+
13+
env = api.Environment(cr, SUPERUSER_ID, {})
14+
15+
# Fetch all projects:
16+
projects = env["compassion.project"].search([])
17+
18+
# Invalidate cache
19+
projects.invalidate_cache(["last_lifecycle_id", "status", "suspension"])
20+
21+
projects._compute_last_lifecycle()
22+
projects._compute_suspension()
23+
24+
_logger.info("Successfully recomputed lifecycle states for %s FCPs.", len(projects))

child_compassion/models/project_compassion.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,26 @@ def _get_months(self):
477477
@api.depends("lifecycle_ids", "lifecycle_ids.date")
478478
def _compute_last_lifecycle(self):
479479
for project in self:
480-
last_info = project.lifecycle_ids[:1]
481-
reactivation_lifecycle = project.lifecycle_ids.filtered(
480+
if not project.lifecycle_ids:
481+
project.last_lifecycle_id = False
482+
continue
483+
484+
# Sort lifecycle events by date descending
485+
sorted_events = project.lifecycle_ids.sorted(
486+
key=lambda r: (str(r.date or ""), r.id), reverse=True
487+
)
488+
# Take first (newest) event
489+
last_info = sorted_events[0]
490+
491+
reactivation_lifecycle = sorted_events.filtered(
482492
lambda r, _last=last_info: r.date == _last.date
483493
and r.type == "Reactivation"
484-
)[:1]
494+
)
495+
485496
# If it exists, lifecycle with type 'Reactivation' is determinant
486-
project.last_lifecycle_id = reactivation_lifecycle or last_info
497+
project.last_lifecycle_id = (
498+
reactivation_lifecycle[0] if reactivation_lifecycle else last_info
499+
)
487500

488501
@api.depends("lifecycle_ids", "lifecycle_ids.write_date")
489502
def _compute_suspension(self):

child_compassion/views/project_compassion_view.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@
8787
</group>
8888
<group string="Lifecycle">
8989
<field name="lifecycle_ids">
90-
<tree>
90+
<tree
91+
default_order="date desc, id desc"
92+
>
9193
<field name="create_date" />
9294
<field name="date" />
9395
<field name="type" />

0 commit comments

Comments
 (0)