- 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.
115 lines
4.2 KiB
Python
115 lines
4.2 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
|
|
|
|
|
|
# ### BEGIN ### API Data Store Models ### Data_Store_Base() ###
|
|
class Data_Store_Base(BaseModel):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# --- 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'])
|
|
|
|
# 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: 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_str: Optional[Union[Json, None]] = Field(
|
|
alias = 'json',
|
|
)
|
|
|
|
# The text fields are case insensitive
|
|
text: Optional[str]
|
|
|
|
meta_json: Optional[str]
|
|
meta_text: Optional[str]
|
|
|
|
access: Optional[str]
|
|
access_read: Optional[str]
|
|
access_write: Optional[str]
|
|
access_delete: Optional[str]
|
|
|
|
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
|
|
|
|
_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.
|
|
"""
|
|
# 0. Scrub stringified NULLs from database
|
|
for k, v in list(values.items()):
|
|
if isinstance(v, str) and v.upper() == 'NULL':
|
|
values[k] = 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 = False
|
|
fields = base_fields
|
|
# ### END ### API Data Store Models ### Data_Store_Base() ###
|