Files
OSIT-AE-API-FastAPI/app/models/event_cfg_models.py
Scott Idem 0f4b4d2f51 feat: Implement V3 ID Vision and fields_to_exclude_from_db across core models
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.
2026-02-24 16:21:27 -05:00

142 lines
5.8 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_registration_cfg_models import Event_Registration_Cfg_Base
# ### BEGIN ### API Event Cfg Models ### Event_Cfg_Base() ###
class Event_Cfg_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_cfg_id_random'])
event_cfg_id: Optional[str] = Field(None, **base_fields['event_cfg_id_random'])
account_id: Optional[str] = Field(None, **base_fields['account_id_random'])
event_id: Optional[str] = Field(None, **base_fields['event_id_random'])
# --- Standardized Legacy / Internal IDs (Excluded) ---
id_random: Optional[str] = Field(None, alias='event_cfg_id_random', exclude=True)
account_id_random: Optional[str] = Field(None, exclude=True)
event_id_random: Optional[str] = Field(None, exclude=True)
@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_cfg_id_random'):
values['id'] = rid
values['event_cfg_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
# 2. Prevent "Collision Population"
for k in ['id', 'event_cfg_id', 'account_id', 'event_id']:
if k in values and not isinstance(values[k], str) and values[k] is not None:
del values[k]
return values
enable: Optional[bool]
enable_from: Optional[datetime.datetime]
enable_to: Optional[datetime.datetime]
conference: Optional[bool]
enable_comments: Optional[bool]
disable_navigation: Optional[bool] # limits navigation; rename to limit_navigation? 2022-08-23
enable_event_file_upload_for_event: Optional[bool]
enable_event_file_upload_for_location: Optional[bool]
enable_event_file_upload_for_presentation: Optional[bool]
enable_event_file_upload_for_presenter: Optional[bool]
enable_event_file_upload_for_session: Optional[bool]
enable_event_file_upload_for_track: Optional[bool]
# enable_event_file_upload_for_event_track: Optional[bool] # Change to this pattern in the future?
# enable_file_upload_for_track: Optional[bool] # Change to this pattern in the future?
enable_event_file_upload_review_question: Optional[bool]
enable_event_file_upload_email_question: Optional[bool]
enable_event_file_upload_comments_question: Optional[bool]
enable_event_file_approval_option: Optional[bool]
enable_event_file_public_use_option: Optional[bool]
enable_event_file_member_use_option: Optional[bool]
enable_event_file_attendee_use_option: Optional[bool]
enable_event_file_publish_option: Optional[bool]
enable_event_file_purpose_option: Optional[bool]
enable_event_file_os_selection_option: Optional[bool]
enable_event_file_os_change_option: Optional[bool]
custom_event_file_upload_description: Optional[str]
custom_event_file_agreement_1_text: Optional[str]
custom_event_file_agreement_1_description: Optional[str]
ask_speaker_ready_room: Optional[bool]
ask_presentation_publish_optout: Optional[bool]
default_event_file_to_public_use: Optional[bool]
ask_for_public_version: Optional[bool]
mod_abstracts_json: Optional[Union[Json, None]]
mod_badges_json: Optional[Union[Json, None]] # NOTE: Not used yet
mod_exhibits_json: Optional[Union[Json, None]] # NOTE: Not used yet
# mod_launcher_json: Optional[Union[Json, None]] # NOTE: Not used yet
# mod_logistics_json: Optional[Union[Json, None]] # NOTE: Not used yet
mod_pres_mgmt_json: Optional[Union[Json, None]] # NOTE: Not used yet
# file_approval_enabled: Optional[bool] # no longer used; use enable_event_file_approval_option 2022-08-23
hide_file_upload_presentation_name: Optional[bool] # is this needed?
hide_session_codes: Optional[bool]
# hide_file_upload_review_input: Optional[bool] # not used anywhere 2022-08-23
# hide_file_upload_email_input: Optional[bool] # not used anywhere 2022-08-23
# hide_file_upload_comments_input: Optional[bool] # not used anywhere 2022-08-23
unauthenticated_access: Optional[bool]
unauthenticated_access_public_endpoint: Optional[bool]
hide: Optional[bool]
status: Optional[int]
review: Optional[bool]
approve: Optional[bool]
ready: Optional[bool]
ready_on: Optional[datetime.datetime]
archive: Optional[bool]
archive_on: Optional[datetime.datetime]
priority: Optional[bool]
sort: Optional[int]
group: Optional[str]
notes: Optional[str]
# Including other related objects
event_registration_cfg: Optional[Event_Registration_Cfg_Base] = Field(
alias = 'registration_cfg'
)
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
# 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_id', 'event_registration_cfg'
]
class Config:
underscore_attrs_are_private = True
allow_population_by_field_name = True
# fields = base_fields
# ### END ### API Event Cfg Models ### Event_Cfg_Base() ###