Files
OSIT-AE-API-FastAPI/app/models/hosted_file_link_models.py

67 lines
2.5 KiB
Python

import datetime, pytz
from typing import Dict, List, Optional, Set, Union, ClassVar
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator, root_validator
from app.db_sql import redis_lookup_id_random
from app.lib_general import log, logging
from app.models.common_field_schema import base_fields, default_num_bytes
# ### BEGIN ### API Hosted File Link Models ### Hosted_File_Link_Base() ###
class Hosted_File_Link_Base(BaseModel):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
id: Optional[Union[int, str]] = Field(None)
account_id: Optional[Union[int, str]] = Field(None, **base_fields['account_id_random'])
hosted_file_id: Optional[Union[int, str]] = Field(None, **base_fields['hosted_file_id_random'])
link_to_type: Optional[str] # Should this be renamed to "link_to_obj_type" for clarity?
link_to_id: Optional[Union[int, str]] = Field(None) # Random string or integer
# notes: Optional[str]
created_on: Optional[datetime.datetime] = None
updated_on: Optional[datetime.datetime] = None
# Including other related objects
link_to: Optional[dict] # Should this be renamed to "link_to_obj" or leave off the "_obj"?
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
@root_validator(pre=True)
def map_v3_ids(cls, values):
"""
Vision Transformer:
Map DB keys to clean API keys and strip internal integers.
"""
# 1. Map account_id
if a_rid := values.get('account_id_random'):
if not isinstance(values.get('account_id'), int):
values['account_id'] = a_rid
# 2. Map hosted_file_id
if f_rid := values.get('hosted_file_id_random'):
if not isinstance(values.get('hosted_file_id'), int):
values['hosted_file_id'] = f_rid
# 3. Map link_to_id
if l_rid := values.get('link_to_id_random'):
if not isinstance(values.get('link_to_id'), int):
values['link_to_id'] = l_rid
return values
# Fields that are part of the model (for reading) but should not be saved to the DB table
fields_to_exclude_from_db: ClassVar[list] = [
'link_to'
]
class Config:
underscore_attrs_are_private = True
allow_population_by_field_name = True
fields = base_fields
# ### END ### API Hosted File Link Models ### Hosted_File_Link_Base() ###