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.
158 lines
5.7 KiB
Python
158 lines
5.7 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 redis_lookup_id_random
|
|
from app.lib_general import log, logging
|
|
|
|
from app.models.common_field_schema import base_fields, default_num_bytes
|
|
from app.models.event_cfg_models import Event_Cfg_Base
|
|
from app.models.event_location_models import Event_Location_Base
|
|
|
|
|
|
# ### BEGIN ### API Event Device Models ### Event_Device_Base() ###
|
|
class Event_Device_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['event_device_id_random'])
|
|
event_device_id: Optional[str] = Field(None, **base_fields['event_device_id_random'])
|
|
|
|
account_id: Optional[str] = Field(None, **base_fields['account_id_random'])
|
|
event_id: Optional[str] = Field(None, **base_fields['event_id_random'])
|
|
event_location_id: Optional[str] = Field(None, **base_fields['event_location_id_random'])
|
|
|
|
code: Optional[str]
|
|
|
|
name: Optional[str]
|
|
description: Optional[str]
|
|
|
|
app_mode: Optional[str] # null, default, onsite, app
|
|
use_local_api: Optional[bool]
|
|
use_local_app: Optional[bool]
|
|
|
|
api_secret_key: Optional[str]
|
|
|
|
api_base_url: Optional[str]
|
|
app_base_url: Optional[str]
|
|
file_server_base_url: Optional[str]
|
|
|
|
api_base_url_bak: Optional[str] # Backup URL
|
|
app_base_url_bak: Optional[str] # Backup URL
|
|
file_server_base_url_bak: Optional[str] # Backup URL
|
|
|
|
trigger_open_filename: Optional[str] # The file hash filename
|
|
trigger_open_extension: Optional[str] # The file hash extension part
|
|
trigger_open_open_in_os: Optional[str] # Use open_in_os for win vs mac
|
|
|
|
trigger_open_file_id: Optional[str] # The file ID random; use along with filename, extension, and open_in_os
|
|
trigger_open_hash_file: Optional[str] # The file hash; use along with filename, extension, and open_in_os
|
|
trigger_open_file_path: Optional[str] # Use along with filename
|
|
|
|
trigger_open_session_id: Optional[str]
|
|
trigger_open_session_id: Optional[str]
|
|
|
|
trigger_recording_start: Optional[bool]
|
|
trigger_recording_stop: Optional[bool]
|
|
|
|
trigger_reset: Optional[bool]
|
|
|
|
trigger_show_admin: Optional[str]
|
|
trigger_show_hidden: Optional[str]
|
|
|
|
local_file_cache_path: Optional[str] # Path for hash file cache only
|
|
host_file_temp_path: Optional[str] # Path for copied and renamed temporary files from hash file cache directory
|
|
recording_path: Optional[str] # Path to save recordings
|
|
|
|
record_audio: Optional[bool] # Record
|
|
record_video: Optional[bool] # Record
|
|
|
|
check_event_loop_period: Optional[int]
|
|
check_event_device_loop_period: Optional[int]
|
|
check_event_location_loop_period: Optional[int]
|
|
check_event_session_loop_period: Optional[int]
|
|
|
|
passcode: Optional[str]
|
|
|
|
alert: Optional[bool]
|
|
alert_msg: Optional[str]
|
|
alert_on: Optional[datetime.datetime]
|
|
status: Optional[str]
|
|
status_msg: Optional[str]
|
|
status_msg_on: Optional[datetime.datetime]
|
|
record_status: Optional[str]
|
|
record_status_msg: Optional[str]
|
|
record_status_on: Optional[datetime.datetime]
|
|
|
|
heartbeat: Optional[datetime.datetime]
|
|
|
|
info_hostname: Optional[str]
|
|
info_ip: Optional[str]
|
|
info_ip_list: Optional[str] # string list of IPs separated by ;
|
|
info_os: Optional[str]
|
|
|
|
cfg_json: Optional[Union[Json, None]] # Store per device config options like theme, language, etc
|
|
data_json: Optional[Union[Json, None]] # For key value data. Careful with overwriting existing fields!
|
|
|
|
enable: Optional[bool]
|
|
|
|
hide: Optional[bool]
|
|
priority: Optional[bool]
|
|
sort: Optional[int]
|
|
group: Optional[str]
|
|
|
|
event_notes: Optional[str]
|
|
notes: Optional[str]
|
|
|
|
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.
|
|
|
|
# Including JSON data
|
|
other_json: Optional[Union[str,Json]]
|
|
meta_json: Optional[Union[str,Json]]
|
|
|
|
# Including other related objects
|
|
event_cfg: Optional[Event_Cfg_Base]
|
|
event_location: Optional[Event_Location_Base]
|
|
|
|
_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('event_device_id_random'):
|
|
values['id'] = rid
|
|
values['event_device_id'] = rid
|
|
|
|
if a_rid := values.get('account_id_random'):
|
|
values['account_id'] = a_rid
|
|
if e_rid := values.get('event_id_random'):
|
|
values['event_id'] = e_rid
|
|
if el_rid := values.get('event_location_id_random'):
|
|
values['event_location_id'] = el_rid
|
|
|
|
# 2. Prevent "Collision Population"
|
|
for k in ['id', 'event_device_id', 'account_id', 'event_id', 'event_location_id']:
|
|
if k in values and not isinstance(values[k], str) and values[k] is not None:
|
|
del values[k]
|
|
|
|
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] = ['account_id', 'event_cfg', 'event_location']
|
|
|
|
class Config:
|
|
underscore_attrs_are_private = True
|
|
allow_population_by_field_name = False
|
|
fields = base_fields
|
|
# ### END ### API Event Device Models ### Event_Device_Base() ###
|