Skip to content

Commit f0666c0

Browse files
author
Jongmin Kim
authored
Merge pull request #136 from whdalsrnt/master
refactor: apply trim to strings when creating or updating database
2 parents 4cfe150 + fb16725 commit f0666c0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/spaceone/core/model/mongo_model/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,23 @@ def _get_unique_fields(cls):
202202

203203
return unique_fields
204204

205+
@classmethod
206+
def _trim_value(cls, value: any) -> any:
207+
if isinstance(value, str):
208+
return value.strip()
209+
elif isinstance(value, dict):
210+
changed_value = {}
211+
for k, v in value.items():
212+
if isinstance(k, str):
213+
k = k.strip()
214+
if isinstance(v, str):
215+
v = v.strip()
216+
changed_value[k] = cls._trim_value(v)
217+
218+
return changed_value
219+
else:
220+
return value
221+
205222
@classmethod
206223
def create(cls, data):
207224
create_data = {}
@@ -227,6 +244,9 @@ def create(cls, data):
227244
if vos.count() > 0:
228245
raise ERROR_SAVE_UNIQUE_VALUES(keys=unique_field)
229246

247+
for key, value in create_data.items():
248+
create_data[key] = cls._trim_value(value)
249+
230250
try:
231251
new_vo = cls(**create_data).save()
232252
except Exception as e:
@@ -263,6 +283,9 @@ def update(self, data):
263283
del data[key]
264284

265285
if data != {}:
286+
for key, value in data.items():
287+
data[key] = self._trim_value(value)
288+
266289
try:
267290
super().update(**data)
268291
self.reload()

0 commit comments

Comments
 (0)