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.
147 lines
6.1 KiB
Python
147 lines
6.1 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 .common_field_schema import base_fields, default_num_bytes
|
|
# from app.models.event_session_models import Event_Session_Base
|
|
|
|
|
|
# ### BEGIN ### API Event Location Models ### Event_Location_Base() ###
|
|
class Event_Location_Base(BaseModel):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# --- Standardized Vision IDs (Strings) ---
|
|
id: Optional[str] = Field(None, **base_fields['event_location_id_random'])
|
|
event_location_id: Optional[str] = Field(None, **base_fields['event_location_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_track_id: Optional[str] = Field(None, **base_fields['event_track_id_random'])
|
|
|
|
# --- Standardized Legacy / Internal IDs (Excluded) ---
|
|
id_random: Optional[str] = Field(None, alias='event_location_id_random', exclude=True)
|
|
account_id_random: Optional[str] = Field(None, exclude=True)
|
|
event_id_random: Optional[str] = Field(None, exclude=True)
|
|
event_track_id_random: Optional[str] = Field(None, exclude=True)
|
|
|
|
code: Optional[str] = Field(
|
|
# alias = 'event_location_code'
|
|
)
|
|
|
|
external_id: Optional[str] = Field(
|
|
# alias = 'event_location_external_id'
|
|
)
|
|
|
|
lu_location_type_id: Optional[int]
|
|
location_type_code: Optional[str]
|
|
location_type: Optional[str]
|
|
|
|
name: Optional[str]
|
|
description: Optional[str]
|
|
|
|
# New internal use fields to help with logistics and planning 2022-09-15
|
|
internal_use: Optional[bool] # Will hide from moderators, presenters, non support people, etc
|
|
record_audio: Optional[bool] # Record the sessions in this location
|
|
record_video: Optional[bool] # Record the sessions in this location
|
|
internal_notes: Optional[str] # general notes
|
|
internal_notes_access: Optional[str] # accessibility
|
|
internal_notes_av: Optional[str] # audio video
|
|
internal_notes_fb: Optional[str] # food and beverage
|
|
internal_notes_it: Optional[str] # IT and networking
|
|
internal_notes_staff: Optional[str] # staffing and labor
|
|
|
|
passcode: Optional[str]
|
|
|
|
cfg_json: Optional[Union[Json, None]] # Store per location config options
|
|
data_json: Optional[Union[Json, None]] # For key value data. Careful with overwriting existing fields!
|
|
|
|
file_count: Optional[int]
|
|
internal_use_count: Optional[int] # Should be renamed to "internal_use_file_count"???
|
|
event_file_id_li_json: Optional[Union[Json, None]] # List of file IDs (actually id_random)
|
|
file_count_all: Optional[int] # Of all files under a location
|
|
|
|
alert: Optional[bool]
|
|
alert_msg: Optional[str]
|
|
|
|
enable: Optional[bool]
|
|
enable_from: Optional[datetime.datetime] = None
|
|
enable_to: Optional[datetime.datetime] = None
|
|
|
|
hide: Optional[bool]
|
|
public: Optional[bool]
|
|
public_hide: Optional[bool]
|
|
hide_event_launcher: 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
|
|
|
|
# Including convenience data
|
|
# This is only for convenience. Probably going to keep unless it causes a problem.
|
|
event_name: Optional[str]
|
|
event_start_datetime: Optional[datetime.datetime]
|
|
event_end_datetime: Optional[datetime.datetime]
|
|
|
|
# Including other related objects
|
|
#event: Optional[Event_Base]
|
|
event_abstract_list: Optional[list] # Optional[Event_Abstract_Base]
|
|
event_device_list: Optional[list] # Optional[Event_Device_Base]
|
|
event_file_list: Optional[list] # Optional[Event_File_Base]
|
|
event_file_internal_use_list: Optional[list] # Optional[Event_File_Base]
|
|
event_presentation_list: Optional[list] # Optional[Event_Presentation_Base]
|
|
event_presenter_list: Optional[list] # Optional[Event_Presenter_Base]
|
|
event_session_list: Optional[list] # Optional[Event_Session_Base]
|
|
event_track_list: Optional[list] # Optional[Event_Track_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_location_id_random'):
|
|
values['id'] = rid
|
|
values['event_location_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 et_rid := values.get('event_track_id_random'):
|
|
values['event_track_id'] = et_rid
|
|
|
|
# 2. Prevent "Collision Population"
|
|
for k in ['id', 'event_location_id', 'account_id', 'event_id', 'event_track_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',
|
|
'location_type', 'file_count', 'internal_use_count', 'event_file_id_li_json', 'file_count_all',
|
|
'event_name', 'event_start_datetime', 'event_end_datetime',
|
|
'event_abstract_list', 'event_device_list', 'event_file_list',
|
|
'event_file_internal_use_list', 'event_presentation_list',
|
|
'event_presenter_list', 'event_session_list', 'event_track_list'
|
|
]
|
|
|
|
class Config:
|
|
underscore_attrs_are_private = True
|
|
allow_population_by_field_name = False
|
|
fields = base_fields
|
|
# ### END ### API Event Location Models ### Event_Location_Base() ###
|