Skip to content

Commit

Permalink
Fixed backward compatibility issue with pymongo
Browse files Browse the repository at this point in the history
  • Loading branch information
kirgrim committed Oct 9, 2023
1 parent aadc3b3 commit 2995cde
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
10 changes: 5 additions & 5 deletions chat_server/server_utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def save_translations(cls, translation_mapping: dict) -> Dict[str, List[str]]:
filter_expression = {"_id": shout_id}
cls.db_controller.exec_query(
query=MongoQuery(
command=MongoCommands.UPDATE,
command=MongoCommands.UPDATE_MANY,
document=MongoDocuments.SHOUTS,
filters=filter_expression,
data={"translations": {}},
Expand Down Expand Up @@ -546,7 +546,7 @@ def get_user_preferences(cls, user_id):
if user and not user.get("preferences"):
cls.db_controller.exec_query(
MongoQuery(
command=MongoCommands.UPDATE,
command=MongoCommands.UPDATE_MANY,
document=MongoDocuments.USERS,
filters=MongoFilter(key="_id", value=user_id),
data={"preferences": prefs},
Expand All @@ -570,7 +570,7 @@ def set_user_preferences(cls, user_id, preferences_mapping: dict):
}
cls.db_controller.exec_query(
MongoQuery(
command=MongoCommands.UPDATE,
command=MongoCommands.UPDATE_MANY,
document=MongoDocuments.USERS,
filters=MongoFilter("_id", user_id),
data=update_mapping,
Expand Down Expand Up @@ -603,7 +603,7 @@ def save_tts_response(
)
cls.db_controller.exec_query(
query=MongoQuery(
command=MongoCommands.UPDATE,
command=MongoCommands.UPDATE_MANY,
document=MongoDocuments.SHOUTS,
filters=MongoFilter("_id", shout_id),
data={f"audio.{lang}.{gender}": audio_file_name},
Expand All @@ -628,7 +628,7 @@ def save_stt_response(cls, shout_id, message_text: str, lang: str = "en"):
try:
cls.db_controller.exec_query(
query=MongoQuery(
command=MongoCommands.UPDATE,
command=MongoCommands.UPDATE_MANY,
document=MongoDocuments.SHOUTS,
filters=MongoFilter("_id", shout_id),
data={f"transcripts.{lang}": message_text},
Expand Down
4 changes: 2 additions & 2 deletions chat_server/server_utils/prompt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def handle_prompt_message(message: dict) -> bool:
}
db_controller.exec_query(
MongoQuery(
command=MongoCommands.UPDATE,
command=MongoCommands.UPDATE_MANY,
document=MongoDocuments.PROMPTS,
filters=MongoFilter(key="_id", value=prompt_id),
**data_kwargs,
Expand Down Expand Up @@ -128,7 +128,7 @@ def handle_prompt_message(message: dict) -> bool:
}
db_controller.exec_query(
MongoQuery(
command=MongoCommands.UPDATE,
command=MongoCommands.UPDATE_MANY,
document=MongoDocuments.PROMPTS,
filters=MongoFilter(key="_id", value=prompt_id),
**data_kwargs,
Expand Down
2 changes: 1 addition & 1 deletion chat_server/server_utils/user_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def get_bot_data(
elif not bot_data.get("is_bot") == "1":
db_controller.exec_query(
MongoQuery(
command=MongoCommands.UPDATE,
command=MongoCommands.UPDATE_MANY,
document=MongoDocuments.USERS,
filters=MongoFilter("_id", bot_data["_id"]),
data={"is_bot": "1"},
Expand Down
4 changes: 2 additions & 2 deletions chat_server/sio.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ async def user_message(sid, data):
)
db_controller.exec_query(
query=MongoQuery(
command=MongoCommands.UPDATE,
command=MongoCommands.UPDATE_MANY,
document=MongoDocuments.CHATS,
filters=filter_expression,
data={"chat_flow": new_shout_data["_id"]},
Expand Down Expand Up @@ -363,7 +363,7 @@ async def prompt_completed(sid, data):
try:
db_controller.exec_query(
MongoQuery(
command=MongoCommands.UPDATE,
command=MongoCommands.UPDATE_MANY,
document=MongoDocuments.PROMPTS,
filters=MongoFilter(key="_id", value=prompt_id),
data=prompt_summary_agg,
Expand Down
7 changes: 6 additions & 1 deletion utils/database_utils/mongo_utils/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class MongoCommands(Enum):
DELETE_MANY = "delete_many"
# Update operation
UPDATE = "update"
UPDATE_MANY = "update_many"
UPDATE_ONE = "update_one"


class MongoDocuments(Enum):
Expand Down Expand Up @@ -135,7 +137,10 @@ def build_filters(self):
def build_setter(self) -> dict:
"""Builds setter for Mongo Query"""
res = None
if self.command.value == MongoCommands.UPDATE.value:
if self.command.value in (
MongoCommands.UPDATE_MANY.value,
MongoCommands.UPDATE_ONE.value,
):
res = {f"${self.data_action.lower()}": self.data}
elif self.command.value in (
MongoCommands.INSERT_ONE.value,
Expand Down
3 changes: 2 additions & 1 deletion utils/database_utils/mongodb_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class MongoDBConnector(DatabaseConnector):
"delete_many",
"find",
"find_one",
"update",
"update_one",
"update_many",
)

@property
Expand Down

0 comments on commit 2995cde

Please sign in to comment.