improved string operation code

This commit is contained in:
Bastien Gérard 2019-03-08 17:09:39 +01:00
parent b407c0e6c6
commit 82e28dec43

View File

@ -13,6 +13,8 @@ import pymongo
import six import six
from six import iteritems from six import iteritems
from mongoengine.queryset.transform import STRING_OPERATORS
try: try:
import dateutil import dateutil
except ImportError: except ImportError:
@ -106,12 +108,12 @@ class StringField(BaseField):
if not isinstance(op, six.string_types): if not isinstance(op, six.string_types):
return value return value
if op.lstrip('i') in ('startswith', 'endswith', 'contains', 'exact'): if op in STRING_OPERATORS:
flags = 0 case_insensitive = op.startswith('i')
if op.startswith('i'):
flags = re.IGNORECASE
op = op.lstrip('i') op = op.lstrip('i')
flags = re.IGNORECASE if case_insensitive else 0
regex = r'%s' regex = r'%s'
if op == 'startswith': if op == 'startswith':
regex = r'^%s' regex = r'^%s'