Files
OSIT-AE-API-FastAPI/app/models/journal_entry_models.py
Scott Idem 1c0922ace2 Enhance API robustness: Add model validators, view-field filtering, and test suite.
- Added validators to Person_Base, Journal_Base, Journal_Entry_Base, Address_Base, and Contact_Base to handle null values and unsafe lookups.
- Implemented 'fields_to_exclude_from_db' ClassVar in Journal models to prevent view-only fields from causing DB errors.
- Updated Contact object map to align with DB schema.
- Added comprehensive test suite in 'tests/' directory (model validation, filtering logic).
- Updated GEMINI.md with progress.
2026-01-09 15:36:28 -05:00

154 lines
5.6 KiB
Python

import datetime, pytz
from typing import Dict, List, Optional, Set, Union, ClassVar
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, 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
# ### BEGIN ### API Journal Entry Models ### Journal_Entry_Base() ###
class Journal_Entry_Base(BaseModel):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
id_random: Optional[str] = Field(
**base_fields['journal_entry_id_random'],
alias = 'journal_entry_id_random',
)
id: Optional[int] = Field(
alias = 'journal_entry_id'
)
journal_id_random: Optional[str]
journal_id: Optional[int]
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]
for_id_random: Optional[str]
name: Optional[str]
short_name: Optional[str]
summary: Optional[str]
outline: Optional[str]
content: Optional[str]
content_html: Optional[str]
content_json: Optional[Union[Json, None]]
content_encrypted: Optional[str]
history: Optional[str] # Used to store the history of the journal entry content
history_encrypted: Optional[str]
passcode_hash: Optional[str] # Used to store the hash of the passcode for looking up the passcode
template: Optional[bool] = False # If this is a template entry, it can be used to create new entries based on this template
type_code: Optional[str] # 'log', 'tracking', 'personal', 'professional', etc
topic_code: Optional[str]
category_code: Optional[str]
# keywords: Optional[str]
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
parent_id_random: Optional[str]
parent_id: Optional[int]
related_entry_id_random: Optional[List[str]]
related_entry_id_li: Optional[List[int]]
due_datetime: Optional[datetime.datetime]
due_alert: Optional[bool]
archive_on: Optional[datetime.datetime]
archive: Optional[bool]
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
passcode_read: Optional[str] # Used to read the journal entry
passcode_read_expire: Optional[int] # Number of seconds to expire the read passcode
# passcode_write: Optional[str] # Used to write to the journal entry
# passcode_write_expire: Optional[int] # Number of seconds to expire the write passcode
url_kv_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 about the journal entry
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
# This is only for convenience. Probably going to keep unless it causes a problem.
file_count: Optional[int]
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
@validator('id', always=True)
def journal_entry_id_lookup(cls, v, values, **kwargs):
if isinstance(v, int) and v > 0: return v
elif id_random := values.get('id_random'):
return redis_lookup_id_random(record_id_random=id_random, table_name='journal_entry')
return None
@validator('journal_id', always=True)
def journal_id_lookup(cls, v, values, **kwargs):
if isinstance(v, int) and v > 0: return v
elif id_random := values.get('journal_id_random'):
return redis_lookup_id_random(record_id_random=id_random, table_name='journal')
return None
@validator('parent_id', always=True)
def parent_id_lookup(cls, v, values, **kwargs):
if isinstance(v, int) and v > 0: return v
elif id_random := values.get('parent_id_random'):
return redis_lookup_id_random(record_id_random=id_random, table_name='journal_entry')
return None
@validator('for_id', always=True)
def for_id_lookup(cls, v, values, **kwargs):
log.setLevel(logging.WARNING)
log.debug(locals())
if values.get('for_id_random') and values.get('for_type'):
return redis_lookup_id_random(record_id_random=values['for_id_random'], table_name=values['for_type'])
return None
# 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] = ['file_count']
class Config:
underscore_attrs_are_private = True
allow_population_by_field_name = True
fields = base_fields
# ### END ### API Journal Entry Models ### Journal_Entry_Base() ###