(20241210) Started working on files.

This commit is contained in:
2024-12-10 19:56:25 +05:30
parent 4c3d546065
commit 570a28ef38
7 changed files with 828 additions and 57 deletions
+1
View File
@@ -197,6 +197,7 @@ class CoreAuthTokenModel(BaseModel):
if __name__ == "__main__":
from utils_v2.string import json
auth_token = CoreAuthTokenModel(
+284
View File
@@ -0,0 +1,284 @@
"""
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 CoreFilePermissionsModel(BaseModel):
read: bool = Field(
description = "grants permissions to read/view this file",
frozen = True,
default = True
)
delete: bool = Field(
description = "grants permissions to delete this file entirely",
frozen = True,
default = False
)
changePermissions: bool = Field(
description = "grants permissions to modify the permissions of this file",
frozen = True,
default = False
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "allow"
arbitrary_types_allowed = True
# ---------------------------------------------------------------------------------------------------------------------
class CoreFileSharingModel(BaseModel):
user: CoreUserInfoModel = Field(
description = "to identify the user who has access to this file",
frozen = True
)
permissions: CoreFilePermissionsModel = Field(
description = "the permissions that the above mentioned user has to this file",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "allow"
arbitrary_types_allowed = True
# ---------------------------------------------------------------------------------------------------------------------
class CoreFileInfoModel(BaseModel):
version: str = Field(
description = "a hint about the version no. of this message",
min_length = 1,
frozen = True,
default = "2.0.0"
)
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 message was sent by the sender",
frozen = True
)
length: int = Field(
description = "to note when the user has marked this message as unread",
frozen = False,
default = False
)
hash: str = Field(
description = "a simple hash to verify the integrity of the uploaded data",
frozen = True
)
metadata: Dict[str, Any] = Field(
description = "any addition 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"]
)
parentId: ObjectId | None = Field(
description = "to identify the parent directory of this file; null means root directory",
frozen = False
)
isPrivate: bool = Field(
description = "whether, or not, this file is a private file",
frozen = True
)
sharedWith: List[CoreFileSharingModel] = Field(
description = "sharing settings; specially relevant when the file is private",
frozen = True,
default = []
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "allow"
arbitrary_types_allowed = True
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@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("parentId", 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
file_info = CoreFileInfoModel(
user = CoreUserInfoModel(
fullName = "Bhopli Narangi",
userId = 1,
entityId = 2,
billingAccountId = 3,
departmentId = 4,
branchId = 5,
industry = "fashion"
),
filename = "Giga-Cat.png",
uploadTs = date_time.get_current_utc_date_time(as_string = False),
length = 1024,
hash = "abcdefgh",
metadata = {
"camera": "iPhone 1000 Pro Max XS"
},
tags = [
"image",
"cute",
"cat"
],
parentId = "67519cf3a7804fcbc6f12452",
isPrivate = True,
sharedWith = [
CoreFileSharingModel(
user = CoreUserInfoModel(
fullName = "Polki Muchhwaali",
userId = 6,
entityId = 7,
billingAccountId = 8,
departmentId = 9,
branchId = 10,
industry = "entertainment"
),
permissions = CoreFilePermissionsModel(
read = True,
delete = False,
changePermissions = False
)
)
]
)
print("FILE INFO MODEL:", json.to_string(file_info.model_dump(), default = str))
+21 -9
View File
@@ -91,12 +91,24 @@ class CoreMessageModel(BaseModel):
frozen = True
)
readTs: AwareDatetime = Field(
description = "the time (utc) at which this message was read and stored by your server",
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
@@ -127,10 +139,9 @@ class CoreMessageModel(BaseModel):
default = None
)
isInward: bool = Field(
description = "to understand whether this message was an inward message or outward message",
frozen = True,
default = True
isSent: bool = Field(
description = "to understand whether this message was an incoming message or outgoing message",
frozen = True
)
isBroadcast: bool = Field(
@@ -145,7 +156,7 @@ class CoreMessageModel(BaseModel):
default = False
)
payload: dict = Field(
message: dict = Field(
description = "the actual contents of the message; will differ for each client",
frozen = True
)
@@ -163,7 +174,7 @@ class CoreMessageModel(BaseModel):
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("ts", "readTs", mode = "before")
@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)
@@ -191,7 +202,8 @@ if __name__ == "__main__":
client = "gmail",
clientMessageId = 123,
clientThreadId = 456,
payload = {
isSent = False,
message = {
"from": "bhopli@gmil.com",
"to": "hello@thecaoffice.com",
"message": "Hello, World!"
@@ -89,37 +89,44 @@ 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(
userId: int | str | None = Field(
description = "the id of the user as found in the database",
frozen = True
frozen = True,
default = None
)
entityId: int | str | None = Field(
entityId: int | str | None = Field(
description = "the id of the entity with which this user is associated",
frozen = True
frozen = True,
default = None
)
billingAccountId: int | str | None = Field(
billingAccountId: int | str | None = Field(
description = "the id of the billing account with which this user is associated",
frozen = True
frozen = True,
default = None
)
departmentId: int | str | None = Field(
departmentId: int | str | None = Field(
description = "the id of the dept. in which this user is working",
frozen = True
frozen = True,
default = None
)
branchId: int | str | None = Field(
branchId: int | str | None = Field(
description = "the id of the branch in which this user is working",
frozen = True
frozen = True,
default = None
)
industry: str | None = Field(
industry: str | None = Field(
description = "the name of the industry this user is working in",
frozen = True
frozen = True,
default = None
)
# ┏┓ ┏•
@@ -140,4 +147,17 @@ class CoreUserInfoModel(BaseModel):
if __name__ == "__main__":
pass
from utils_v2.string import json
user_info = CoreUserInfoModel(
fullName = "Bhopli Narangi",
userId = 1,
entityId = 2,
billingAccountId = 3,
departmentId = 4,
branchId = 5,
industry = "fashion"
)
print("USER INFO MODEL:", json.to_string(user_info.model_dump(), default = str))