change has_word ==> wholeword
This commit is contained in:
Ido Shraga 2021-09-12 18:21:01 +03:00
parent 6bc1b83695
commit 3d6b650592
4 changed files with 17 additions and 20 deletions

View File

@ -86,6 +86,10 @@ expressions:
* ``istartswith`` -- string field starts with value (case insensitive) * ``istartswith`` -- string field starts with value (case insensitive)
* ``endswith`` -- string field ends with value * ``endswith`` -- string field ends with value
* ``iendswith`` -- string field ends with value (case insensitive) * ``iendswith`` -- string field ends with value (case insensitive)
* ``wholeword`` -- string field contains whole word
* ``iwholeword`` -- string field contains whole word (case insensitive)
* ``regex`` -- string field match by regex
* ``iregex`` -- string field match by regex (case insensitive)
* ``match`` -- performs an $elemMatch so you can match an entire document within an array * ``match`` -- performs an $elemMatch so you can match an entire document within an array

View File

@ -157,7 +157,7 @@ class StringField(BaseField):
regex = r"%s$" regex = r"%s$"
elif op == "exact": elif op == "exact":
regex = r"^%s$" regex = r"^%s$"
elif op == "has_word": elif op == "wholeword":
regex = r"\b%s\b" regex = r"\b%s\b"
elif op == "regex": elif op == "regex":
regex = value regex = value
@ -1094,14 +1094,7 @@ class DictField(ComplexBaseField):
def prepare_query_value(self, op, value): def prepare_query_value(self, op, value):
match_operators = [ match_operators = [
"contains", *STRING_OPERATORS
"icontains",
"startswith",
"istartswith",
"endswith",
"iendswith",
"exact",
"iexact",
] ]
if op in match_operators and isinstance(value, str): if op in match_operators and isinstance(value, str):

View File

@ -53,8 +53,8 @@ STRING_OPERATORS = (
"iexact", "iexact",
"regex", "regex",
"iregex", "iregex",
"has_word", "wholeword",
"ihas_word", "iwholeword",
) )
CUSTOM_OPERATORS = ("match",) CUSTOM_OPERATORS = ("match",)
MATCH_OPERATORS = ( MATCH_OPERATORS = (

View File

@ -1258,18 +1258,18 @@ class TestQueryset(unittest.TestCase):
assert obj is None assert obj is None
# Test has_word # Test wholeword
obj = self.Person.objects(name__has_word="Guido").first() obj = self.Person.objects(name__wholeword="Guido").first()
assert obj == person assert obj == person
obj = self.Person.objects(name__has_word="rossum").first() obj = self.Person.objects(name__wholeword="rossum").first()
assert obj is None assert obj is None
obj = self.Person.objects(name__has_word="Rossu").first() obj = self.Person.objects(name__wholeword="Rossu").first()
assert obj is None assert obj is None
# Test ihas_word # Test iwholeword
obj = self.Person.objects(name__ihas_word="rOSSUM").first() obj = self.Person.objects(name__iwholeword="rOSSUM").first()
assert obj == person assert obj == person
obj = self.Person.objects(name__ihas_word="rOSSU").first() obj = self.Person.objects(name__iwholeword="rOSSU").first()
assert obj is None assert obj is None
# Test regex # Test regex
@ -1374,8 +1374,8 @@ class TestQueryset(unittest.TestCase):
.filter(name__not__endswith="tum")\ .filter(name__not__endswith="tum")\
.filter(name__icontains="VAN")\ .filter(name__icontains="VAN")\
.filter(name__regex="^Guido")\ .filter(name__regex="^Guido")\
.filter(name__has_word="Guido")\ .filter(name__wholeword="Guido")\
.filter(name__has_word="van") .filter(name__wholeword="van")
assert people.count() == 1 assert people.count() == 1
def assertSequence(self, qs, expected): def assertSequence(self, qs, expected):