|
1 | 1 | from typing import Any, Final |
2 | | -from enum import auto |
3 | | -from dataclasses import dataclass, field |
4 | | -from io import TextIOBase |
5 | | -from django.db.models import Model |
6 | 2 | from urllib.parse import urlparse |
7 | 3 |
|
| 4 | +from django.db.models import Model |
| 5 | + |
8 | 6 | from country_workspace.contrib.aurora.models import Project, Registration |
9 | 7 | from country_workspace.contrib.hope.sync.base import ( |
10 | | - BaseSync, |
11 | | - BaseSyncStep, |
12 | 8 | ParamDateName, |
13 | 9 | SyncConfig, |
14 | 10 | SkipRecordError, |
15 | | - sync_context, |
| 11 | + sync_entity, |
| 12 | + build_endpoint, |
16 | 13 | ) |
17 | | -from country_workspace.contrib.aurora.client import AuroraClient |
18 | | - |
19 | 14 |
|
20 | 15 | MODELS: Final[tuple[type[Model], ...]] = (Project, Registration) |
21 | 16 | """List of models to synchronize.""" |
22 | 17 |
|
23 | 18 |
|
24 | | -class SyncStep(BaseSyncStep): |
25 | | - """Synchronization steps for aurora-related models.""" |
26 | | - |
27 | | - PROJECTS = (auto(), lambda self: self.sync_projects) |
28 | | - REGISTRATIONS = (auto(), lambda self: self.sync_registrations) |
29 | | - |
30 | | - |
31 | | -@dataclass |
32 | | -class SyncContextAurora(BaseSync): |
33 | | - """Context for synchronizing Aurora-related models.""" |
34 | | - |
35 | | - SyncStep = SyncStep |
36 | | - client: AuroraClient = field(default_factory=AuroraClient) |
37 | | - |
38 | | - def sync_projects(self) -> None: |
39 | | - """Fetch and process Project records from the Aurora system.""" |
40 | | - self.sync_entity( |
41 | | - SyncConfig( |
42 | | - model=Project, |
43 | | - reference_id="reference_pk", |
44 | | - endpoint=self._build_endpoint("project", Project, ParamDateName.MODIFIED), |
45 | | - prepare_defaults=lambda r: {"name": r["name"]}, |
46 | | - ), |
47 | | - ) |
48 | | - |
49 | | - def sync_registrations(self) -> None: |
50 | | - """Fetch and process Registration records from the Aurora system.""" |
51 | | - |
52 | | - def _prepare_defaults(rec: dict[str, Any]) -> dict[str, Any] | None: |
53 | | - if (extracted_id := self._extract_related_id(rec["project"])) is None: |
54 | | - raise SkipRecordError("Invalid project URL format.") |
55 | | - try: |
56 | | - project = Project.objects.get(reference_pk=extracted_id) |
57 | | - except Project.DoesNotExist as e: |
58 | | - raise SkipRecordError("Project not found.") from e |
59 | | - return { |
60 | | - "name": rec["name"], |
61 | | - "project": project, |
62 | | - "reference_pk": rec["id"], |
63 | | - } |
64 | | - |
65 | | - self.sync_projects() |
66 | | - self.sync_entity( |
67 | | - SyncConfig( |
68 | | - model=Registration, |
69 | | - reference_id="reference_pk", |
70 | | - endpoint=self._build_endpoint("registration", Registration, ParamDateName.MODIFIED), |
71 | | - prepare_defaults=_prepare_defaults, |
72 | | - ), |
73 | | - ) |
74 | | - |
75 | | - def _extract_related_id(self, url: str) -> int | None: |
76 | | - """Extract the related object ID from the given URL. |
77 | | -
|
78 | | - Args: |
79 | | - url (str): A URL string that is expected to end with the object's ID as its last path segment. |
80 | | -
|
81 | | - Returns: |
82 | | - int | None: The extracted ID if successful, otherwise None. |
83 | | -
|
84 | | - """ |
85 | | - parsed_url = urlparse(url) |
86 | | - try: |
87 | | - related_id = parsed_url.path.rstrip("/").split("/")[-1] |
88 | | - return int(related_id) |
89 | | - except (ValueError, IndexError): |
90 | | - return None |
91 | | - |
92 | | - |
93 | | -def sync_context_aurora( |
94 | | - *, |
95 | | - delta_sync: bool = False, |
96 | | - step: SyncStep | None = None, |
97 | | - stdout: TextIOBase | None = None, |
98 | | -) -> dict[str, Any]: |
99 | | - """Run synchronization for geo-related models. |
| 19 | +def _extract_related_id(url: str) -> int | None: |
| 20 | + """Extract the related object ID from the given URL. |
100 | 21 |
|
101 | 22 | Args: |
102 | | - delta_sync (bool): If True, only synchronize records updated after the last sync, |
103 | | - otherwise synchronize all records. |
104 | | - step (SyncStep | None): Specific step to execute (e.g., SyncStep.REGISTRATIONS). If None, all steps are run. |
105 | | - stdout (TextIOBase | None): Optional output stream for logging. |
| 23 | + url (str): A URL string that is expected to end with the object's ID as its last path segment. |
106 | 24 |
|
107 | 25 | Returns: |
108 | | - dict[str, Any]: Synchronization results, including counts and errors. |
| 26 | + int | None: The extracted ID if successful, otherwise None. |
109 | 27 |
|
110 | 28 | """ |
111 | | - return sync_context( |
112 | | - SyncContextAurora, |
113 | | - delta_sync=delta_sync, |
114 | | - step=step, |
115 | | - stdout=stdout, |
| 29 | + parsed_url = urlparse(url) |
| 30 | + try: |
| 31 | + related_id = parsed_url.path.rstrip("/").split("/")[-1] |
| 32 | + return int(related_id) |
| 33 | + except (ValueError, IndexError): |
| 34 | + return None |
| 35 | + |
| 36 | + |
| 37 | +# client: AuroraClient = field(default_factory=AuroraClient) |
| 38 | + |
| 39 | + |
| 40 | +def sync_projects(delta_sync: bool) -> None: |
| 41 | + """Fetch and process Project records from the Aurora system.""" |
| 42 | + sync_entity( |
| 43 | + SyncConfig( |
| 44 | + model=Project, |
| 45 | + reference_id="reference_pk", |
| 46 | + endpoint=build_endpoint("project", Project, ParamDateName.MODIFIED, delta_sync), |
| 47 | + prepare_defaults=lambda r: {"name": r["name"]}, |
| 48 | + delta_sync=delta_sync, |
| 49 | + ), |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def sync_registrations(delta_sync: bool) -> None: |
| 54 | + """Fetch and process Registration records from the Aurora system.""" |
| 55 | + |
| 56 | + def _prepare_defaults(rec: dict[str, Any]) -> dict[str, Any] | None: |
| 57 | + if (extracted_id := _extract_related_id(rec["project"])) is None: |
| 58 | + raise SkipRecordError("Invalid project URL format.") |
| 59 | + try: |
| 60 | + project = Project.objects.get(reference_pk=extracted_id) |
| 61 | + except Project.DoesNotExist as e: |
| 62 | + raise SkipRecordError("Project not found.") from e |
| 63 | + return { |
| 64 | + "name": rec["name"], |
| 65 | + "project": project, |
| 66 | + "reference_pk": rec["id"], |
| 67 | + } |
| 68 | + |
| 69 | + sync_entity( |
| 70 | + SyncConfig( |
| 71 | + model=Registration, |
| 72 | + reference_id="reference_pk", |
| 73 | + endpoint=build_endpoint("registration", Registration, ParamDateName.MODIFIED), |
| 74 | + prepare_defaults=_prepare_defaults, |
| 75 | + delta_sync=delta_sync, |
| 76 | + ), |
116 | 77 | ) |
0 commit comments