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 # ### 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()) # --- Standardized Vision IDs (Strings) --- id: Optional[str] = Field(None, **base_fields['journal_entry_id_random']) journal_entry_id: Optional[str] = Field(None, **base_fields['journal_entry_id_random']) journal_id: Optional[str] = Field(None, **base_fields['journal_id_random']) account_id: Optional[str] = Field(None, **base_fields['account_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] 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: Optional[str] = Field(None, **base_fields['journal_entry_id_random']) # parent_id_random: Optional[str] related_entry_id_random: Optional[List[str]] related_entry_id_li: Optional[List[int]] = Field(None, exclude=True) 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 default_qry_str: Optional[str] = None # Default query string used for searching and filtering journal entries # 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) @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('journal_entry_id_random'): values['id'] = rid values['journal_entry_id'] = rid if j_rid := values.get('journal_id_random'): values['journal_id'] = j_rid if a_rid := values.get('account_id_random'): values['account_id'] = a_rid if p_rid := values.get('parent_id_random'): values['parent_id'] = p_rid # 2. Prevent "Collision Population" for k in ['id', 'journal_entry_id', 'journal_id', 'account_id', 'parent_id']: if k in values and not isinstance(values[k], str): 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] = ['file_count'] class Config: underscore_attrs_are_private = True allow_population_by_field_name = False fields = base_fields # ### END ### API Journal Entry Models ### Journal_Entry_Base() ###