(20241209) Standardizing the data models for the database. Started with mail authentication.

This commit is contained in:
2024-12-09 12:05:12 +05:30
parent 30088dcea4
commit e2c9200941
11 changed files with 262 additions and 244 deletions
View File
+225
View File
@@ -0,0 +1,225 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Saturday, 7th Dec., 2024.
OBJECTIVE:
To define how auth tokens will be stored in the database.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatetime
from typing import Optional, Literal, Union
# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time
# Other core models:
from models.data.core.user_info import CoreUserInfoModel
# To work with MongoDB:
from bson.objectid import ObjectId
# To work with date and time:
import datetime
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
class CoreAuthTokenModel(BaseModel):
version: str = Field(
description = "a hint about the version no. of this message",
min_length = 1,
frozen = True,
default = "1.0.0"
)
serviceType: Literal["email", "sms", "chat"] = Field(
description = "the kind of service this message was sent/received from",
frozen = True
)
client: Literal[
"gmail", "outlook", # ...................... Mail Clients
"telegram", "whatsapp", # .................. Chat Clients
"nimbusSmsIndia", "savvyBulkSmsKenya", # ... SMS Clients
"razorpay", "safaricomMPesaExpress" # ...... Payment Gateways
] = Field(
description = "the third-part client that was used",
frozen = True
)
authType: Literal["oauth", "auth"] = Field(
description = "the type of authentication procedure used",
frozen = True
)
firstRequestTs: AwareDatetime = Field(
description = "the time (utc) at which authorization was first requested",
frozen = True,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
lastRequestTs: AwareDatetime = Field(
description = "the time (utc) at which authorization was last requested",
frozen = False,
default = None
)
firstRefreshTs: AwareDatetime = Field(
description = "the time (utc) at which the tokens were first refreshed",
frozen = False,
default = None
)
lastRefreshTs: AwareDatetime = Field(
description = "the time (utc) at which the tokens were last refreshed",
frozen = False,
default = None
)
auth: dict | None = Field(
description = "any direct auth details like api keys or passwords; will differ for each client",
frozen = True,
default = None
)
token: dict | None = Field(
description = "the actual auth tokens of that client; will differ for each client",
frozen = True,
default = None
)
user: CoreUserInfoModel = Field(
description = "how you identify your user",
frozen = True
)
clientUserId: dict = Field(
description = "how third-party client identifies the same user",
frozen = True
)
status: Literal["pending", "active", "disabled"] = Field(
description = "to indicate the status of this account",
frozen = False,
default = "pending"
)
syncFreq: Literal[60, 300, 1500] = Field(
description = "the no. of seconds after which to poll for updates from the client (if applicable)",
frozen = False,
default = 300
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "allow"
arbitrary_types_allowed = True
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator(
"firstRequestTs",
"lastRequestTs", "firstRefreshTs", "lastRefreshTs",
mode = "before"
)
def parse_date_time(cls, value):
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
from utils_v2.string import json
auth_token = CoreAuthTokenModel(
serviceType = "email",
client = "gmail",
authType = "oauth",
firstRequestTs = date_time.get_current_utc_date_time(as_string = False),
lastRequestTs = date_time.get_current_utc_date_time(as_string = False),
firstRefreshTs = date_time.get_current_utc_date_time(as_string = False),
lastRefreshTs = date_time.get_current_utc_date_time(as_string = False),
token = {
"username": "testing123",
"password": "abcdefgh"
},
user = {
"userId": 0,
"entityId": 1,
"billingAccountId": 2,
"fullName": "Bhopli"
},
clientUserId = {
"email": "bhopli@gmail.com"
}
)
print("AUTH-TOKEN MODEL:", json.to_string(auth_token.model_dump(), default = str))
+183
View File
@@ -0,0 +1,183 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Saturday, 7th Dec., 2024.
OBJECTIVE:
To define how messages will be stored in the database.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatetime
from typing import Optional, Literal, Union
# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time
# To work with MongoDB:
from bson.objectid import ObjectId
# To work with date and time:
import datetime
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
class CoreMessageModel(BaseModel):
version: str = Field(
description = "a hint about the version no. of this message",
min_length = 1,
frozen = True,
default = "1.0.0"
)
ts: AwareDatetime = Field(
description = "the time (utc) at which this message was sent by the sender",
frozen = True
)
readTs: AwareDatetime = Field(
description = "the time (utc) at which this message was read and stored by your server",
frozen = True,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
tokenId: ObjectId = Field(
description = "the id of the auth token that is associated with this message",
frozen = True
)
serviceType: Literal["email", "sms", "chat"] = Field(
description = "the kind of service this message was sent/received from",
frozen = True
)
client: Literal[
"gmail", "outlook", # ...................... Mail Clients
"telegram", "whatsapp", # .................. Chat Clients
"nimbusSmsIndia", "savvyBulkSmsKenya", # ... SMS Clients
] = Field(
description = "the third-part client that was used",
frozen = True
)
clientMessageId: str | int = Field(
description = "how the client identifies this message",
frozen = True
)
clientThreadId: str | int | None = Field(
description = "how the client identifies the chat/thread in which this message was sent/received",
frozen = True,
default = None
)
payload: dict = Field(
description = "the actual contents of the message; will differ for each client",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "allow"
arbitrary_types_allowed = True
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("ts", "readTs", mode = "before")
def parse_date_time(cls, value):
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
@field_validator("tokenId", mode = "before")
def parse_oid(cls, value):
try: value = ObjectId(value)
except: pass
return value
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
from utils_v2.string import json
message = CoreMessageModel(
ts = date_time.get_current_utc_date_time(as_string = False),
tokenId = "67519cf3a7804fcbc6f12452",
serviceType = "email",
client = "gmail",
clientMessageId = 123,
clientThreadId = 456,
payload = {
"from": "bhopli@gmil.com",
"to": "hello@thecaoffice.com",
"message": "Hello, World!"
}
)
print("MESSAGE MODEL:", json.to_string(message.model_dump(), default = str))
+269
View File
@@ -0,0 +1,269 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Saturday, 7th Dec., 2024.
OBJECTIVE:
To define how payment transactions will be stored in the database.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatetime
from typing import Optional, Literal, Union, List
# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time
# To work with MongoDB:
from bson.objectid import ObjectId
# To work with date and time:
import datetime
# To work with currencies:
import pycountry
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
class PaymentEvent(BaseModel):
eventTs: AwareDatetime = Field(
description = "to know the date and time (utc) of this update",
frozen = True
)
initByPG: bool = Field(
description = "to figure out whether the payment gateway initiated this event or we did",
frozen = True
)
httpCode: int | None = Field(
description = "the http code generated by the event",
frozen = True,
examples = [200, 400, 401]
)
payload: dict = Field(
description = "the json payload or set of query params received from an event from the payment gateway",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "allow"
arbitrary_types_allowed = True
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("eventTs", mode = "before")
def parse_date_time(cls, value):
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
# ---------------------------------------------------------------------------------------------------------------------
class CorePaymentModel(BaseModel):
version: str = Field(
description = "a hint about the version no. of this message",
min_length = 1,
frozen = True,
default = "1.0.0"
)
paymentStatus: Literal[
"initFailed", # ... When we tried to initiate the request, but the payment gateway (PG) rejected it.
"initiated", # .... When we made a successful payment request, or the customer initiated one from the PG.
"failed", # ....... When the customer tried paying, but it failed (e.g.: because of an incorrect pin).
"rejected", # ..... When the customer explicitly rejected the payment.
"authorized", # ... When the customer made the payment (but it hasn't been settled in your account yet).
"settled", # ...... When the PG sends the money to your account.
"refunded", # ..... When the money was refunded to the client.
] = Field(
description = "the status of the payment request to see what stage of the process we are in",
frozen = False
)
lastEventTs: AwareDatetime = Field(
description = "the time (utc) at which the latest payment event occurred",
frozen = True
)
tokenId: ObjectId = Field(
description = "the id of the auth token that is associated with this payment",
frozen = True
)
amount: float | int = Field(
description = "the amount of money being requested"
)
currencyCode: str = Field(
description = "the three-letter ISO 4217 code to identify the currency",
frozen = True,
examples = ["INR", "USD", "KES"]
)
metadata: dict | None = Field(
description = "any arbitrary amount of data to identify the user and payment details",
frozen = True
)
client: Literal["razorpay", "safaricomMPesaExpress"] = Field(
description = "the third-part client that was used",
frozen = True
)
clientPaymentReferenceId: int | str = Field(
description = "the reference id given by the third-party client",
frozen = False,
default = None
)
events: List[PaymentEvent] = Field(
description = "an array of all the events that happened in the process of this payment",
frozen = False
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "allow"
arbitrary_types_allowed = True
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("lastEventTs", mode = "before")
def parse_date_time(cls, value):
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
@field_validator("tokenId", mode = "before")
def parse_oid(cls, value):
try: value = ObjectId(value)
except: pass
return value
@field_validator("currencyCode", mode = "before")
def validate_currency(cls, value):
currency = pycountry.currencies.get(alpha_3 = value)
if currency is None: raise ValueError("invalid currency code, please use iso 4217 standard")
return value
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
from utils_v2.string import json
now = date_time.get_current_utc_date_time(as_string = False)
payment = CorePaymentModel(
paymentStatus = "authorized",
tokenId = "67519cf3a7804fcbc6f12452",
amount = 1.00,
currencyCode = "INR",
metadata = {
"userId": 1,
"name": "Bhopli"
},
client = "razorpay",
clientPaymentReferenceId = "txn_123_abc",
lastEventTs = now,
events = [
PaymentEvent(
eventTs = now - datetime.timedelta(minutes = 1, seconds = 12),
initByPG = True,
httpCode = None,
payload = {
"status": "captured",
"from": "Barfi",
}
),
PaymentEvent(
eventTs = now,
initByPG = True,
httpCode = None,
payload = {
"status": "authorized",
"from": "Barfi",
"amount": -100.00,
"description": "meow"
}
)
]
)
print("PAYMENT TXN. MODEL:", json.to_string(payment.model_dump(), default = str))
+28 -97
View File
@@ -6,11 +6,11 @@
DATE:
Saturday, 7th Dec., 2024.
Monday, 9th Dec., 2024.
OBJECTIVE:
To define how auth tokens will be stored in the database.
To define how user info will be stored in the database.
REFERENCES:
@@ -77,7 +77,7 @@ import datetime
# *****************************************************************************************************************
class CoreAuthTokenModel(BaseModel):
class CoreUserInfoModel(BaseModel):
version: str = Field(
description = "a hint about the version no. of this message",
@@ -86,72 +86,40 @@ class CoreAuthTokenModel(BaseModel):
default = "1.0.0"
)
serviceType: Literal["email", "sms", "chat"] = Field(
description = "the kind of service this message was sent/received from",
frozen = True
)
client: Literal[
"gmail", "outlook", # ...................... Mail Clients
"telegram", "whatsapp", # .................. Chat Clients
"nimbusSmsIndia", "savvyBulkSmsKenya", # ... SMS Clients
"razorpay", "safaricomMPesaExpress" # ...... Payment Gateways
] = Field(
description = "the third-part client that was used",
frozen = True
)
authType: Literal["oauth", "auth"] = Field(
description = "the type of authentication procedure used",
frozen = True
)
firstRequestTs: AwareDatetime = Field(
description = "the time (utc) at which authorization was first requested",
frozen = True
)
lastRequestTs: AwareDatetime = Field(
description = "the time (utc) at which authorization was last requested",
frozen = False
)
firstRefreshTs: AwareDatetime = Field(
description = "the time (utc) at which the tokens were first refreshed",
frozen = False
)
lastRefreshTs: AwareDatetime = Field(
description = "the time (utc) at which the tokens were last refreshed",
frozen = False
)
token: dict | None = Field(
description = "the actual auth tokens of that client; will differ for each client",
fullName: str | None = Field(
description = "the full name of the user as found in the database",
frozen = True,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
examples = ["Bhopli Narangi"]
)
user: dict = Field(
description = "how you identify your user",
userId: int | str | None = Field(
description = "the id of the user as found in the database",
frozen = True
)
clientUserId: dict = Field(
description = "how third-party client identifies the same user",
entityId: int | str | None = Field(
description = "the id of the entity with which this user is associated",
frozen = True
)
status: Literal["active", "disabled"] = Field(
description = "to indicate the status of this account",
frozen = False,
default = "active"
billingAccountId: int | str | None = Field(
description = "the id of the billing account with which this user is associated",
frozen = True
)
syncFreq: Literal[60, 300, 1500] = Field(
description = "the no. of seconds after which to poll for updates from the client (if applicable)",
frozen = False,
default = 300
departmentId: int | str | None = Field(
description = "the id of the dept. in which this user is working",
frozen = True
)
branchId: int | str | None = Field(
description = "the id of the branch in which this user is working",
frozen = True
)
industry: str | None = Field(
description = "the name of the industry this user is working in",
frozen = True
)
# ┏┓ ┏•
@@ -160,21 +128,9 @@ class CoreAuthTokenModel(BaseModel):
# ┛
class Config:
extra = "allow"
extra = "ignore"
arbitrary_types_allowed = True
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator(
"firstRequestTs",
"lastRequestTs", "firstRefreshTs", "lastRefreshTs",
mode = "before"
)
def parse_date_time(cls, value):
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
# *****************************************************************************************************************
# ***** ****
@@ -184,29 +140,4 @@ class CoreAuthTokenModel(BaseModel):
if __name__ == "__main__":
from utils_v2.string import json
auth_token = CoreAuthTokenModel(
serviceType = "email",
client = "gmail",
authType = "oauth",
firstRequestTs = date_time.get_current_utc_date_time(as_string = False),
lastRequestTs = date_time.get_current_utc_date_time(as_string = False),
firstRefreshTs = date_time.get_current_utc_date_time(as_string = False),
lastRefreshTs = date_time.get_current_utc_date_time(as_string = False),
token = {
"username": "testing123",
"password": "abcdefgh"
},
user = {
"userId": 0,
"entityId": 1,
"billingAccountId": 2,
"fullName": "Bhopli"
},
clientUserId = {
"email": "bhopli@gmail.com"
}
)
print("AUTH-TOKEN MODEL:", json.to_string(auth_token.model_dump(), default = str))
pass