Browse Source

Ltree field: convert values to lowercase

We're always using lowercase for all ltree usages (group paths, topic
tags), so it's best to just have the Ltree field do this conversion.
This fixes some minor issues like tag-filtering not working if the
casing of the tag was wrong.

A few other changes needed to be made as part of this, in places where I
was inadvertently passing an Ltree value into the Ltree field, instead
of a string.
merge-requests/45/head
Deimos 6 years ago
parent
commit
ff613476be
  1. 7
      tildes/tests/test_group.py
  2. 2
      tildes/tildes/models/group/group.py
  3. 2
      tildes/tildes/schemas/fields.py
  4. 2
      tildes/tildes/schemas/topic.py

7
tildes/tests/test_group.py

@ -39,10 +39,9 @@ def test_middle_element_end_with_underscore():
assert not is_valid_group_path("x.y_.z")
def test_uppercase_letters_invalid():
"""Ensure a group path can't contain uppercase chars."""
assert is_valid_group_path("comp.lang.c")
assert not is_valid_group_path("comp.lang.C")
def test_uppercase_letters_fixed():
"""Ensure a group path gets uppercase letters converted to lowercase."""
assert Group("Comp.Lang.C").path == "comp.lang.c"
def test_paths_with_invalid_characters():

2
tildes/tildes/models/group/group.py

@ -67,7 +67,7 @@ class Group(DatabaseModel):
def __init__(self, path: str, short_desc: Optional[str] = None) -> None:
"""Create a new group."""
self.path = Ltree(path)
self.path = path
self.short_description = short_desc
def __acl__(self) -> Sequence[Tuple[str, Any, str]]:

2
tildes/tildes/schemas/fields.py

@ -147,6 +147,6 @@ class Ltree(Field):
def _deserialize(self, value: str, attr: str, data: dict) -> sqlalchemy_utils.Ltree:
"""Deserialize a string path to an Ltree object."""
try:
return sqlalchemy_utils.Ltree(value)
return sqlalchemy_utils.Ltree(value.lower())
except (TypeError, ValueError):
raise ValidationError("Invalid path")

2
tildes/tildes/schemas/topic.py

@ -80,7 +80,7 @@ class TopicSchema(Schema):
group_schema = GroupSchema(partial=True)
for tag in value:
try:
group_schema.validate({"path": tag})
group_schema.validate({"path": str(tag)})
except ValidationError:
raise ValidationError("Tag %s is invalid" % tag)

Loading…
Cancel
Save