ID Vision Phase 2: Standardize Page, Post, Person, Organization, and Hosted File objects
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import datetime, pytz
|
||||
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator
|
||||
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
|
||||
@@ -14,17 +14,10 @@ class Hosted_File_Base(BaseModel):
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
id_random: Optional[str] = Field(
|
||||
**base_fields['hosted_file_id_random'],
|
||||
alias = 'hosted_file_id_random',
|
||||
# default_factory = lambda:secrets.token_urlsafe(default_num_bytes),
|
||||
)
|
||||
id: Optional[int] = Field(
|
||||
alias = 'hosted_file_id'
|
||||
)
|
||||
|
||||
account_id_random: Optional[str]
|
||||
account_id: Optional[int]
|
||||
# --- Standardized Vision IDs (Strings) ---
|
||||
id: Optional[str] = Field(None, **base_fields['hosted_file_id_random'])
|
||||
hosted_file_id: Optional[str] = Field(None, **base_fields['hosted_file_id_random'])
|
||||
account_id: Optional[str] = Field(None, **base_fields['account_id_random'])
|
||||
|
||||
hash_sha256: Optional[str]
|
||||
title: Optional[str]
|
||||
@@ -39,21 +32,11 @@ class Hosted_File_Base(BaseModel):
|
||||
mimetype: Optional[str]
|
||||
size: Optional[int] # In bytes
|
||||
|
||||
# cloud_storage: Optional[str]
|
||||
# owner_user_id: Optional[int]
|
||||
# group_user_id: Optional[str]
|
||||
|
||||
# package_name: Optional[str]
|
||||
|
||||
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
|
||||
|
||||
# metadata: Optional[str]
|
||||
|
||||
enable: Optional[bool]
|
||||
# enable_from: Optional[datetime.datetime] = None
|
||||
# enable_to: Optional[datetime.datetime] = None
|
||||
|
||||
hide: Optional[bool]
|
||||
priority: Optional[bool]
|
||||
@@ -78,28 +61,29 @@ class Hosted_File_Base(BaseModel):
|
||||
|
||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||
|
||||
#@validator('hosted_file_id_random', always=True)
|
||||
def hosted_file_id_random_copy(cls, v, values, **kwargs):
|
||||
if values['id_random']:
|
||||
return values['id_random']
|
||||
return None
|
||||
|
||||
@validator('id', always=True)
|
||||
def hosted_file_id_lookup(cls, v, values, **kwargs):
|
||||
if isinstance(v, int) and v > 0: return v
|
||||
elif id_random := values.get('id_random'):
|
||||
return redis_lookup_id_random(record_id_random=id_random, table_name='hosted_file')
|
||||
return None
|
||||
|
||||
@validator('account_id', always=True)
|
||||
def account_id_lookup(cls, v, values, **kwargs):
|
||||
if isinstance(v, int) and v > 0: return v
|
||||
elif id_random := values.get('account_id_random'):
|
||||
return redis_lookup_id_random(record_id_random=id_random, table_name='account')
|
||||
return None
|
||||
@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 Random Strings to Clean Names
|
||||
if rid := values.get('id_random') or values.get('hosted_file_id_random'):
|
||||
values['id'] = rid
|
||||
values['hosted_file_id'] = rid
|
||||
|
||||
if a_rid := values.get('account_id_random'):
|
||||
values['account_id'] = a_rid
|
||||
|
||||
# 2. Prevent "Collision Population"
|
||||
for k in ['id', 'account_id']:
|
||||
if k in values and not isinstance(values[k], str):
|
||||
del values[k]
|
||||
|
||||
return values
|
||||
|
||||
class Config:
|
||||
underscore_attrs_are_private = True
|
||||
allow_population_by_field_name = True
|
||||
allow_population_by_field_name = False
|
||||
fields = base_fields
|
||||
# ### END ### API Hosted File Models ### Hosted_File_Base() ###
|
||||
|
||||
Reference in New Issue
Block a user