94 lines
3.1 KiB
Python
94 lines
3.1 KiB
Python
import datetime, pytz
|
|
|
|
from typing import Dict, List, Optional, Set, Union
|
|
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'])
|
|
|
|
for_type: Optional[str]
|
|
for_id_random: Optional[str]
|
|
for_id: Optional[int] = 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]
|
|
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 Random Strings to Clean Names
|
|
if rid := values.get('id_random') or values.get('address_id_random'):
|
|
values['id'] = rid
|
|
values['address_id'] = rid
|
|
|
|
if a_rid := values.get('account_id_random'):
|
|
values['account_id'] = a_rid
|
|
if c_rid := values.get('contact_id_random'):
|
|
values['contact_id'] = c_rid
|
|
|
|
# 2. Prevent "Collision Population"
|
|
for k in ['id', 'account_id', 'contact_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 Address Models ### Address_Base() ### |