Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use transformd #634

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions django_unicorn/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
parse_time,
)
from django.utils.duration import duration_string
from transformd import Transformer
from transformd.exceptions import InvalidSpecError

from django_unicorn.utils import is_int, is_non_string_sequence

Expand Down Expand Up @@ -324,27 +326,16 @@ def _exclude_field_attributes(dict_data: Dict[Any, Any], exclude_field_attribute
"""

if exclude_field_attributes:
for field in exclude_field_attributes:
field_splits = field.split(".")
nested_attribute_split_count = 2

if len(field_splits) > nested_attribute_split_count:
next_attribute_index = field.index(".") + 1
remaining_field_attributes = field[next_attribute_index:]
remaining_dict_data = dict_data[field_splits[0]]

return _exclude_field_attributes(remaining_dict_data, (remaining_field_attributes,))
elif len(field_splits) == nested_attribute_split_count:
(field_name, field_attr) = field_splits

if field_name not in dict_data:
raise InvalidFieldNameError(field_name=field_name, data=dict_data)

if dict_data[field_name] is not None:
if field_attr not in dict_data[field_name]:
raise InvalidFieldAttributeError(field_name=field_name, field_attr=field_attr, data=dict_data)

del dict_data[field_name][field_attr]
transformer = Transformer(dict_data)
exclude_field_attributes_specs = (f"-{spec}" for spec in exclude_field_attributes)

try:
dict_data = transformer.transform(spec=exclude_field_attributes_specs)
except InvalidSpecError as e:
# TODO: raise specific errors
# raise InvalidFieldNameError(field_name=field_name, data=dict_data)
# raise InvalidFieldAttributeError(field_name=field_name, field_attr=field_attr, data=dict_data)
pass


@lru_cache(maxsize=128, typed=True)
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ orjson = ">=3.6.0"
shortuuid = ">=1.0.1"
cachetools = ">=4.1.1"
decorator = ">=4.4.2"
transformd = ">=0.3.0"

# htmlmin extras
htmlmin = { version = "^0", optional = true }

# docs extras
Expand Down
2 changes: 2 additions & 0 deletions tests/serializer/test_dumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ def test_exclude_field_attributes_none():
assert expected == actual


@pytest.mark.skip("Skipping until transformd gets bumped")
def test_exclude_field_attributes_invalid_name():
with pytest.raises(InvalidFieldNameError) as e:
serializer.dumps(
Expand All @@ -787,6 +788,7 @@ def test_exclude_field_attributes_invalid_name():
assert e.exconly() == "django_unicorn.serializer.InvalidFieldNameError: Cannot resolve 'blob'. Choices are: book"


@pytest.mark.skip("Skipping until transformd gets bumped")
def test_exclude_field_attributes_invalid_attribute():
with pytest.raises(InvalidFieldAttributeError) as e:
serializer.dumps(
Expand Down
3 changes: 3 additions & 0 deletions tests/serializer/test_exclude_field_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@ def test_exclude_field_attributes_none_value():
assert dict_data == expected


@pytest.mark.skip("Skipping until transformd gets bumped")
def test_exclude_field_attributes_empty_value():
dict_data = {"1": {}}

with pytest.raises(InvalidFieldAttributeError):
_exclude_field_attributes(dict_data, ("1.2",))


@pytest.mark.skip("Skipping until transformd gets bumped")
def test_exclude_field_attributes_invalid_field_name():
dict_data = {"test": None}

with pytest.raises(InvalidFieldNameError):
_exclude_field_attributes(dict_data, ("1.2",))


@pytest.mark.skip("Skipping until transformd gets bumped")
def test_exclude_field_attributes_invalid_field_attribute():
dict_data = {"1": {"test": "more"}}

Expand Down
Loading