Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class Database:
'ne': '$ne',
}

BOOL_VALUE_MAP = {
'true': True,
'false': False
}

def __init__(self, service='mongodb://db:27017', db_name='kernelci'):
self._motor = motor_asyncio.AsyncIOMotorClient(service)
self._db = self._motor[db_name]
Expand Down Expand Up @@ -103,10 +108,19 @@ def _convert_int_values(cls, attributes):
if isinstance(val, tuple) and len(val) == 2 and val[0] == 'int'
}

def _convert_bool_values(self, attributes):
for key, val in attributes.items():
if isinstance(val, str):
bool_value = self.BOOL_VALUE_MAP.get(val.lower())
if bool_value is not None:
attributes[key] = bool_value
return attributes

def _prepare_query(self, attributes):
query = attributes.copy()
query.update(self._translate_operators(query))
query.update(self._convert_int_values(query))
query.update(self._convert_bool_values(query))
return query

async def find_by_attributes(self, model, attributes):
Expand Down
6 changes: 3 additions & 3 deletions migrations/20231102101356_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def upgrade(db: "pymongo.database.Database"):
"_id": user['_id'],
"email": user['profile']['email'],
"hashed_password": user['profile']['hashed_password'],
"is_active": 1,
"is_superuser": 0,
"is_verified": 0,
"is_active": True,
"is_superuser": False,
"is_verified": False,
"username": user['profile']['username'],
"groups": user['profile']['groups']
},
Expand Down