Skip to content

Commit df13df9

Browse files
chg ! use p_code instead of UUID for admin area HOPE core API push
1 parent 2bc89a4 commit df13df9

File tree

3 files changed

+3
-72
lines changed

3 files changed

+3
-72
lines changed

src/country_workspace/contrib/hope/push.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,11 @@ def prepare_batch(self) -> tuple[list[int], list[dict]]:
193193
ids, data = [], []
194194
for item in self.queryset:
195195
ids.append(item.id)
196+
filter_none = lambda d: {k: v for k, v in d.items() if v is not None}
196197
data.append(
197-
{**item.flex_fields, "members": [m.flex_fields for m in item.members.all()]}
198+
{**filter_none(item.flex_fields), "members": [filter_none(m.flex_fields) for m in item.members.all()]}
198199
if self.master_detail
199-
else item.flex_fields
200+
else filter_none(item.flex_fields)
200201
)
201202
return ids, data
202203

src/country_workspace/utils/fields.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import binascii
21
from collections.abc import Callable, Mapping, Iterable
32
from functools import reduce
43
from typing import Any
5-
from base64 import b64decode
6-
from uuid import UUID
74

85
from django.utils import timezone
96

@@ -58,32 +55,3 @@ def uppercase_field_value(k: str, v: Any, fields_to_uppercase: Iterable[str] = T
5855
5956
"""
6057
return v.upper() if isinstance(v, str) and any(k.startswith(prefix) for prefix in fields_to_uppercase) else v
61-
62-
63-
def extract_uuid(value: str, prefix: str | None = None) -> UUID:
64-
"""Extract a UUID from the given string.
65-
66-
- If `value` is already a UUID, returns it unchanged.
67-
- Otherwise attempts Base64-decoding and stripping an optional `prefix`.
68-
69-
"""
70-
if not isinstance(value, str):
71-
raise TypeError("value must be a str")
72-
if prefix is not None and not isinstance(prefix, str):
73-
raise TypeError("prefix must be a str or None")
74-
75-
try:
76-
return UUID(value)
77-
except ValueError:
78-
pass
79-
80-
try:
81-
decoded = b64decode(value, validate=True).decode()
82-
except (binascii.Error, UnicodeDecodeError):
83-
raise ValueError(f"value is neither a valid UUID nor valid Base64: {value!r}")
84-
85-
raw = decoded.removeprefix(prefix or "")
86-
try:
87-
return UUID(raw)
88-
except ValueError:
89-
raise ValueError(f"decoded data is not a valid UUID: {raw!r}")

tests/utils/test_utils_fields.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
from django.core.files.uploadedfile import SimpleUploadedFile
55
from pytest_mock import MockerFixture
66

7-
from base64 import b64encode
8-
from uuid import uuid4, UUID
97

108
from country_workspace.contrib.kobo.api.data.helpers import VALUE_FORMAT
119
from country_workspace.utils.fields import (
1210
clean_field_name,
1311
TO_REMOVE_VALUES,
1412
clean_field_names,
15-
extract_uuid,
1613
)
1714
from country_workspace.utils.flex_fields import (
1815
Base64ImageInput,
@@ -85,41 +82,6 @@ def test_base64_image_field_content_is_unchanged(mocker: MockerFixture) -> None:
8582
super_clean_mock.assert_called_once_with(data, initial_data)
8683

8784

88-
FAKE_UUID = uuid4()
89-
FAKE_PREFIX = "Area:"
90-
ENC_B64_PREF = b64encode(f"{FAKE_PREFIX}{FAKE_UUID}".encode()).decode()
91-
ENC_B64 = b64encode(str(FAKE_UUID).encode()).decode()
92-
ENC_B64_BAD = b64encode("hello-world".encode()).decode()
93-
94-
95-
@pytest.mark.parametrize(
96-
("value", "prefix", "expected"),
97-
[
98-
(str(FAKE_UUID), None, FAKE_UUID),
99-
(ENC_B64_PREF, FAKE_PREFIX, FAKE_UUID),
100-
(ENC_B64, None, FAKE_UUID),
101-
],
102-
ids=["raw-uuid", "b64-with-prefix", "b64-no-prefix"],
103-
)
104-
def test_extract_uuid_success(value: str, prefix: str | None, expected: UUID) -> None:
105-
assert extract_uuid(value, prefix) == expected
106-
107-
108-
@pytest.mark.parametrize(
109-
("value", "prefix", "exc_type"),
110-
[
111-
("not-a-uuid-or-base64", None, ValueError),
112-
(ENC_B64_BAD, None, ValueError),
113-
(123, None, TypeError),
114-
(str(FAKE_UUID), 123, TypeError),
115-
],
116-
ids=["invalid-string", "b64-not-uuid", "value-not-str", "prefix-not-str"],
117-
)
118-
def test_extract_uuid_errors(value: str | int, prefix: str | int | None, exc_type: type[Exception]) -> None:
119-
with pytest.raises(exc_type):
120-
extract_uuid(value, prefix)
121-
122-
12385
@pytest.mark.parametrize(
12486
("value", "expected"),
12587
[

0 commit comments

Comments
 (0)