1. Added fallback mechanism to Event_File_Base to resolve string IDs from integers when views return partial data.\n2. Added 'a2pPIT_W28o' as a permanent regression test target.\n3. Hardened lu_file_purpose_id stripping.
198 lines
8.3 KiB
Python
198 lines
8.3 KiB
Python
import datetime, pytz
|
|
|
|
from typing import Dict, List, Optional, Set, Union
|
|
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
|
|
from app.log import log, logging, logger_reset
|
|
|
|
from app.models.common_field_schema import base_fields, default_num_bytes
|
|
from app.models.hosted_file_models import Hosted_File_Base
|
|
|
|
|
|
# ### BEGIN ### API Event File Models ### Event_File_Base() ###
|
|
class Event_File_Base(BaseModel):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# --- Standardized Vision IDs (Strings for API, Integers for DB) ---
|
|
id: Optional[Union[int, str]] = Field(**base_fields['event_file_id_random'])
|
|
event_file_id: Optional[Union[int, str]] = Field(**base_fields['event_file_id_random'])
|
|
hosted_file_id: Optional[Union[int, str]] = Field(**base_fields['hosted_file_id_random'])
|
|
|
|
# Generic Relational target
|
|
for_id: Optional[Union[int, str]] = Field(**base_fields['obj_id_random'])
|
|
|
|
event_id: Optional[Union[int, str]] = Field(**base_fields['event_id_random'])
|
|
event_exhibit_id: Optional[Union[int, str]] = Field(**base_fields['event_exhibit_id_random'])
|
|
event_location_id: Optional[Union[int, str]] = Field(**base_fields['event_location_id_random'])
|
|
event_presentation_id: Optional[Union[int, str]] = Field(**base_fields['event_presentation_id_random'])
|
|
event_presenter_id: Optional[Union[int, str]] = Field(**base_fields['event_presenter_id_random'])
|
|
event_session_id: Optional[Union[int, str]] = Field(**base_fields['event_session_id_random'])
|
|
event_track_id: Optional[Union[int, str]] = Field(**base_fields['event_track_id_random'])
|
|
|
|
# --- Standardized Legacy / Internal IDs (Excluded) ---
|
|
id_random: Optional[str] = Field(None, alias='event_file_id_random', exclude=True)
|
|
hosted_file_id_random: Optional[str] = Field(None, exclude=True)
|
|
for_id_random: Optional[str] = Field(None, exclude=True)
|
|
event_id_random: Optional[str] = Field(None, exclude=True)
|
|
event_exhibit_id_random: Optional[str] = Field(None, exclude=True)
|
|
event_location_id_random: Optional[str] = Field(None, exclude=True)
|
|
event_presentation_id_random: Optional[str] = Field(None, exclude=True)
|
|
event_presenter_id_random: Optional[str] = Field(None, exclude=True)
|
|
event_session_id_random: Optional[str] = Field(None, exclude=True)
|
|
event_track_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 during READ operations.
|
|
Falls back to Redis/DB lookups if random string IDs are missing from the view.
|
|
"""
|
|
# 1. Map Primary Object ID
|
|
rid = values.get('id_random') or values.get('event_file_id_random')
|
|
if rid and isinstance(rid, str):
|
|
values['id'] = rid
|
|
values['event_file_id'] = rid
|
|
|
|
# 2. Map & Resolve Relational IDs
|
|
# (Field Name, Table Name)
|
|
id_map = [
|
|
('hosted_file_id', 'hosted_file'),
|
|
('event_id', 'event'),
|
|
('event_exhibit_id', 'event_exhibit'),
|
|
('event_location_id', 'event_location'),
|
|
('event_presentation_id', 'event_presentation'),
|
|
('event_presenter_id', 'event_presenter'),
|
|
('event_session_id', 'event_session'),
|
|
('event_track_id', 'event_track'),
|
|
('lu_file_purpose_id', 'lu_file_purpose'),
|
|
]
|
|
|
|
# 2a. Handle specific relational fields
|
|
for field, table in id_map:
|
|
# Check for existing random string version
|
|
r_val = values.get(f'{field}_random')
|
|
if r_val and isinstance(r_val, str):
|
|
values[field] = r_val
|
|
elif values.get(field) and isinstance(values[field], int):
|
|
# Fallback: Resolve from Redis/DB if missing from view result
|
|
resolved_rid = get_id_random(values[field], table)
|
|
if resolved_rid:
|
|
values[field] = resolved_rid
|
|
values[f'{field}_random'] = resolved_rid
|
|
|
|
# 2b. Handle Polymorphic for_id
|
|
if f_rid := values.get('for_id_random'):
|
|
values['for_id'] = f_rid
|
|
elif values.get('for_id') and isinstance(values.get('for_id'), int) and values.get('for_type'):
|
|
# Resolve based on the for_type
|
|
resolved_for_rid = get_id_random(values['for_id'], values['for_type'])
|
|
if resolved_for_rid:
|
|
values['for_id'] = resolved_for_rid
|
|
values['for_id_random'] = resolved_for_rid
|
|
|
|
# 3. Final Vision Enforcement: Strip internal integers
|
|
id_fields = [f for f, t in id_map] + ['id', 'event_file_id', 'for_id']
|
|
for k in id_fields:
|
|
val = values.get(k)
|
|
if val is not None and not isinstance(val, str):
|
|
values[k] = None
|
|
|
|
return values
|
|
|
|
for_type: Optional[str]
|
|
|
|
filename: Optional[str]
|
|
filename_no_ext: Optional[str] # Currently created with a view
|
|
filename_w_ext: Optional[str] # Currently created with a view
|
|
extension: Optional[str]
|
|
title: Optional[str]
|
|
description: Optional[str]
|
|
|
|
lu_file_purpose_id: Optional[int]
|
|
file_purpose: 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
|
|
|
|
#### NOTE ***** NOTE
|
|
open_in_os: Optional[str]
|
|
public_use: Optional[bool] = False
|
|
public: Optional[bool]
|
|
approve: Optional[bool]
|
|
#### open_in_os needs to be set
|
|
#### Are other important fields missing????
|
|
publish_optout: Optional[bool]
|
|
#### NOTE ^^^^^ NOTE
|
|
|
|
enable: Optional[bool]
|
|
enable_from: Optional[datetime.datetime] = None
|
|
enable_to: Optional[datetime.datetime] = None
|
|
|
|
hide: Optional[bool]
|
|
priority: Optional[bool]
|
|
sort: Optional[int]
|
|
group: Optional[str] # Same or similar as file_purpose?
|
|
|
|
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.
|
|
hosted_file_hash_sha256: Optional[str] = Field(
|
|
alias = 'hash_sha256'
|
|
)
|
|
hosted_file_subdirectory_path: Optional[str] = Field( # NOTE: This will frequently only contain numbers, but it still needs to be a string
|
|
alias = 'subdirectory_path',
|
|
exclude = True
|
|
)
|
|
hosted_file_content_type: Optional[str] = Field(
|
|
alias = 'content_type'
|
|
)
|
|
hosted_file_size: Optional[str] = Field(
|
|
alias = 'file_size'
|
|
)
|
|
|
|
lu_event_file_purpose_name: Optional[str] = Field(
|
|
alias = 'file_purpose_name'
|
|
)
|
|
|
|
event_name: Optional[str]
|
|
event_code: Optional[str]
|
|
event_start_datetime: Optional[datetime.datetime]
|
|
event_end_datetime: Optional[datetime.datetime]
|
|
event_location_code: Optional[str]
|
|
event_location_name: Optional[str]
|
|
event_presentation_code: Optional[str]
|
|
event_presentation_type_code: Optional[str]
|
|
event_presentation_name: Optional[str]
|
|
event_presentation_start_datetime: Optional[datetime.datetime]
|
|
event_presentation_end_datetime: Optional[datetime.datetime]
|
|
event_presenter_code: Optional[str]
|
|
event_presenter_given_name: Optional[str]
|
|
event_presenter_family_name: Optional[str]
|
|
event_presenter_full_name: Optional[str]
|
|
event_presenter_email: Optional[str]
|
|
event_session_code: Optional[str]
|
|
event_session_type_code: Optional[str]
|
|
event_session_name: Optional[str]
|
|
event_session_start_datetime: Optional[datetime.datetime]
|
|
event_session_end_datetime: Optional[datetime.datetime]
|
|
event_track_code: Optional[str]
|
|
event_track_name: Optional[str]
|
|
|
|
# Including other related objects
|
|
hosted_file: Optional[Union[Hosted_File_Base, None]]
|
|
|
|
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
|
|
|
class Config:
|
|
underscore_attrs_are_private = True
|
|
allow_population_by_field_name = True
|
|
fields = base_fields
|
|
# ### END ### API Event File Models ### Event_File_Base() ###
|