Vision ID: Standardize Site Domain and Journal objects with string-only IDs and searchable mapping
This commit is contained in:
@@ -126,9 +126,10 @@ def sanitize_payload(data: dict, model: Any, ignore_extra: bool = False) -> None
|
||||
"""
|
||||
Sanitizes an input payload before database insertion or update.
|
||||
|
||||
1. Resolves virtual lookup fields (`*_id_random`) into their integer database IDs.
|
||||
2. Removes virtual lookup fields (ending in `_id_random`) that are used for API
|
||||
convenience but do not exist in the database.
|
||||
1. Resolves ID strings to integers:
|
||||
- Handles legacy `*_id_random` fields.
|
||||
- Handles Vision `*_id` fields where the value is a string (e.g., account_id: "random_str").
|
||||
2. Removes virtual lookup fields (ending in `_id_random`) after resolution.
|
||||
3. Removes fields explicitly marked for exclusion in the model's
|
||||
`fields_to_exclude_from_db` ClassVar (e.g., view-only fields).
|
||||
4. If `ignore_extra` is True, removes all fields NOT present in the model definition.
|
||||
@@ -140,22 +141,35 @@ def sanitize_payload(data: dict, model: Any, ignore_extra: bool = False) -> None
|
||||
|
||||
from app.db_sql import redis_lookup_id_random
|
||||
|
||||
# Resolve virtual _id_random fields to integer IDs (e.g., account_id_random -> account_id)
|
||||
# This must happen BEFORE we delete them.
|
||||
# Resolve ID strings to integers
|
||||
for k, v in list(data.items()):
|
||||
if k.endswith('_id_random') and k != 'id_random' and v:
|
||||
if not v or not isinstance(v, str):
|
||||
continue
|
||||
|
||||
target_id_field = None
|
||||
obj_type_lookup = None
|
||||
|
||||
# Scenario A: Legacy suffix (e.g., account_id_random: "abc")
|
||||
if k.endswith('_id_random') and k != 'id_random':
|
||||
target_id_field = k.replace('_id_random', '_id')
|
||||
# Only resolve if the integer version is missing or null
|
||||
if not data.get(target_id_field):
|
||||
obj_type_lookup = k.replace('_id_random', '')
|
||||
resolved_id = redis_lookup_id_random(record_id_random=v, table_name=obj_type_lookup)
|
||||
if resolved_id:
|
||||
data[target_id_field] = resolved_id
|
||||
|
||||
# Filter out virtual _id_random fields (e.g., account_id_random)
|
||||
keys_to_remove = [k for k in data.keys() if k.endswith('_id_random') and k != 'id_random']
|
||||
for k in keys_to_remove:
|
||||
del data[k]
|
||||
obj_type_lookup = k.replace('_id_random', '')
|
||||
|
||||
# Scenario B: Vision naming (e.g., account_id: "abc")
|
||||
# We only resolve if it's a string of the correct length (random ID format)
|
||||
elif k.endswith('_id') and 11 <= len(v) <= 22:
|
||||
target_id_field = k
|
||||
obj_type_lookup = k.replace('_id', '')
|
||||
|
||||
if target_id_field and obj_type_lookup:
|
||||
# Special table mapping if needed
|
||||
if obj_type_lookup == 'address_location': obj_type_lookup = 'address'
|
||||
|
||||
resolved_id = redis_lookup_id_random(record_id_random=v, table_name=obj_type_lookup)
|
||||
if resolved_id:
|
||||
data[target_id_field] = resolved_id
|
||||
# If we were handling Scenario A, remove the original random key
|
||||
if k.endswith('_id_random'):
|
||||
del data[k]
|
||||
|
||||
# Filter out model-specific excluded fields (e.g., view-only fields)
|
||||
if hasattr(model, 'fields_to_exclude_from_db'):
|
||||
|
||||
Reference in New Issue
Block a user