This commit refactors numerous Pydantic models to align with the V3 ID Vision standard, ensuring that primary and foreign key fields are represented as clean string IDs in the API. It also introduces and populates the ClassVar in each model to prevent view-only fields and linked objects from being inadvertently written to the database during PATCH/POST operations. Specifically, this includes: - Adding to exclude view-derived or joined fields such as , , nested objects (e.g., , ), and convenience fields (e.g., ). - Adjusting root validators to correctly map string IDs and strip internal integer IDs for API responses. - Resolving a KeyError by adding to . These changes are crucial for maintaining data integrity and consistency with the V3 API architecture.
132 lines
4.9 KiB
Python
132 lines
4.9 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 get_id_random, 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 Address Models ### Address_Base() ###
|
|
class Address_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['address_id_random'])
|
|
address_id: Optional[str] = Field(None, **base_fields['address_id_random'])
|
|
account_id: Optional[str] = Field(None, **base_fields['account_id_random'])
|
|
contact_id: Optional[str] = Field(None, **base_fields['contact_id_random'])
|
|
|
|
# Standardized Polymorphic Target
|
|
for_type: Optional[str]
|
|
for_id: Optional[str] = Field(**base_fields['obj_id_random'])
|
|
|
|
# --- Standardized Legacy / Internal IDs (Excluded) ---
|
|
id_random: Optional[str] = Field(None, alias='address_id_random', exclude=True)
|
|
account_id_random: Optional[str] = Field(None, exclude=True)
|
|
contact_id_random: Optional[str] = Field(None, exclude=True)
|
|
for_id_random: Optional[str] = Field(None, exclude=True)
|
|
|
|
#organization: Optional[Organization_Base] = Organization_Base()
|
|
|
|
name: Optional[str]
|
|
attention_to: Optional[str]
|
|
organization_name: Optional[str]
|
|
|
|
line_1: Optional[str]
|
|
line_2: Optional[str]
|
|
line_3: Optional[str]
|
|
city: Optional[str]
|
|
country_subdivision_code: Optional[str]
|
|
country_subdivision_name: Optional[str] # From country subdivision lookup table
|
|
state_province: Optional[str] # Avoid using
|
|
postal_code: Optional[str]
|
|
country_alpha_2_code: Optional[str]
|
|
country_name: Optional[str] # From country lookup table
|
|
country: Optional[str] # Avoid using
|
|
|
|
lu_time_zone_id: Optional[str] = Field(None, exclude=True)
|
|
timezone: Optional[str]
|
|
|
|
latitude: Optional[str]
|
|
longitude: Optional[str]
|
|
|
|
map_url: Optional[str]
|
|
|
|
congressional_district: 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.
|
|
"""
|
|
# 1. Map Primary Object ID
|
|
if rid := values.get('id_random') or values.get('address_id_random'):
|
|
values['id'] = rid
|
|
values['address_id'] = rid
|
|
|
|
# 2. Map & Resolve Relational IDs
|
|
id_map = [
|
|
('account_id', 'account'),
|
|
('contact_id', 'contact'),
|
|
]
|
|
|
|
for field, table in id_map:
|
|
r_val = values.get(f'{field}_random')
|
|
if r_val and isinstance(r_val, str):
|
|
values[field] = r_val
|
|
elif values.get(field) and isinstance(values[field], (int, str)):
|
|
is_random = isinstance(values[field], str) and len(values[field]) >= 11
|
|
if not is_random:
|
|
resolved_rid = get_id_random(values[field], table)
|
|
if resolved_rid:
|
|
values[field] = resolved_rid
|
|
values[f'{field}_random'] = resolved_rid
|
|
|
|
# 3. Handle Polymorphic for_id
|
|
if f_rid := values.get('for_id_random'):
|
|
values['for_id'] = f_rid
|
|
elif values.get('for_id') and values.get('for_type'):
|
|
# Resolve based on the for_type
|
|
is_random = isinstance(values['for_id'], str) and len(values['for_id']) >= 11
|
|
if not is_random:
|
|
resolved_for_rid = get_id_random(values['for_id'], values['for_type'])
|
|
if resolved_for_rid:
|
|
values['for_id'] = resolved_for_rid
|
|
values['for_id_random'] = resolved_for_rid
|
|
|
|
# 4. Final Vision Enforcement
|
|
for k in ['id', 'address_id', 'account_id', 'contact_id', 'for_id']:
|
|
val = values.get(k)
|
|
if val is not None:
|
|
if not isinstance(val, str) or len(val) < 11:
|
|
values[k] = None
|
|
|
|
return values
|
|
|
|
# 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] = [
|
|
'country_subdivision_name', 'country_name'
|
|
]
|
|
|
|
class Config:
|
|
underscore_attrs_are_private = True
|
|
allow_population_by_field_name = False
|
|
fields = base_fields
|
|
# ### END ### API Address Models ### Address_Base() ### |