102 lines
3.5 KiB
Python
102 lines
3.5 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 Archive Models ### Archive_Base() ###
|
|
class Archive_Base(BaseModel):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# --- Standardized Vision IDs (Strings for API, Integers for DB) ---
|
|
id: Optional[Union[int, str]] = Field(None, **base_fields['archive_id_random'])
|
|
archive_id: Optional[Union[int, str]] = Field(None, **base_fields['archive_id_random'])
|
|
account_id: Optional[Union[int, str]] = Field(None, **base_fields['account_id_random'])
|
|
|
|
# --- Standardized Legacy / Internal IDs (Excluded) ---
|
|
id_random: Optional[str] = Field(None, alias='archive_id_random', exclude=True)
|
|
account_id_random: Optional[str] = Field(None, exclude=True)
|
|
|
|
@root_validator(pre=True)
|
|
def map_v3_ids(cls, values):
|
|
"""
|
|
Vision Transformer:
|
|
Map DB keys to clean API keys and strip internal integers during READ operations.
|
|
During CREATE (POST) operations, we ensure resolved integers are preserved.
|
|
"""
|
|
# 1. Map Random Strings to Clean Names
|
|
rid = values.get('id_random') or values.get('archive_id_random')
|
|
if rid and isinstance(rid, str):
|
|
values['id'] = rid
|
|
values['archive_id'] = rid
|
|
|
|
if a_rid := values.get('account_id_random'): values['account_id'] = a_rid
|
|
|
|
# 2. Prevent leakage of integers during API responses (Vision Standard)
|
|
for k in ['id', 'archive_id', 'account_id']:
|
|
val = values.get(k)
|
|
if val is not None and not isinstance(val, str):
|
|
if values.get(f'{k}_random') or (k=='id' and values.get('id_random')):
|
|
del values[k]
|
|
|
|
return values
|
|
|
|
archive_type_id: Optional[int]
|
|
archive_type: Optional[str]
|
|
|
|
name: Optional[str]
|
|
description: Optional[str]
|
|
|
|
content_html: Optional[str]
|
|
content_json: Optional[str]
|
|
content_url: Optional[str]
|
|
content_url_text: Optional[str]
|
|
|
|
original_datetime: Optional[datetime.datetime] = None
|
|
original_timezone: Optional[str] = None
|
|
original_location: Optional[str] = None
|
|
original_address_id: Optional[int] = None
|
|
|
|
original_url: Optional[str]
|
|
original_url_text: Optional[str]
|
|
|
|
archive_content_count: Optional[int] # archive content count using view
|
|
|
|
meta_data: Optional[str]
|
|
access_key: Optional[str]
|
|
|
|
sort_by: Optional[str]
|
|
sort_by_desc: Optional[bool]
|
|
|
|
enable: Optional[bool]
|
|
enable_from: Optional[datetime.datetime] = None
|
|
enable_to: Optional[datetime.datetime] = None
|
|
|
|
cfg_json: Optional[Union[Json, None]]
|
|
|
|
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 other related objects
|
|
archive_content_list: Optional[list] # Archive_Content_Base()
|
|
|
|
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
|
|
|
class Config:
|
|
underscore_attrs_are_private = True
|
|
allow_population_by_field_name = False
|
|
fields = base_fields
|
|
# ### END ### API Archive Models ### Archive_Base() ###
|