Browse Source

Ltree field: convert spaces to underscores

This will allow using spaces in query strings and similar places when it
makes sense.
merge-requests/53/head
Deimos 6 years ago
parent
commit
bc2b9ea4ed
  1. 2
      tildes/tests/test_group.py
  2. 5
      tildes/tildes/schemas/fields.py

2
tildes/tests/test_group.py

@ -46,7 +46,7 @@ def test_uppercase_letters_fixed():
def test_paths_with_invalid_characters(): def test_paths_with_invalid_characters():
"""Ensure that paths can't include some characters (not comprehensive).""" """Ensure that paths can't include some characters (not comprehensive)."""
invalid_chars = ' ~!@#$%^&*()+={}[]|\\:;"<>,?/'
invalid_chars = '~!@#$%^&*()+={}[]|\\:;"<>,?/'
for char in invalid_chars: for char in invalid_chars:
path = f"abc{char}xyz" path = f"abc{char}xyz"

5
tildes/tildes/schemas/fields.py

@ -144,8 +144,11 @@ class Ltree(Field):
def _deserialize(self, value: str, attr: str, data: dict) -> sqlalchemy_utils.Ltree: def _deserialize(self, value: str, attr: str, data: dict) -> sqlalchemy_utils.Ltree:
"""Deserialize a string path to an Ltree object.""" """Deserialize a string path to an Ltree object."""
# convert to lowercase and replace spaces with underscores
value = value.lower().replace(" ", "_")
try: try:
return sqlalchemy_utils.Ltree(value.lower())
return sqlalchemy_utils.Ltree(value)
except (TypeError, ValueError): except (TypeError, ValueError):
raise ValidationError("Invalid path") raise ValidationError("Invalid path")

Loading…
Cancel
Save