MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / StringField

Class StringField

mongoengine/fields.py:112–180  ·  view source on GitHub ↗

A unicode string field.

Source from the content-addressed store, hash-verified

110
111
112class StringField(BaseField):
113 """A unicode string field."""
114
115 def __init__(self, regex=None, max_length=None, min_length=None, **kwargs):
116 """
117 :param regex: (optional) A string pattern that will be applied during validation
118 :param max_length: (optional) A max length that will be applied during validation
119 :param min_length: (optional) A min length that will be applied during validation
120 :param kwargs: Keyword arguments passed into the parent :class:`~mongoengine.BaseField`
121 """
122 self.regex = re.compile(regex) if regex else None
123 self.max_length = max_length
124 self.min_length = min_length
125 super().__init__(**kwargs)
126
127 def to_python(self, value):
128 if isinstance(value, str):
129 return value
130 try:
131 value = value.decode("utf-8")
132 except Exception:
133 pass
134 return value
135
136 def validate(self, value):
137 if not isinstance(value, str):
138 self.error("StringField only accepts string values")
139
140 if self.max_length is not None and len(value) > self.max_length:
141 self.error("String value is too long")
142
143 if self.min_length is not None and len(value) < self.min_length:
144 self.error("String value is too short")
145
146 if self.regex is not None and self.regex.match(value) is None:
147 self.error("String value did not match validation regex")
148
149 def lookup_member(self, member_name):
150 return None
151
152 def prepare_query_value(self, op, value):
153 if not isinstance(op, str):
154 return value
155
156 if op in STRING_OPERATORS:
157 case_insensitive = op.startswith("i")
158 op = op.lstrip("i")
159
160 flags = re.IGNORECASE if case_insensitive else 0
161
162 regex = r"%s"
163 if op == "startswith":
164 regex = r"^%s"
165 elif op == "endswith":
166 regex = r"%s$"
167 elif op == "exact":
168 regex = r"^%s$"
169 elif op == "wholeword":

Callers 15

History1Class · 0.90
History2Class · 0.90
HistoryClass · 0.90
UserClass · 0.90
PersonClass · 0.90
BlogPostClass · 0.90
PersonClass · 0.90
EmbedDataClass · 0.90
DataDocClass · 0.90
PersonClass · 0.90
AClass · 0.90
BClass · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected