- Replace integer `id` (alias archive_content_id) with Vision string fields:
`id: Optional[str]` and `archive_content_id: Optional[str]` — both always
hold the random string ID, never the DB integer.
- Add `root_validator(pre=True)` (map_v3_ids) that maps id_random /
archive_content_id_random → id and archive_content_id, with collision
prevention to reject any integer that arrives in these fields.
- Remove old `archive_content_id_lookup` integer validator (superseded by
sanitize_payload + root_validator).
- Keep `id_random` (alias archive_content_id_random) in responses for
backward compatibility; add id, archive_content_id, id_random to
fields_to_exclude_from_db so they never appear in INSERT/UPDATE payloads.
Generic CRUD layer safety net (post_obj + post_child_obj):
- After building resp_data on create, swap any integer {obj_type}_id with
the corresponding {obj_type}_id_random value — catches models not yet
migrated to Vision IDs.
- Fix return_obj=False fallback to return obj_id as the random string.
Docs: add Section 3D to GUIDE__AE_API_V3_for_Frontend.md documenting the
Vision ID convention — {obj_type}_id is always the random string; the
_id_random suffix is a legacy artifact that frontend code should phase out.
Fixes: POST /v3/crud/archive/{id}/archive_content/ returning integer ID,
breaking the subsequent PATCH flow (422 min_length validation failure).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
193 lines
7.1 KiB
Python
193 lines
7.1 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 Archive Content Models ### Archive_Content_Base() ###
|
|
class Archive_Content_Base(BaseModel):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# def testing(test_var=None):
|
|
# log.debug(test_var)
|
|
# return test_var
|
|
|
|
# --- Vision IDs (primary public identifiers — always random strings) ---
|
|
id: Optional[str] = Field(None, **base_fields['archive_content_id_random'])
|
|
archive_content_id: Optional[str] = Field(None, **base_fields['archive_content_id_random'])
|
|
# Legacy alias kept for backward compatibility; populated by root_validator
|
|
id_random: Optional[str] = Field(None, alias='archive_content_id_random')
|
|
|
|
account_id_random: Optional[str]
|
|
account_id: Optional[int]
|
|
|
|
archive_id_random: Optional[str]
|
|
archive_id: Optional[int]
|
|
|
|
archive_content_type_id: Optional[int]
|
|
archive_content_type: Optional[str]
|
|
|
|
lu_media_type_id: Optional[int]
|
|
lu_media_type: Optional[str]
|
|
|
|
name: Optional[str]
|
|
description: Optional[str]
|
|
|
|
content_html: Optional[str]
|
|
content_json: Optional[Union[Json, None]]
|
|
|
|
url: Optional[str]
|
|
url_text: Optional[str]
|
|
|
|
hosted_file_id_random: Optional[str]
|
|
hosted_file_id: Optional[int]
|
|
|
|
file_path: Optional[str]
|
|
|
|
filename: Optional[str]
|
|
file_extension: Optional[str]
|
|
|
|
# xxxx_red: str = Field(default='xxx')
|
|
# xxxx_blue: str = Field(default_factory=testing)
|
|
hosted_file_path: str = None # '/testing/test-test'
|
|
api_hosted_file_path_download: str = None # '/testing/test-test'
|
|
api_hosted_file_path_stream: str = None # '/testing/test-test'
|
|
|
|
original_datetime: Optional[datetime.datetime]
|
|
original_timezone: Optional[str]
|
|
original_location: Optional[str]
|
|
original_address_id: Optional[int]
|
|
original_url: Optional[str]
|
|
original_url_text: Optional[str]
|
|
|
|
meta_data: Optional[str]
|
|
access_key: Optional[str]
|
|
|
|
enable_for_public: Optional[bool]
|
|
|
|
enable: Optional[bool]
|
|
enable_from: Optional[datetime.datetime]
|
|
enable_to: Optional[datetime.datetime]
|
|
|
|
hide: Optional[bool]
|
|
priority: Optional[bool]
|
|
sort: Optional[int]
|
|
group: Optional[str]
|
|
|
|
notes: Optional[str]
|
|
created_on: Optional[datetime.datetime]
|
|
updated_on: Optional[datetime.datetime]
|
|
|
|
# Including convenience data
|
|
# This is only for convenience. Probably going to keep unless it causes a problem.
|
|
hosted_file_hash_sha256: Optional[str]
|
|
hosted_file_subdirectory_path: Optional[str] = Field(None, exclude=True)
|
|
hosted_file_content_type: Optional[str]
|
|
hosted_file_size: Optional[str]
|
|
|
|
# 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] = [
|
|
'id', 'archive_content_id', 'id_random',
|
|
'account_id', 'account_id_random', 'archive_id_random', 'hosted_file_id_random',
|
|
'hosted_file_path', 'api_hosted_file_path_download', 'api_hosted_file_path_stream',
|
|
'hosted_file_hash_sha256', 'hosted_file_content_type', 'hosted_file_size'
|
|
]
|
|
|
|
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
|
|
|
@root_validator(pre=True)
|
|
def map_v3_ids(cls, values):
|
|
"""
|
|
Vision Transformer: Map DB random-string keys to clean Vision ID fields.
|
|
Collision prevention strips any integer that snuck into the string ID fields.
|
|
"""
|
|
rid = values.get('id_random') or values.get('archive_content_id_random')
|
|
if rid and isinstance(rid, str):
|
|
values['id'] = rid
|
|
values['archive_content_id'] = rid
|
|
# Collision prevention: reject integer values in Vision string fields
|
|
for k in ['id', 'archive_content_id']:
|
|
if k in values and not isinstance(values[k], str):
|
|
del values[k]
|
|
return values
|
|
|
|
@validator('archive_id', always=True)
|
|
def archive_id_lookup(cls, v, values, **kwargs):
|
|
if isinstance(v, int) and v > 0: return v
|
|
elif id_random := values.get('archive_id_random'):
|
|
return redis_lookup_id_random(record_id_random=id_random, table_name='archive')
|
|
return None
|
|
|
|
@validator('hosted_file_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('hosted_file_id_random'):
|
|
return redis_lookup_id_random(record_id_random=id_random, table_name='hosted_file')
|
|
return None
|
|
|
|
@validator('hosted_file_path', always=True)
|
|
def hosted_file_path_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(v)
|
|
log.debug(values)
|
|
|
|
if hosted_file_id_random := values.get('hosted_file_id_random'):
|
|
log.debug('Found hosted_file_id_random...')
|
|
path_str = f'/hosted_file/download/{hosted_file_id_random}'
|
|
|
|
if filename := values.get('filename'):
|
|
path_str = f'{path_str}?filename={filename}'
|
|
|
|
log.info(f'Path: {path_str}')
|
|
return path_str
|
|
log.debug('NOT Found hosted_file_id_random...')
|
|
return v
|
|
|
|
@validator('api_hosted_file_path_download', always=True)
|
|
def api_hosted_file_path_download_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(v)
|
|
log.debug(values)
|
|
|
|
if hosted_file_id_random := values.get('hosted_file_id_random'):
|
|
log.debug('Found hosted_file_id_random...')
|
|
path_str = f'/hosted_file/{hosted_file_id_random}/download'
|
|
|
|
if filename := values.get('filename'):
|
|
path_str = f'{path_str}?filename={filename}'
|
|
|
|
log.info(f'Path: {path_str}')
|
|
return path_str
|
|
log.debug('NOT Found hosted_file_id_random...')
|
|
return v
|
|
|
|
@validator('api_hosted_file_path_stream', always=True)
|
|
def api_hosted_file_path_stream_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(v)
|
|
log.debug(values)
|
|
|
|
if hosted_file_id_random := values.get('hosted_file_id_random'):
|
|
log.debug('Found hosted_file_id_random...')
|
|
path_str = f'/hosted_file/{hosted_file_id_random}/stream'
|
|
|
|
if filename := values.get('filename'):
|
|
path_str = f'{path_str}?filename={filename}'
|
|
|
|
log.info(f'Path: {path_str}')
|
|
return path_str
|
|
log.debug('NOT Found hosted_file_id_random...')
|
|
return v
|
|
|
|
class Config:
|
|
underscore_attrs_are_private = True
|
|
allow_population_by_field_name = True
|
|
fields = base_fields
|
|
# ### END ### API Archive Content Models ### Archive_Content_Base() ###
|