(20241218) Can now add tags to SMS messages while sending them, and small 'limit' bug fixed in listing mails where a huge no. of tokens are being sent.

This commit is contained in:
2024-12-18 11:10:18 +05:30
parent c340ebcba5
commit b42da0dacb
8 changed files with 32 additions and 13 deletions
+1 -1
View File
@@ -134,7 +134,7 @@ def init(blueprint_setup_state):
attr_name = "logs_mongo", attr_name = "logs_mongo",
project = constants.PROJECT_NAME, project = constants.PROJECT_NAME,
log_type = constants.MODULE_NAME, log_type = constants.MODULE_NAME,
operation = "mailListByTokIdApi", operation = "mailListApi",
log_input = True, log_input = True,
log_output = 1, log_output = 1,
sensitive_keys = ["sessionToken", "X-Session-Token"] sensitive_keys = ["sessionToken", "X-Session-Token"]
+2 -1
View File
@@ -177,7 +177,8 @@ async def send_sms(
mongo_conn = current_app.data_mongo, mongo_conn = current_app.data_mongo,
http_client = current_app.http_client, http_client = current_app.http_client,
auth_token = auth_token, auth_token = auth_token,
messages = inbound_data.message messages = inbound_data.message,
tags = inbound_data.tags
) )
# ┳┓ # ┳┓
+1 -1
View File
@@ -594,7 +594,7 @@ class MailController:
additional_filter: dict = None additional_filter: dict = None
) -> List[CoreMessageModel] | None: ) -> List[CoreMessageModel] | None:
# regardless of what additional filter is provided from outside, # Regardless of what additional filter is provided from outside,
# we add a mail-selecting filter here: # we add a mail-selecting filter here:
if additional_filter is None: additional_filter = {} if additional_filter is None: additional_filter = {}
additional_filter["serviceType"] = "email" additional_filter["serviceType"] = "email"
+10 -5
View File
@@ -187,6 +187,7 @@ class SMSController:
http_client: httpx.AsyncClient, http_client: httpx.AsyncClient,
auth_token: CoreAuthTokenModel, auth_token: CoreAuthTokenModel,
messages: List[NimbusSMSIndiaMessage], messages: List[NimbusSMSIndiaMessage],
tags: List[Any]
) -> SMSSendManyResults: ) -> SMSSendManyResults:
# Start with a blank variable: # Start with a blank variable:
@@ -232,7 +233,7 @@ class SMSController:
message = client_response.model_dump(), message = client_response.model_dump(),
snippet = message.text, snippet = message.text,
aiSnippet = None, aiSnippet = None,
tags = ["sms", "nimbusSmsIndia"] tags = list(set(tags + ["SMS", "Nimbus SMS", "India"]))
)) ))
# Done here: # Done here:
@@ -243,6 +244,7 @@ class SMSController:
http_client: httpx.AsyncClient, http_client: httpx.AsyncClient,
auth_token: CoreAuthTokenModel, auth_token: CoreAuthTokenModel,
messages: List[SavvyBulkSMSKenyaMessage], messages: List[SavvyBulkSMSKenyaMessage],
tags: List[Any]
) -> SMSSendManyResults: ) -> SMSSendManyResults:
# Start with a blank variable: # Start with a blank variable:
@@ -286,7 +288,7 @@ class SMSController:
message = client_response.model_dump(), message = client_response.model_dump(),
snippet = message.text, snippet = message.text,
aiSnippet = None, aiSnippet = None,
tags = ["sms", "savvyBulkSmsKenya"] tags = list(set(tags + ["SMS", "Savvy Bulk SMS", "Kenya"]))
)) ))
# Done here: # Done here:
@@ -297,7 +299,8 @@ class SMSController:
mongo_conn: AsyncMongo, mongo_conn: AsyncMongo,
http_client: httpx.AsyncClient, http_client: httpx.AsyncClient,
auth_token: CoreAuthTokenModel, auth_token: CoreAuthTokenModel,
messages: List[NimbusSMSIndiaMessage | SavvyBulkSMSKenyaMessage] messages: List[NimbusSMSIndiaMessage | SavvyBulkSMSKenyaMessage],
tags: List[Any]
) -> SMSSendManyResults: ) -> SMSSendManyResults:
# Start by assuming failure: # Start by assuming failure:
@@ -309,13 +312,15 @@ class SMSController:
send_results = await self.__send_from_nimbus_sms_india( send_results = await self.__send_from_nimbus_sms_india(
http_client = http_client, http_client = http_client,
auth_token = auth_token, auth_token = auth_token,
messages = messages messages = messages,
tags = tags
) )
case "savvyBulkSmsKenya": case "savvyBulkSmsKenya":
send_results = await self.__send_from_savvy_bulk_sms_kenya( send_results = await self.__send_from_savvy_bulk_sms_kenya(
http_client = http_client, http_client = http_client,
auth_token = auth_token, auth_token = auth_token,
messages = messages messages = messages,
tags = tags
) )
case _: case _:
send_results.message = f"invalid client {auth_token.client}" send_results.message = f"invalid client {auth_token.client}"
+8 -2
View File
@@ -320,12 +320,14 @@ class CoreAuthTokenController(BaseModel):
self, self,
mongo_conn: AsyncMongo, mongo_conn: AsyncMongo,
token_ids: List[ObjectId | str] = None, token_ids: List[ObjectId | str] = None,
limit: int = 100
) -> List[CoreAuthTokenModel]: ) -> List[CoreAuthTokenModel]:
""" """
To retrieve stored tokens from the database. Multiple tokens at a time. To retrieve stored tokens from the database. Multiple tokens at a time.
:param mongo_conn: The database connection (MongoDB) to use to perform the action. :param mongo_conn: The database connection (MongoDB) to use to perform the action.
:param token_ids: The identifier of the document that holds the token's details. :param token_ids: The identifier of the document that holds the token's details.
:param limit: The max. no. of records to pick.
:return: The retrieved record that has the token, and information about the service and client if found, else :return: The retrieved record that has the token, and information about the service and client if found, else
None when there is no matching record. None when there is no matching record.
""" """
@@ -333,7 +335,8 @@ class CoreAuthTokenController(BaseModel):
# If there is some filtering possible, we fetch the token: # If there is some filtering possible, we fetch the token:
tokens = await mongo_conn.find_many( tokens = await mongo_conn.find_many(
collection = self.AUTH_COLLECTION, collection = self.AUTH_COLLECTION,
filter = {"_id": {"$in": [ObjectId(k) for k in token_ids]}} filter = {"_id": {"$in": [ObjectId(k) for k in token_ids]}},
limit = limit
) )
# Done here: # Done here:
@@ -343,12 +346,14 @@ class CoreAuthTokenController(BaseModel):
self, self,
mongo_conn: AsyncMongo, mongo_conn: AsyncMongo,
token_keys: List[ObjectId | str] = None, token_keys: List[ObjectId | str] = None,
limit: int = 100
) -> List[CoreAuthTokenModel]: ) -> List[CoreAuthTokenModel]:
""" """
To retrieve stored tokens from the database. Multiple tokens at a time. To retrieve stored tokens from the database. Multiple tokens at a time.
:param mongo_conn: The database connection (MongoDB) to use to perform the action. :param mongo_conn: The database connection (MongoDB) to use to perform the action.
:param token_keys: the identifiers granted by the 'get_token_key' method. :param token_keys: the identifiers granted by the 'get_token_key' method.
:param limit: The max. no. of records to pick.
:return: The retrieved record that has the token, and information about the service and client if found, else :return: The retrieved record that has the token, and information about the service and client if found, else
None when there is no matching record. None when there is no matching record.
""" """
@@ -356,7 +361,8 @@ class CoreAuthTokenController(BaseModel):
# If there is some filtering possible, we fetch the token: # If there is some filtering possible, we fetch the token:
tokens = await mongo_conn.find_many( tokens = await mongo_conn.find_many(
collection = self.AUTH_COLLECTION, collection = self.AUTH_COLLECTION,
filter = {"key": {"$in": [ObjectId(k) for k in token_keys]}} filter = {"key": {"$in": [ObjectId(k) for k in token_keys]}},
limit = limit
) )
# Done here: # Done here:
+2 -1
View File
@@ -164,8 +164,9 @@ class PGPaymentListData(BaseModel):
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗ # ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("tokenKeys", "tags", "paymentStatus", mode = "before") @field_validator("tokenKeys", "tags", "paymentStatus", mode = "before")
def ensure_list(cls, value): def ensure_unique_list(cls, value):
if not isinstance(value, list): value = [value] if not isinstance(value, list): value = [value]
value = list(set(value))
return value return value
+2 -1
View File
@@ -139,8 +139,9 @@ class MailListRequestData(BaseModel):
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗ # ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("tokenKeys", "tags", mode = "before") @field_validator("tokenKeys", "tags", mode = "before")
def ensure_list(cls, value): def ensure_unique_list(cls, value):
if not isinstance(value, list): value = [value] if not isinstance(value, list): value = [value]
value = list(set(value))
return value return value
+6 -1
View File
@@ -37,7 +37,7 @@ sys.path.append("..")
# For making data behaviour_models: # For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, PastDatetime from pydantic import BaseModel, Field, field_validator, PastDatetime
from typing import Optional, Literal, Union, List from typing import Optional, Literal, Union, List, Any
# My utils: # My utils:
from utils_v2.string import regex from utils_v2.string import regex
@@ -179,6 +179,7 @@ class SMSSendRequestData(BaseModel):
tokenKey: ObjectId = Field(description = "the auth token to use to send this message") tokenKey: ObjectId = Field(description = "the auth token to use to send this message")
message: List[NimbusSMSIndiaMessage] | List[SavvyBulkSMSKenyaMessage] message: List[NimbusSMSIndiaMessage] | List[SavvyBulkSMSKenyaMessage]
tags: List[Any]
# ┏┓ ┏• # ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓ # ┃ ┏┓┏┓╋┓┏┓
@@ -204,6 +205,10 @@ class SMSSendRequestData(BaseModel):
if not isinstance(value, list): value = [value] if not isinstance(value, list): value = [value]
return value return value
@field_validator("tags", mode = "before")
def null_to_list(cls, value):
return [] if value is None else value
# --------------------------------------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------------------------------------