|
1 | 1 | from collections.abc import Callable, Iterator |
2 | 2 | from dataclasses import dataclass, field |
| 3 | +from functools import cached_property |
3 | 4 | from itertools import batched |
4 | 5 | from json import JSONDecodeError |
5 | 6 | from typing import Any, TypedDict, ReadOnly |
|
12 | 13 |
|
13 | 14 | from country_workspace.contrib.hope.client import HopeClient |
14 | 15 | from country_workspace.exceptions import RemoteError |
15 | | -from country_workspace.models import AsyncJob, Rdp |
| 16 | +from country_workspace.models import AsyncJob, Rdp, Program |
16 | 17 | from country_workspace.workspaces.models import CountryHousehold, CountryIndividual |
17 | | -from country_workspace.utils.fields import map_fields |
18 | 18 |
|
19 | 19 |
|
20 | 20 | type Beneficiary = CountryHousehold | CountryIndividual |
@@ -189,17 +189,24 @@ def safe_post(self, path: str, data: Any, error_msg: str) -> dict[str, Any] | No |
189 | 189 | self.total["errors"].append(f"{error_msg}: {e}") |
190 | 190 | return None |
191 | 191 |
|
| 192 | + @cached_property |
| 193 | + def program(self) -> Program: |
| 194 | + return Program.objects.get(hope_id=self.program_hope_id) |
| 195 | + |
192 | 196 | def prepare_batch(self) -> tuple[list[int], list[dict]]: |
193 | 197 | """Prepare a batch of household/individual|people data for API submission.""" |
194 | 198 | ids, data = [], [] |
195 | 199 | for item in self.queryset: |
196 | 200 | ids.append(item.id) |
197 | | - data.append( |
198 | | - {**map_fields(item.flex_fields), "members": [map_fields(m.flex_fields) for m in item.members.all()]} |
199 | | - if self.master_detail |
200 | | - else map_fields(item.flex_fields) |
201 | | - ) |
202 | | - return ids, data |
| 201 | + flex_fields = item.apply_grouping() |
| 202 | + if self.master_detail: |
| 203 | + flex_fields["members"] = [] |
| 204 | + for member in item.members.all(): |
| 205 | + flex_fields["members"].append(member.apply_grouping()) |
| 206 | + data.append(flex_fields) |
| 207 | + else: |
| 208 | + data.append(flex_fields) |
| 209 | + return ids, self.program.serialize(data) |
203 | 210 |
|
204 | 211 | def process_batch_response(self, response: dict | None, batch_ids: list[int]) -> list[int]: |
205 | 212 | """Process the API response for a batch push operation.""" |
@@ -279,7 +286,7 @@ def push_to_hope_core(job: AsyncJob) -> dict[str, Any]: |
279 | 286 |
|
280 | 287 | def steps() -> Iterator[Callable[[], None]]: |
281 | 288 | """Yield steps for pushing beneficiaries data in batches.""" |
282 | | - yield processor.rdi_create |
| 289 | + # yield processor.rdi_create |
283 | 290 | for batch_pks in batched(config["pks"], config["batch_size"]): |
284 | 291 | processor.set_queryset(batch_pks) |
285 | 292 | yield from (processor.check_beneficiaries_validity, processor.rdi_push) |
|
0 commit comments