Vision ID: Standardize Site Domain and Journal objects with string-only IDs and searchable mapping
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import datetime, pytz
|
||||
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator
|
||||
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
|
||||
@@ -14,16 +14,11 @@ class Site_Domain_Base(BaseModel):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
id_random: Optional[str] = Field(
|
||||
**base_fields['site_domain_id_random'],
|
||||
alias = 'site_domain_id_random',
|
||||
)
|
||||
id: Optional[int] = Field(
|
||||
alias = 'site_domain_id'
|
||||
)
|
||||
|
||||
site_id_random: Optional[str]
|
||||
site_id: Optional[int]
|
||||
# --- Standardized Vision IDs (Strings) ---
|
||||
id: Optional[str] = Field(None, **base_fields['site_domain_id_random'])
|
||||
site_domain_id: Optional[str] = Field(None, **base_fields['site_domain_id_random'])
|
||||
site_id: Optional[str] = Field(None, **base_fields['site_id_random'])
|
||||
account_id: Optional[str] = Field(None, **base_fields['account_id_random'])
|
||||
|
||||
fqdn: Optional[str]
|
||||
|
||||
@@ -38,59 +33,45 @@ class Site_Domain_Base(BaseModel):
|
||||
|
||||
cfg_json: Optional[Union[Json, None]] # In use 2024-03-04
|
||||
|
||||
hide: Optional[bool]
|
||||
priority: Optional[bool]
|
||||
sort: Optional[int]
|
||||
group: Optional[str]
|
||||
hide: Optional[bool] = None # Field missing in physical table but common in views
|
||||
# priority: Optional[bool] # MISSING in physical table
|
||||
# sort: Optional[int] # MISSING in physical table
|
||||
# group: Optional[str] # MISSING in physical table
|
||||
|
||||
notes: Optional[str]
|
||||
notes: Optional[str] = None # MISSING in physical table
|
||||
created_on: Optional[datetime.datetime] = None
|
||||
updated_on: Optional[datetime.datetime] = None
|
||||
|
||||
# Including convenience data
|
||||
# This is only for convenience. Probably going to keep unless it causes a problem.
|
||||
account_id: Optional[int]
|
||||
account_id_random: Optional[str]
|
||||
|
||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||
|
||||
@validator('id', always=True)
|
||||
def site_domain_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='site_domain')
|
||||
return None
|
||||
|
||||
@validator('account_id', always=True)
|
||||
def account_id_lookup(cls, v, values, **kwargs):
|
||||
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('site_id', always=True)
|
||||
def site_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['site_id_random']:
|
||||
return redis_lookup_id_random(record_id_random=values['site_id_random'], table_name='site')
|
||||
return None
|
||||
@root_validator(pre=True)
|
||||
def map_v3_ids(cls, values):
|
||||
"""
|
||||
Vision Transformer:
|
||||
Map DB-centric keys to clean API keys and strip internal integers.
|
||||
"""
|
||||
# 1. Map Random Strings to Clean Names
|
||||
# We prioritize the random strings to ensure the Vision is string-based.
|
||||
if rid := values.get('id_random') or values.get('site_domain_id_random'):
|
||||
values['id'] = rid
|
||||
values['site_domain_id'] = rid
|
||||
|
||||
if s_rid := values.get('site_id_random'):
|
||||
values['site_id'] = s_rid
|
||||
|
||||
if a_rid := values.get('account_id_random'):
|
||||
values['account_id'] = a_rid
|
||||
|
||||
# 2. Prevent "Collision Population"
|
||||
for k in ['id', 'site_id', 'account_id']:
|
||||
if k in values and not isinstance(values[k], str):
|
||||
del values[k]
|
||||
|
||||
return values
|
||||
|
||||
class Config:
|
||||
underscore_attrs_are_private = True
|
||||
fields = base_fields
|
||||
allow_population_by_field_name = True
|
||||
allow_population_by_field_name = False
|
||||
# ### END ### API Site Domain Models ### Site_Domain_Base() ###
|
||||
|
||||
|
||||
@@ -99,19 +80,11 @@ class Site_Domain_FQDN_ID_Base(BaseModel):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
id_random: Optional[str] = Field(
|
||||
**base_fields['site_domain_id_random'],
|
||||
alias = 'site_domain_id_random',
|
||||
)
|
||||
id: Optional[int] = Field(
|
||||
alias = 'site_domain_id'
|
||||
)
|
||||
|
||||
site_id_random: Optional[str]
|
||||
site_id: Optional[int]
|
||||
|
||||
site_domain_id_random: Optional[str]
|
||||
site_domain_id: Optional[int]
|
||||
# --- Standardized Vision IDs (Strings) ---
|
||||
id: Optional[str] = Field(None, **base_fields['site_domain_id_random'])
|
||||
site_domain_id: Optional[str] = Field(None, **base_fields['site_domain_id_random'])
|
||||
site_id: Optional[str] = Field(None, **base_fields['site_id_random'])
|
||||
account_id: Optional[str] = Field(None, **base_fields['account_id_random'])
|
||||
|
||||
fqdn: Optional[str]
|
||||
|
||||
@@ -124,19 +97,13 @@ class Site_Domain_FQDN_ID_Base(BaseModel):
|
||||
valid_for: Optional[int] # number of hours
|
||||
enable: Optional[bool]
|
||||
|
||||
hide: Optional[bool]
|
||||
priority: Optional[bool]
|
||||
sort: Optional[int]
|
||||
group: Optional[str]
|
||||
|
||||
notes: Optional[str]
|
||||
hide: Optional[bool] = None
|
||||
|
||||
notes: Optional[str] = None
|
||||
created_on: Optional[datetime.datetime] = None
|
||||
updated_on: Optional[datetime.datetime] = None
|
||||
|
||||
# Including convenience data
|
||||
# This is only for convenience. Probably going to keep unless it causes a problem.
|
||||
account_id: Optional[int]
|
||||
account_id_random: Optional[str]
|
||||
account_code: Optional[str] # Useful for export file naming
|
||||
account_name: Optional[str] # Generally useful for display
|
||||
account_enable: Optional[bool]
|
||||
@@ -157,54 +124,22 @@ class Site_Domain_FQDN_ID_Base(BaseModel):
|
||||
|
||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||
|
||||
@validator('id', always=True)
|
||||
def id_lookup(cls, v, values, **kwargs):
|
||||
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='site_domain')
|
||||
return None
|
||||
|
||||
@validator('site_domain_id_random', always=True)
|
||||
def site_domain_id_random_lookup(cls, v, values, **kwargs):
|
||||
if isinstance(v, str) and len(v) >= 11: return v
|
||||
elif id_random := values.get('id_random'):
|
||||
return id_random
|
||||
return None
|
||||
|
||||
@validator('account_id', always=True)
|
||||
def account_id_lookup(cls, v, values, **kwargs):
|
||||
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('site_id', always=True)
|
||||
def site_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['site_id_random']:
|
||||
return redis_lookup_id_random(record_id_random=values['site_id_random'], table_name='site')
|
||||
return None
|
||||
|
||||
@validator('site_domain_id', always=True)
|
||||
def site_domain_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['site_domain_id_random']:
|
||||
return redis_lookup_id_random(record_id_random=values['site_domain_id_random'], table_name='site')
|
||||
return None
|
||||
@root_validator(pre=True)
|
||||
def map_v3_ids(cls, values):
|
||||
if rid := values.get('id_random') or values.get('site_domain_id_random'):
|
||||
values['id'] = rid
|
||||
values['site_domain_id'] = rid
|
||||
if s_rid := values.get('site_id_random'):
|
||||
values['site_id'] = s_rid
|
||||
if a_rid := values.get('account_id_random'):
|
||||
values['account_id'] = a_rid
|
||||
|
||||
for k in ['id', 'site_id', 'account_id']:
|
||||
if k in values and not isinstance(values[k], str):
|
||||
del values[k]
|
||||
return values
|
||||
|
||||
class Config:
|
||||
underscore_attrs_are_private = True
|
||||
fields = base_fields
|
||||
allow_population_by_field_name = True
|
||||
allow_population_by_field_name = False
|
||||
# ### END ### API Site Domain Models ### Site_Domain_FQDN_ID_Base() ###
|
||||
|
||||
Reference in New Issue
Block a user