(20241212) Reorganizing code to perform core actions in one place.
This commit is contained in:
@@ -0,0 +1,255 @@
|
||||
"""
|
||||
|
||||
AUTHOR:
|
||||
|
||||
Khushal P Soonderji
|
||||
|
||||
DATE:
|
||||
|
||||
Thursday, 5th Dec., 2024.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To provide a structure to normalize input to and output from a standardized LLM wrapper.
|
||||
|
||||
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, Any
|
||||
|
||||
# My utils:
|
||||
from utils_v2.string import regex
|
||||
from utils_v2.date_time import date_time
|
||||
|
||||
# To work with date and time:
|
||||
import datetime
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MACROS / ONE-TIME INIT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# RegEx Patterns:
|
||||
REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$"
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** VARIABLES ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** FUNCTIONS ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class LLMInputMessage(BaseModel):
|
||||
|
||||
role: Literal["system", "ai", "human"] = Field(
|
||||
description = "the role of this message",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
content: str = Field(
|
||||
description = "the message sent by the 'role'",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class LLMInput(BaseModel):
|
||||
|
||||
messages: List[LLMInputMessage]
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@field_validator("messages")
|
||||
def validate_messages(cls, value):
|
||||
|
||||
# Maintain counter(s):
|
||||
system_message_index = -1
|
||||
system_message_count = 0
|
||||
|
||||
# Loop through the messages and check them:
|
||||
for index, message in enumerate(value):
|
||||
|
||||
# For 'system' messages:
|
||||
if message.role == "system":
|
||||
system_message_index = index
|
||||
system_message_count += 1
|
||||
|
||||
# Verify that there is AT MOST ONE 'system' message,
|
||||
# and verify that the 'system' message is the first message:
|
||||
if system_message_count > 1: raise ValueError(f"there can be at most 1 'system' message, found {system_message_count}")
|
||||
if system_message_index > 0: raise ValueError(f"'system' message must always be at index 0, found it at index {system_message_index}")
|
||||
|
||||
# Done here:
|
||||
return value
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class LLMUsageTokens(BaseModel):
|
||||
|
||||
input: int = Field(
|
||||
description = "how many tokens were given in the input",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
output: int = Field(
|
||||
description = "how many tokens were generated as the output",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
total: int = Field(
|
||||
description = "the sum of the input and output tokens",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class LLMOutput(BaseModel):
|
||||
|
||||
ts: AwareDatetime = Field(
|
||||
description = "the time at which the llm was invoked",
|
||||
default_factory = date_time.get_current_utc_date_time,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
messages: List[LLMInputMessage] = Field(
|
||||
description = "the messages that came in that invoked the llm",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
output: str | None = Field(
|
||||
description = "what the llm generated",
|
||||
default = None,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
client: Literal["openai"] = Field(
|
||||
description = "the co./brand that was used to use an llm",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
model: str = Field(
|
||||
description = "to know which model used in the process",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
tokens: LLMUsageTokens = Field(
|
||||
description = "to know how many tokens were used in the process",
|
||||
default = LLMUsageTokens(input = 0, output = 0, total = 0),
|
||||
frozen = True
|
||||
)
|
||||
|
||||
invocationId: Any | None = Field(
|
||||
description = "the id of the document that notes this invocation; useful for reconciliation",
|
||||
frozen = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# llm_messages = [
|
||||
# {
|
||||
# "role": "system",
|
||||
# "content": "You are an office assistant."
|
||||
# },
|
||||
# {
|
||||
# "role": "ai",
|
||||
# "content": "Hello, sir. How may I help you today?"
|
||||
# },
|
||||
# {
|
||||
# "role": "human",
|
||||
# "content": "Please summarize this mail for me..."
|
||||
# }
|
||||
# ]
|
||||
#
|
||||
# llm_input = LLMInput(messages = llm_messages)
|
||||
# print(llm_input.model_dump_json(indent = 4))
|
||||
|
||||
llm_output = LLMOutput(
|
||||
messages=[LLMInputMessage(role='system', content="You are an office assistant. It's Christmas, so definitley respond like Santa Claus."), LLMInputMessage(role='ai', content='Hello, sir. How may I help you today?'), LLMInputMessage(role='human', content='Please summarize this mail for me...')],
|
||||
client = "openai",
|
||||
model = "o1"
|
||||
)
|
||||
@@ -0,0 +1,229 @@
|
||||
"""
|
||||
|
||||
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.core.user 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):
|
||||
|
||||
authTokenId: ObjectId = Field(
|
||||
description = "the id of the document in mongodb that holds this information",
|
||||
frozen = True,
|
||||
default = None,
|
||||
alias = "_id"
|
||||
)
|
||||
|
||||
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 | None = Field(
|
||||
description = "the time (utc) at which authorization was first requested",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
lastRequestTs: AwareDatetime | None = Field(
|
||||
description = "the time (utc) at which authorization was last requested",
|
||||
frozen = False,
|
||||
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
|
||||
)
|
||||
|
||||
firstRefreshTs: AwareDatetime | None = Field(
|
||||
description = "the time (utc) at which the tokens were first refreshed",
|
||||
frozen = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
lastRefreshTs: AwareDatetime | None = 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 = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
token: dict | None = Field(
|
||||
description = "the actual auth tokens of that client; will differ for each client",
|
||||
frozen = False,
|
||||
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 = False
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
def model_dump(self, *args, **kwargs):
|
||||
return super().model_dump(*args, by_alias = True, **kwargs)
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@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))
|
||||
@@ -0,0 +1,266 @@
|
||||
"""
|
||||
|
||||
AUTHOR:
|
||||
|
||||
Khushal P Soonderji
|
||||
|
||||
DATE:
|
||||
|
||||
Thursday, 12th Dec., 2024.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To define how information about files 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, Any, Dict, 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
|
||||
|
||||
# Other data models:
|
||||
from models.data.core.user import CoreUserInfoModel
|
||||
|
||||
# To work with date and time:
|
||||
import datetime
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MACROS / ONE-TIME INIT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** VARIABLES ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** FUNCTIONS ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class CoreFileInfoModel(BaseModel):
|
||||
|
||||
fileId: ObjectId = Field(
|
||||
description = "the id of the document in mongodb that holds this information",
|
||||
frozen = False,
|
||||
default = None,
|
||||
alias = "_id"
|
||||
)
|
||||
|
||||
user: CoreUserInfoModel = Field(
|
||||
description = "to identify the user who owns this file",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
filename: str = Field(
|
||||
description = "the name of this file",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
uploadTs: AwareDatetime = Field(
|
||||
description = "the time (utc) at which this file was uploaded",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
length: int | None = Field(
|
||||
description = "to note the size of the file in bytes",
|
||||
frozen = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
hash: str | None = Field(
|
||||
description = "a simple hash to verify the integrity of the uploaded data; irrelevant for dirs",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
metadata: Dict[str, Any] = Field(
|
||||
description = "any additional data about this file to filter it later",
|
||||
frozen = False,
|
||||
default = {}
|
||||
)
|
||||
|
||||
tags: List[str] = Field(
|
||||
description = "a list of keywords to apply to this file to filter it later",
|
||||
frozen = False,
|
||||
default = [],
|
||||
examples = ["Bank Statement", "PDF", "bhopli@orange.com"]
|
||||
)
|
||||
|
||||
isPrivate: bool = Field(
|
||||
description = "whether this file is a private file, or publicly available",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
def model_dump(self, *args, **kwargs):
|
||||
return super().model_dump(*args, by_alias = True, **kwargs)
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@field_validator("uploadTs", mode = "before")
|
||||
def parse_date_time(cls, value):
|
||||
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
|
||||
|
||||
@field_validator("metadata", mode = "before")
|
||||
def validate_metadata(cls, value):
|
||||
if value is None: value = {}
|
||||
return value
|
||||
|
||||
@field_validator("tags", mode = "before")
|
||||
def validate_tags(cls, value):
|
||||
if value is None: value = []
|
||||
return value
|
||||
|
||||
@field_validator("fileId", mode = "before")
|
||||
def parse_oid(cls, value):
|
||||
try:
|
||||
if isinstance(value, str):
|
||||
value = ObjectId(value)
|
||||
except: pass
|
||||
return value
|
||||
|
||||
# ┏┓ •
|
||||
# ┃┃┏┓┏┓┏┓┏┓┏┓╋┓┏┓┏
|
||||
# ┣┛┛ ┗┛┣┛┗ ┛ ┗┗┗ ┛
|
||||
# ┛
|
||||
|
||||
@property
|
||||
def summary(self):
|
||||
return {
|
||||
"user": self.user.model_dump(),
|
||||
"name": self.name,
|
||||
"createTs": self.createTs,
|
||||
"parentId": self.parentId,
|
||||
"isPrivate": self.isPrivate,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class CoreFileAccessResponseModel(BaseModel):
|
||||
|
||||
success: bool = Field(
|
||||
description = "whether, or not, the operation was successful",
|
||||
frozen = False,
|
||||
default = False
|
||||
)
|
||||
|
||||
message: str | None = Field(
|
||||
description = "useful to note the reason in case of an unsuccessful operation",
|
||||
frozen = False,
|
||||
default = "message not documented"
|
||||
)
|
||||
|
||||
data: Any = Field(
|
||||
description = "the result of the operation",
|
||||
frozen = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
exception: Exception | None = Field(
|
||||
description = "to document any exceptions that came up",
|
||||
frozen = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
from utils_v2.string import json
|
||||
|
||||
file_info = CoreFileInfoModel(
|
||||
_id = "67519cf3a7804fcbc6f12452",
|
||||
user = CoreUserInfoModel(
|
||||
fullName = "Bhopli Narangi",
|
||||
userId = 1,
|
||||
entityId = 2,
|
||||
billingAccountId = 3,
|
||||
departmentId = 4,
|
||||
branchId = 5,
|
||||
industry = "technology"
|
||||
),
|
||||
filename = "Graduation Certificate.png",
|
||||
uploadTs = date_time.get_current_utc_date_time(as_string = False),
|
||||
length = 1024,
|
||||
hash = "abcdefgh12345678",
|
||||
metadata = {
|
||||
"camera": "iPhone 1000 Pro Max XS"
|
||||
},
|
||||
tags = [
|
||||
"important"
|
||||
],
|
||||
parentId = None,
|
||||
isPrivate = True
|
||||
)
|
||||
|
||||
print("FILE INFO MODEL:", json.to_string(file_info.model_dump(), default = str))
|
||||
@@ -0,0 +1,487 @@
|
||||
"""
|
||||
|
||||
AUTHOR:
|
||||
|
||||
Khushal P Soonderji
|
||||
|
||||
DATE:
|
||||
|
||||
Tuesday, 10th Dec., 2024.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To define how information about files 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, Any, Dict, 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
|
||||
|
||||
# Other data models:
|
||||
from models.data.core.user import CoreUserInfoModel
|
||||
|
||||
# To work with date and time:
|
||||
import datetime
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MACROS / ONE-TIME INIT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** VARIABLES ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** FUNCTIONS ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class CoreFileObjectPermissionsModel(BaseModel):
|
||||
|
||||
read: bool = Field(
|
||||
description = "grants permissions to read/view this file/dir",
|
||||
frozen = True,
|
||||
default = True
|
||||
)
|
||||
|
||||
write: bool = Field(
|
||||
description = "grants permissions to write new files this dir; irrelevant for files",
|
||||
frozen = True,
|
||||
default = False
|
||||
)
|
||||
|
||||
delete: bool = Field(
|
||||
description = "grants permissions to delete this file/dir entirely",
|
||||
frozen = True,
|
||||
default = False
|
||||
)
|
||||
|
||||
changePermissions: bool = Field(
|
||||
description = "grants permissions to modify the permissions of this file/dir",
|
||||
frozen = True,
|
||||
default = False
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class CoreFileObjectSharingModel(BaseModel):
|
||||
|
||||
user: CoreUserInfoModel = Field(
|
||||
description = "to identify the user who has access to this file",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
permissionType: Literal["explicit", "inherited"] = Field(
|
||||
description = "to know whether the permission was inherited from a parent dir, or explicitly granted",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
inheritedFromId: ObjectId | None = Field(
|
||||
description = "when some permission was inherited, this tells you the id of the parent",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
permissions: CoreFileObjectPermissionsModel = Field(
|
||||
description = "the permissions that the above mentioned user has to this file",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@field_validator("inheritedFromId", mode = "before")
|
||||
def parse_oid(cls, value):
|
||||
try:
|
||||
if isinstance(value, str):
|
||||
value = ObjectId(value)
|
||||
except: pass
|
||||
return value
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class CoreFileObjectInfoModel(BaseModel):
|
||||
|
||||
fileObjectId: ObjectId = Field(
|
||||
description = "the id of the document in mongodb that holds this information",
|
||||
frozen = False,
|
||||
default = None,
|
||||
alias = "_id"
|
||||
)
|
||||
|
||||
user: CoreUserInfoModel = Field(
|
||||
description = "to identify the user who owns this file/dir",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
isDir: bool = Field(
|
||||
description = "to know whether this object is s file or a directory",
|
||||
frozen = True,
|
||||
default = False
|
||||
)
|
||||
|
||||
name: str = Field(
|
||||
description = "the name of this file/dir",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
createTs: AwareDatetime = Field(
|
||||
description = "the time (utc) at which this file/dir was created",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
length: int | None = Field(
|
||||
description = "to note the size of the file; irrelevant for dirs",
|
||||
frozen = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
hash: str | None = Field(
|
||||
description = "a simple hash to verify the integrity of the uploaded data; irrelevant for dirs",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
metadata: Dict[str, Any] = Field(
|
||||
description = "any additional data about this file/dir to filter it later",
|
||||
frozen = False,
|
||||
default = {}
|
||||
)
|
||||
|
||||
tags: List[str] = Field(
|
||||
description = "a list of keywords to apply to this file/dir to filter it later",
|
||||
frozen = False,
|
||||
default = [],
|
||||
examples = ["Bank Statement", "PDF", "bhopli@orange.com"]
|
||||
)
|
||||
|
||||
parentId: ObjectId | None = Field(
|
||||
description = "to identify the parent dir of this file/dir; null means root dir",
|
||||
frozen = False
|
||||
)
|
||||
|
||||
isPrivate: bool = Field(
|
||||
description = "whether, or not, this file/dir is a private file",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
sharedWith: List[CoreFileObjectSharingModel] = Field(
|
||||
description = "sharing settings; specially relevant when the file/dir is private",
|
||||
frozen = True,
|
||||
default = []
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
def model_dump(self, *args, **kwargs):
|
||||
return super().model_dump(*args, by_alias = True, **kwargs)
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@field_validator("createTs", mode = "before")
|
||||
def parse_date_time(cls, value):
|
||||
return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)
|
||||
|
||||
@field_validator("metadata", mode = "before")
|
||||
def validate_metadata(cls, value):
|
||||
if value is None: value = {}
|
||||
return value
|
||||
|
||||
@field_validator("tags", mode = "before")
|
||||
def validate_tags(cls, value):
|
||||
if value is None: value = []
|
||||
return value
|
||||
|
||||
@field_validator("fileObjectId", "parentId", mode = "before")
|
||||
def parse_oid(cls, value):
|
||||
try:
|
||||
if isinstance(value, str):
|
||||
value = ObjectId(value)
|
||||
except: pass
|
||||
return value
|
||||
|
||||
# ┏┓ •
|
||||
# ┃┃┏┓┏┓┏┓┏┓┏┓╋┓┏┓┏
|
||||
# ┣┛┛ ┗┛┣┛┗ ┛ ┗┗┗ ┛
|
||||
# ┛
|
||||
|
||||
@property
|
||||
def summary(self):
|
||||
return {
|
||||
"user": self.user.model_dump(),
|
||||
"isDir": self.isDir,
|
||||
"name": self.name,
|
||||
"createTs": self.createTs,
|
||||
"parentId": self.parentId,
|
||||
"isPrivate": self.isPrivate,
|
||||
}
|
||||
|
||||
# ┏┓ ┏┓
|
||||
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
|
||||
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
|
||||
|
||||
def is_owner(
|
||||
self,
|
||||
user: CoreUserInfoModel,
|
||||
ignore_null: bool = False
|
||||
):
|
||||
|
||||
"""
|
||||
To check if a given user is the owner of this file object.
|
||||
:param user: The instance of the model that defines the user that needs to be checked.
|
||||
:param ignore_null: Whether, or not, to consider null values in the user to check.
|
||||
:return: True if the given user is the owner, else False.
|
||||
"""
|
||||
|
||||
if self.user.matches(user, ignore_null = ignore_null): return True
|
||||
else: return False
|
||||
|
||||
def has_permission(
|
||||
self,
|
||||
user: CoreUserInfoModel,
|
||||
permission: Literal["read", "write", "delete", "changePermissions"],
|
||||
ignore_null: bool = False
|
||||
) -> bool:
|
||||
|
||||
"""
|
||||
To check if a given user is the owner of this file object.
|
||||
:param user: The instance of the model that defines the user that needs to be checked.
|
||||
:param permission: The permission to check.
|
||||
:param ignore_null: Whether, or not, to consider null values in the user to check.
|
||||
:return: True if the given user is the owner, else False.
|
||||
"""
|
||||
|
||||
# The owner of the file object always has all permissions:
|
||||
if self.is_owner(user, ignore_null = ignore_null): return True
|
||||
|
||||
# Now we check if the given non-owner user has the requested permission:
|
||||
for sharing_data in self.sharedWith:
|
||||
|
||||
# We match the users.
|
||||
# If they don't match, we move to the next user:
|
||||
if not sharing_data.user.matches(
|
||||
user,
|
||||
ignore_null = ignore_null
|
||||
): continue
|
||||
|
||||
# If we found a matching user,
|
||||
# we check for the permission:
|
||||
if getattr(sharing_data.permissions, permission, False):
|
||||
return True
|
||||
|
||||
# If not condition hit, we don't have the permission:
|
||||
return False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class CoreFileObjectAccessResponseModel(BaseModel):
|
||||
|
||||
success: bool = Field(
|
||||
description = "whether, or not, the operation was successful",
|
||||
frozen = False,
|
||||
default = False
|
||||
)
|
||||
|
||||
message: str | None = Field(
|
||||
description = "useful to note the reason in case of an unsuccessful operation",
|
||||
frozen = False,
|
||||
default = "message not documented"
|
||||
)
|
||||
|
||||
result: Any = Field(
|
||||
description = "the result of the operation",
|
||||
frozen = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
exception: Exception | None = Field(
|
||||
description = "to document any exceptions that came up",
|
||||
frozen = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
from utils_v2.string import json
|
||||
|
||||
file_info = CoreFileObjectInfoModel(
|
||||
_id = "67519cf3a7804fcbc6f12452",
|
||||
user = CoreUserInfoModel(
|
||||
fullName = "Bhopli Narangi",
|
||||
userId = 1,
|
||||
entityId = 2,
|
||||
billingAccountId = 3,
|
||||
departmentId = 4,
|
||||
branchId = 5,
|
||||
industry = "technology"
|
||||
),
|
||||
isDir = False,
|
||||
name = "Graduation Certificate.png",
|
||||
createTs = date_time.get_current_utc_date_time(as_string = False),
|
||||
length = 1024,
|
||||
hash = "abcdefgh12345678",
|
||||
metadata = {
|
||||
"camera": "iPhone 1000 Pro Max XS"
|
||||
},
|
||||
tags = [
|
||||
"important"
|
||||
],
|
||||
# parentId = "67519cf3a7804fcbc6f12452",
|
||||
parentId = None,
|
||||
isPrivate = True,
|
||||
sharedWith = [
|
||||
CoreFileObjectSharingModel(
|
||||
user = CoreUserInfoModel(
|
||||
fullName = "Polki Muchhwaali",
|
||||
userId = 6,
|
||||
entityId = 7,
|
||||
billingAccountId = 8,
|
||||
departmentId = 9,
|
||||
branchId = 10,
|
||||
industry = "finance"
|
||||
),
|
||||
permissionType = "explicit",
|
||||
inheritedFromId = None,
|
||||
permissions = CoreFileObjectPermissionsModel(
|
||||
read = True,
|
||||
write = False,
|
||||
delete = False,
|
||||
changePermissions = False
|
||||
)
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
print("FILE INFO MODEL:", json.to_string(file_info.model_dump(), default = str))
|
||||
|
||||
user_bhopli = CoreUserInfoModel(
|
||||
fullName = "Bhopli Narangi",
|
||||
userId = 1,
|
||||
entityId = 2,
|
||||
billingAccountId = 3,
|
||||
departmentId = 4,
|
||||
branchId = 5,
|
||||
industry = "technology"
|
||||
)
|
||||
|
||||
user_bhopli_partial = CoreUserInfoModel(
|
||||
fullName = "Bhopli Narangi",
|
||||
userId = 1,
|
||||
entityId = None,
|
||||
billingAccountId = None,
|
||||
departmentId = 4,
|
||||
branchId = None,
|
||||
industry = "technology"
|
||||
)
|
||||
|
||||
user_polki = CoreUserInfoModel(
|
||||
fullName = "Polki Muchhwaali",
|
||||
userId = 6,
|
||||
entityId = 7,
|
||||
billingAccountId = 8,
|
||||
departmentId = 9,
|
||||
branchId = 10,
|
||||
industry = "finance"
|
||||
)
|
||||
|
||||
print("IS OWNER:", file_info.is_owner(user_bhopli))
|
||||
print("IS OWNER:", file_info.is_owner(user_bhopli_partial, ignore_null = False))
|
||||
print("IS OWNER:", file_info.is_owner(user_bhopli_partial, ignore_null = True))
|
||||
print("HAS PERM:", file_info.has_permission(user_bhopli_partial, permission = "read", ignore_null = False))
|
||||
print("HAS PERM:", file_info.has_permission(user_bhopli_partial, permission = "read", ignore_null = True))
|
||||
print("HAS PERM:", file_info.has_permission(user_polki, permission = "read", ignore_null = True))
|
||||
print("HAS PERM:", file_info.has_permission(user_polki, permission = "delete", ignore_null = True))
|
||||
@@ -0,0 +1,250 @@
|
||||
"""
|
||||
|
||||
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, List, Any
|
||||
|
||||
# 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
|
||||
|
||||
# Data models:
|
||||
from models.core.ai.llm import LLMOutput
|
||||
|
||||
# To work with date and time:
|
||||
import datetime
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MACROS / ONE-TIME INIT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** VARIABLES ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** FUNCTIONS ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class CoreMessageModel(BaseModel):
|
||||
|
||||
messageId: ObjectId = Field(
|
||||
description = "the id of the document in mongodb that holds this information",
|
||||
frozen = True,
|
||||
default = None,
|
||||
alias = "_id"
|
||||
)
|
||||
|
||||
ts: AwareDatetime = Field(
|
||||
description = "the time (utc) at which this message was sent by the sender",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
syncTs: AwareDatetime = Field(
|
||||
description = "the time (utc) at which this message was pulled and stored in your server",
|
||||
frozen = True,
|
||||
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
|
||||
)
|
||||
|
||||
readTs: AwareDatetime | None = Field(
|
||||
description = "the time (utc) at which this message was read by the user",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
markedAsUnread: bool = Field(
|
||||
description = "to note when the user has marked this message as unread",
|
||||
frozen = False,
|
||||
default = 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 | None = 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
|
||||
)
|
||||
|
||||
isSent: bool = Field(
|
||||
description = "to understand whether this message was an incoming message or outgoing message",
|
||||
frozen = False,
|
||||
default = False
|
||||
)
|
||||
|
||||
isBroadcast: bool = Field(
|
||||
description = "to understand if this message was broadcasted or sent one-to-one",
|
||||
frozen = True,
|
||||
default = False
|
||||
)
|
||||
|
||||
sentSuccessfully: bool | None = Field(
|
||||
description = "when a message is an outgoing message, this indicates if the message was send successfully",
|
||||
frozen = False,
|
||||
default = False
|
||||
)
|
||||
|
||||
aiSnippet: LLMOutput | None = Field(
|
||||
description = "holds a short summary generated by ",
|
||||
frozen = False
|
||||
)
|
||||
|
||||
preview: str = Field(
|
||||
description = "a truncated version of the actual textual content of the message",
|
||||
frozen = False
|
||||
)
|
||||
|
||||
message: dict = Field(
|
||||
description = "the actual contents of the message; will differ for each client",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
tags: List[Any] = Field(
|
||||
description = "a list of keywords to apply to this file/dir to filter it later",
|
||||
frozen = False,
|
||||
default = [],
|
||||
examples = ["urgent", "otp", "GST"]
|
||||
)
|
||||
|
||||
usedAi: bool | None = Field(
|
||||
description = "to mark when a sent message was generated by ai; null means the status is not known",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
def model_dump(self, *args, **kwargs):
|
||||
return super().model_dump(*args, by_alias = True, **kwargs)
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@field_validator("ts", "syncTs", "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:
|
||||
if isinstance(value, str):
|
||||
value = ObjectId(value)
|
||||
except: pass
|
||||
return value
|
||||
|
||||
@field_validator("tags", mode = "before")
|
||||
def validate_tags(cls, value):
|
||||
if value is None: value = []
|
||||
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,
|
||||
isSent = False,
|
||||
message = {
|
||||
"from": "bhopli@gmil.com",
|
||||
"to": "hello@thecaoffice.com",
|
||||
"message": "Hello, World!"
|
||||
}
|
||||
)
|
||||
|
||||
print("MESSAGE MODEL:", json.to_string(message.model_dump(), default = str))
|
||||
@@ -0,0 +1,286 @@
|
||||
"""
|
||||
|
||||
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, Any
|
||||
|
||||
# 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):
|
||||
|
||||
paymentId: ObjectId = Field(
|
||||
description = "the id of the document in mongodb that holds this information",
|
||||
frozen = True,
|
||||
default = None,
|
||||
alias = "_id"
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
tags: List[Any] = Field(
|
||||
description = "a list of keywords to apply to this file/dir to filter it later",
|
||||
frozen = False,
|
||||
default = [],
|
||||
examples = ["renewal", "subscription"]
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
def model_dump(self, *args, **kwargs):
|
||||
return super().model_dump(*args, by_alias = True, **kwargs)
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@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:
|
||||
if isinstance(value, str):
|
||||
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
|
||||
|
||||
@field_validator("tags", mode = "before")
|
||||
def validate_tags(cls, value):
|
||||
if value is None: value = []
|
||||
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))
|
||||
@@ -0,0 +1,219 @@
|
||||
"""
|
||||
|
||||
AUTHOR:
|
||||
|
||||
Khushal P Soonderji
|
||||
|
||||
DATE:
|
||||
|
||||
Monday, 9th Dec., 2024.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To define how user info 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 CoreUserInfoModel(BaseModel):
|
||||
|
||||
fullName: str | None = Field(
|
||||
description = "the full name of the user as found in the database",
|
||||
frozen = True,
|
||||
default = None,
|
||||
examples = ["Bhopli Narangi"]
|
||||
)
|
||||
|
||||
userId: int | str | None = Field(
|
||||
description = "the id of the user as found in the database",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
entityId: int | str | None = Field(
|
||||
description = "the id of the entity with which this user is associated",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
billingAccountId: int | str | None = Field(
|
||||
description = "the id of the billing account with which this user is associated",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
departmentId: int | str | None = Field(
|
||||
description = "the id of the dept. in which this user is working",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
branchId: int | str | None = Field(
|
||||
description = "the id of the branch in which this user is working",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
industry: str | None = Field(
|
||||
description = "the name of the industry this user is working in",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "ignore"
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
# ┏┓ ┏┓
|
||||
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
|
||||
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
|
||||
|
||||
def matches(
|
||||
self,
|
||||
other_user: 'CoreUserInfoModel',
|
||||
ignore_null: bool = False
|
||||
) -> bool:
|
||||
|
||||
"""
|
||||
To check if another instance of this user model matches this instance of the user model.
|
||||
:param other_user: The other user to check. That user's value must match this user's values to return a positive
|
||||
result. Whether, or not, null values are matched will be determined by the next param.
|
||||
:param ignore_null: If set to True, null values IN THE OTHER USER will not be compared during the matching.
|
||||
:return: True if they match, else False.
|
||||
"""
|
||||
|
||||
# Start by assuming success:
|
||||
are_matching = True
|
||||
|
||||
# Iterate through the required items:
|
||||
for k, v in other_user.model_dump().items():
|
||||
|
||||
# Do not consider fields that are nulls if asked to ignore them:
|
||||
if ignore_null and v is None: continue
|
||||
|
||||
# Extract the corresponding value from the other user,
|
||||
# and test it for being equal:
|
||||
_v = getattr(self, k, None)
|
||||
if (
|
||||
(not isinstance(v, type(_v))) or
|
||||
(v != _v)
|
||||
):
|
||||
are_matching = False
|
||||
break
|
||||
|
||||
# Done here:
|
||||
return are_matching
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
from utils_v2.string import json
|
||||
|
||||
user_bhopli = CoreUserInfoModel(
|
||||
fullName = "Bhopli Narangi",
|
||||
userId = 1,
|
||||
entityId = 2,
|
||||
billingAccountId = 3,
|
||||
departmentId = 4,
|
||||
branchId = 5,
|
||||
industry = "technology"
|
||||
)
|
||||
|
||||
user_bhopli_partial = CoreUserInfoModel(
|
||||
fullName = "Bhopli Narangi",
|
||||
userId = 1,
|
||||
entityId = None,
|
||||
billingAccountId = None,
|
||||
departmentId = 4,
|
||||
branchId = None,
|
||||
industry = "technology"
|
||||
)
|
||||
|
||||
user_polki = CoreUserInfoModel(
|
||||
fullName = "Polki Muchhwaali",
|
||||
userId = 6,
|
||||
entityId = 7,
|
||||
billingAccountId = 8,
|
||||
departmentId = 9,
|
||||
branchId = 10,
|
||||
industry = "finance"
|
||||
)
|
||||
|
||||
print(user_bhopli.matches(user_bhopli_partial, ignore_null = False))
|
||||
print(user_bhopli.matches(user_bhopli_partial, ignore_null = True))
|
||||
print(user_bhopli.matches(user_polki, ignore_null = False))
|
||||
print(user_bhopli.matches(user_polki, ignore_null = True))
|
||||
Reference in New Issue
Block a user