- Added validators to Person_Base, Journal_Base, Journal_Entry_Base, Address_Base, and Contact_Base to handle null values and unsafe lookups. - Implemented 'fields_to_exclude_from_db' ClassVar in Journal models to prevent view-only fields from causing DB errors. - Updated Contact object map to align with DB schema. - Added comprehensive test suite in 'tests/' directory (model validation, filtering logic). - Updated GEMINI.md with progress.
134 lines
4.4 KiB
Python
134 lines
4.4 KiB
Python
import datetime, pytz
|
|
|
|
from typing import Dict, List, Optional, Set, Union
|
|
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, 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())
|
|
|
|
id_random: Optional[str] = Field(
|
|
**base_fields['address_id_random'],
|
|
alias = 'address_id_random',
|
|
# default_factory = lambda:secrets.token_urlsafe(default_num_bytes),
|
|
)
|
|
id: Optional[int] = Field(
|
|
alias = 'address_id'
|
|
)
|
|
account_id_random: Optional[str]
|
|
account_id: Optional[int]
|
|
|
|
for_type: Optional[str]
|
|
for_id_random: Optional[str]
|
|
for_id: Optional[int]
|
|
|
|
contact_id_random: Optional[str]
|
|
contact_id: Optional[int]
|
|
|
|
#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)
|
|
|
|
#@validator('address_id_random', always=True)
|
|
def address_id_random_copy(cls, v, values, **kwargs):
|
|
log.setLevel(logging.WARNING)
|
|
log.debug(locals())
|
|
|
|
if values['id_random']:
|
|
return values['id_random']
|
|
return None
|
|
|
|
@validator('id', always=True)
|
|
def address_id_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.WARNING)
|
|
log.debug(locals())
|
|
|
|
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='address')
|
|
return None
|
|
|
|
@validator('account_id', always=True)
|
|
def account_id_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.WARNING)
|
|
log.debug(locals())
|
|
|
|
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('account_id_random', always=True)
|
|
def account_id_random_lookup(cls, v, values, **kwargs):
|
|
if isinstance(v, str) and len(v) >= 11: return v
|
|
elif account_id := values.get('account_id'):
|
|
return get_id_random(record_id=account_id, table_name='account')
|
|
return None
|
|
|
|
@validator('contact_id', always=True)
|
|
def contact_id_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.WARNING)
|
|
log.debug(locals())
|
|
|
|
if isinstance(v, int) and v > 0: return v
|
|
elif id_random := values.get('contact_id_random'):
|
|
return redis_lookup_id_random(record_id_random=id_random, table_name='contact')
|
|
return None
|
|
|
|
@validator('for_id', always=True)
|
|
def for_id_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.WARNING)
|
|
log.debug(locals())
|
|
|
|
if values.get('for_id_random') and values.get('for_type'):
|
|
return redis_lookup_id_random(record_id_random=values['for_id_random'], table_name=values['for_type'])
|
|
return None
|
|
|
|
class Config:
|
|
underscore_attrs_are_private = True
|
|
allow_population_by_field_name = True
|
|
fields = base_fields
|
|
# ### END ### API Address Models ### Address_Base() ### |