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

92 lines
3.3 KiB
Python

import datetime, pytz
from typing import Dict, List, Optional, Set, Union
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 Models ### Hosted_File_Base() ###
class Hosted_File_Base(BaseModel):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
# --- Standardized Vision IDs (Strings) ---
id: Optional[Union[int, str]] = Field(None, **base_fields['hosted_file_id_random'])
hosted_file_id: Optional[Union[int, str]] = Field(None, **base_fields['hosted_file_id_random'])
account_id: Optional[Union[int, str]] = Field(None, **base_fields['account_id_random'])
hash_sha256: Optional[str]
title: Optional[str]
description: Optional[str]
version: Optional[int]
subdirectory_path: Optional[str] = Field(None, exclude=True) # NOTE: This will frequently only contain numbers, but it still needs to be a string
filename: Optional[str]
filename_no_ext: Optional[str]
filename_w_ext: Optional[str]
extension: Optional[str]
content_type: Optional[str]
mimetype: Optional[str]
size: Optional[int] # In bytes
already_exists: Optional[str] # This will probably only be populated on upload results
copy_timer: Optional[str] # This will probably only be populated on upload results
saved: Optional[str] # This will probably only be populated on upload results
enable: Optional[bool]
hide: Optional[bool]
priority: Optional[bool]
sort: Optional[int]
group: Optional[str]
notes: Optional[str]
created_on: Optional[datetime.datetime] = None
updated_on: Optional[datetime.datetime] = None
# Including convenience data
# This is only for convenience. Probably going to keep unless it causes a problem.
hosted_file_found_check: Optional[bool] = Field(
alias = 'found_check'
)
hosted_file_size_check: Optional[int] = Field( # File size in bytes
alias = 'size_check'
)
# Including other related objects
hosted_file_link_list: Optional[list] # Hosted_File_Base()
_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. Capture the random ID string
rid = values.get('id_random') or values.get('hosted_file_id_random')
# 2. Map Random Strings to Clean Names for the Frontend
# We always want the string version in 'id' and 'hosted_file_id' for the API response
if rid:
values['id'] = rid
values['hosted_file_id'] = rid
if a_rid := values.get('account_id_random'):
# If we have a random account ID string, use it for the Vision API
values['account_id'] = a_rid
return values
class Config:
underscore_attrs_are_private = True
allow_population_by_field_name = False
fields = base_fields
# ### END ### API Hosted File Models ### Hosted_File_Base() ###