Files
OSIT-AE-API-FastAPI/app/models/journal_models.py
Scott Idem a7c82615ab fix(v3-vision): platform-wide hardening of ID Vision models
Hardened root_validators in Journal, Post, Page, and Hosted File models to use Union[int, str] for Vision ID fields. This prevents resolved integer IDs from being deleted during CREATE/UPDATE operations, resolving a critical regression found during Post Comment bug fixing.
2026-02-05 20:38:19 -05:00

169 lines
6.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.journal_entry_models import Journal_Entry_Base
# from app.models.person_models import Person_Base
# ### BEGIN ### API Journal Models ### Journal_Base() ###
class Journal_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(None, **base_fields['journal_id_random'])
journal_id: Optional[Union[int, str]] = Field(None, **base_fields['journal_id_random'])
account_id: Optional[Union[int, str]] = Field(None, **base_fields['account_id_random'])
person_id: Optional[Union[int, str]] = Field(None, **base_fields['person_id_random'])
user_id: Optional[Union[int, str]] = Field(None, **base_fields['user_id_random'])
external_id: Optional[str] # ID generated by or for external systems (should be stable and not change)
import_id: Optional[str] # Used for import purposes to track the source of the data
code: Optional[str]
for_type: Optional[str] # 'person', 'user', 'account', etc
for_id: Optional[int] = Field(None, exclude=True)
for_id_random: Optional[str]
name: Optional[str]
short_name: Optional[str]
summary: Optional[str]
outline: Optional[str]
description: Optional[str]
description_html: Optional[str]
description_json: Optional[Union[Json, None]]
type_code: Optional[str] # 'log', 'tracking', 'personal', 'professional', etc
tags: Optional[str]
start_datetime: Optional[datetime.datetime]
end_datetime: Optional[datetime.datetime]
timezone: Optional[str] # = 'UTC' # Default to UTC
seconds: Optional[int]
location: Optional[str]
latitude: Optional[float]
longitude: Optional[float]
billable: Optional[bool]
bill_to: Optional[str]
alert: Optional[bool] = False
alert_msg: Optional[str] = None
private: Optional[bool] = True
public: Optional[bool] = False
personal: Optional[bool] = True
professional: Optional[bool] = False
# Default the Journal Entries to private, public, personal, and professional
default_private: Optional[bool]
default_public: Optional[bool]
default_personal: Optional[bool]
default_professional: Optional[bool]
due_datetime: Optional[datetime.datetime]
due_alert: Optional[bool]
archive_on: Optional[datetime.datetime]
archive: Optional[bool]
allow_auth: Optional[bool]
auth_key: Optional[str]
passcode: Optional[str] # Used to read and write to the journal entry
passcode_timeout: Optional[int] # Number of seconds before asking for the passcode again
# private_passcode: Optional[str]
# public_passcode: Optional[str]
passcode_read: Optional[str] # Used to read the journal
passcode_read_expire: Optional[int] # Number of seconds to expire the read passcode
# passcode_write: Optional[str] # Used to write to the journal
# passcode_write_expire: Optional[int] # Number of seconds to expire the write passcode
private_passcode: Optional[str] # Used with the passcode to encrypt and decrypt the Journal Entries
public_passcode: Optional[str] # Used to allow external people to view a Journal Entry
sort_by: Optional[str]
sort_by_desc: Optional[bool]
cfg_json: Optional[Union[Json, None]]
data_json: Optional[Union[Json, None]] # Used to store additional data for the journal
meta_json: Optional[Union[Json, None]] # Used to store additional data for about the journal
enable: Optional[bool]
hide: 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 other related objects
journal_entry_count: Optional[int] # Number of journal entries in the journal
journal_entry_list: Optional[list[Journal_Entry_Base]] # Journal_Entry_Base()
# This is only for convenience. Probably going to keep unless it causes a problem.
file_count: Optional[int] # Only files directly under the journal
file_count_all: Optional[int] # All files under a journal and entries
# This person is essentially the owner of the journal
# person: Optional[Person_Base]
person_external_id: Optional[str]
person_given_name: Optional[str] = None
person_family_name: Optional[str] = None
person_full_name: Optional[str] = None
person_primary_email: Optional[str] = None
person_passcode: Optional[str] = None
# person: Optional[Person_Base]
# user: Optional[User_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 during READ operations.
During CREATE (POST) operations, we ensure resolved integers are preserved.
"""
# 1. Map Random Strings to Clean Names
rid = values.get('id_random') or values.get('journal_id_random')
if rid and isinstance(rid, str):
values['id'] = rid
values['journal_id'] = rid
if a_rid := values.get('account_id_random'): values['account_id'] = a_rid
if p_rid := values.get('person_id_random'): values['person_id'] = p_rid
if u_rid := values.get('user_id_random'): values['user_id'] = u_rid
# 2. Prevent "Collision Population" or leakage of integers during API responses
for k in ['id', 'journal_id', 'account_id', 'person_id', 'user_id']:
val = values.get(k)
if val is not None and not isinstance(val, str):
if values.get(f'{k}_random') or (k=='id' and values.get('id_random')):
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] = [
'person_external_id', 'person_given_name', 'person_family_name',
'person_full_name', 'person_primary_email', 'person_passcode',
'journal_entry_count', 'file_count', 'file_count_all'
]
class Config:
underscore_attrs_are_private = True
allow_population_by_field_name = False
fields = base_fields
# ### END ### API Journal Models ### Journal_Base() ###