feat(data_store): implement V3 cascading lookup and ID Vision standardization
- Added GET /v3/data_store/code/{code} with hierarchical context-aware fallback.
- Implemented ID Vision standard in Data_Store_Base (string IDs, internal int exclusion).
- Enhanced Data_Store_Base robustness to handle stringified 'NULL' values from the database.
- Fixed legacy router bugs by removing undefined parameters (inc_event_cfg, inc_event_location).
- Corrected type hints and resolved UnboundLocalError in data_store methods.
- Updated Frontend Integration Guide with Section 8: Data Store V3.
- Added unified E2E test script: tests/e2e/test_e2e_v3_data_store_lookup.py.
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 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
|
||||
@@ -11,39 +11,37 @@ from app.models.common_field_schema import base_fields
|
||||
|
||||
# ### BEGIN ### API Data Store Models ### Data_Store_Base() ###
|
||||
class Data_Store_Base(BaseModel):
|
||||
# log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
id_random: Optional[str] = Field(
|
||||
**base_fields['data_store_id_random'],
|
||||
alias = 'data_store_id_random',
|
||||
)
|
||||
id: Optional[int] = Field(
|
||||
alias = 'data_store_id'
|
||||
)
|
||||
# --- Standardized Vision IDs (Strings) ---
|
||||
id: Optional[str] = Field(None, **base_fields['data_store_id_random'])
|
||||
data_store_id: Optional[str] = Field(None, **base_fields['data_store_id_random'])
|
||||
|
||||
account_id: Optional[str] = Field(None, **base_fields['account_id_random'])
|
||||
person_id: Optional[str] = Field(None, **base_fields['person_id_random'])
|
||||
user_id: Optional[str] = Field(None, **base_fields['user_id_random'])
|
||||
|
||||
account_id_random: Optional[str]
|
||||
account_id: Optional[int]
|
||||
# Internal Integer IDs (Excluded from API)
|
||||
# We use Optional[Union[int, str]] here to prevent validation crashes
|
||||
# if the DB returns stringified integers or "NULL" strings.
|
||||
id_int: Optional[Union[int, str]] = Field(None, alias='id', exclude=True)
|
||||
account_id_int: Optional[Union[int, str]] = Field(None, alias='account_id', exclude=True)
|
||||
person_id_int: Optional[Union[int, str]] = Field(None, alias='person_id', exclude=True)
|
||||
user_id_int: Optional[Union[int, str]] = Field(None, alias='user_id', exclude=True)
|
||||
|
||||
for_type: Optional[str]
|
||||
for_id_random: Optional[str]
|
||||
for_id: Optional[int]
|
||||
|
||||
person_id_random: Optional[str]
|
||||
person_id: Optional[int]
|
||||
|
||||
user_id_random: Optional[str]
|
||||
user_id: Optional[int]
|
||||
for_id: Optional[str] # Random ID string
|
||||
for_id_random: Optional[str] # Svelte often uses this name
|
||||
for_id_int: Optional[Union[int, str]] = Field(None, alias='for_id', exclude=True)
|
||||
|
||||
code: Optional[str]
|
||||
|
||||
name: Optional[str]
|
||||
description: Optional[str]
|
||||
|
||||
type: Optional[str] # html, json, md, text
|
||||
|
||||
# The JSON fields are case sensitive
|
||||
# json: Optional[str] # "json" is reserved; need to change field name? json_str?
|
||||
json_str: Optional[Union[Json, None]] = Field(
|
||||
alias = 'json',
|
||||
)
|
||||
@@ -71,58 +69,46 @@ class Data_Store_Base(BaseModel):
|
||||
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.
|
||||
|
||||
# Including JSON data
|
||||
# other_json: Optional[Json]
|
||||
# meta_json: Optional[Json]
|
||||
|
||||
# Including other related objects
|
||||
|
||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||
|
||||
@validator('id', always=True)
|
||||
def data_store_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='data_store')
|
||||
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.
|
||||
"""
|
||||
# 0. Scrub stringified NULLs from database
|
||||
for k, v in list(values.items()):
|
||||
if isinstance(v, str) and v.upper() == 'NULL':
|
||||
values[k] = 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
|
||||
|
||||
@validator('for_id', always=True)
|
||||
def for_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
if isinstance(v, int) and v > 0: return v
|
||||
elif values.get('for_id_random') and values.get('for_type'):
|
||||
for_id_random = values.get('for_id_random')
|
||||
for_type = values.get('for_type')
|
||||
return redis_lookup_id_random(record_id_random=for_id_random, table_name=for_type)
|
||||
return None
|
||||
|
||||
@validator('person_id', always=True)
|
||||
def person_id_lookup(cls, v, values, **kwargs):
|
||||
if isinstance(v, int) and v > 0: return v
|
||||
elif id_random := values.get('person_id_random'):
|
||||
return redis_lookup_id_random(record_id_random=id_random, table_name='person')
|
||||
return None
|
||||
|
||||
@validator('user_id', always=True)
|
||||
def user_id_lookup(cls, v, values, **kwargs):
|
||||
if isinstance(v, int) and v > 0: return v
|
||||
elif id_random := values.get('user_id_random'):
|
||||
return redis_lookup_id_random(record_id_random=id_random, table_name='user')
|
||||
return None
|
||||
# 1. Map Random Strings to Clean Names
|
||||
if rid := values.get('id_random') or values.get('data_store_id_random'):
|
||||
values['id'] = rid
|
||||
values['data_store_id'] = rid
|
||||
|
||||
if a_rid := values.get('account_id_random'):
|
||||
values['account_id'] = a_rid
|
||||
if p_rid := values.get('person_id_random'):
|
||||
values['person_id'] = p_rid
|
||||
if u_rid := values.get('user_id_random'):
|
||||
values['user_id'] = u_rid
|
||||
if f_rid := values.get('for_id_random'):
|
||||
values['for_id'] = f_rid
|
||||
values['for_id_random'] = f_rid
|
||||
|
||||
# 2. Prevent "Collision Population"
|
||||
# We only want strings in our primary ID fields.
|
||||
# If the key exists and isn't a string, it's a DB integer; remove it
|
||||
# so it doesn't fail length validation on the string fields.
|
||||
for k in ['id', 'data_store_id', 'account_id', 'person_id', 'user_id', 'for_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 Data Store Models ### Data_Store_Base() ###
|
||||
|
||||
Reference in New Issue
Block a user