166 lines
5.7 KiB
Python
166 lines
5.7 KiB
Python
import datetime, pytz
|
|
|
|
from typing import Dict, List, Optional, Set, Union
|
|
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
|
|
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())
|
|
|
|
id_random: Optional[str] = Field(
|
|
**base_fields['journal_id_random'],
|
|
alias = 'journal_id_random',
|
|
)
|
|
id: Optional[int] = Field(
|
|
alias = 'journal_id'
|
|
)
|
|
account_id_random: Optional[str]
|
|
account_id: Optional[int]
|
|
|
|
person_id_random: Optional[str]
|
|
person_id: Optional[int]
|
|
|
|
user_id_random: Optional[str]
|
|
user_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]
|
|
|
|
description: Optional[str]
|
|
description_html: Optional[str]
|
|
description_json: Optional[str]
|
|
|
|
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
|
|
|
|
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_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)
|
|
|
|
@validator('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('id_random'):
|
|
return redis_lookup_id_random(record_id_random=id_random, table_name='journal')
|
|
return None
|
|
|
|
@validator('person_id', always=True)
|
|
def person_id_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.WARNING)
|
|
log.debug(locals())
|
|
|
|
if values['person_id_random']:
|
|
return redis_lookup_id_random(record_id_random=values['person_id_random'], table_name='person')
|
|
return None
|
|
|
|
@validator('user_id', always=True)
|
|
def user_id_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.WARNING)
|
|
log.debug(locals())
|
|
|
|
if values['user_id_random']:
|
|
return redis_lookup_id_random(record_id_random=values['user_id_random'], table_name='user')
|
|
return None
|
|
|
|
class Config:
|
|
underscore_attrs_are_private = True
|
|
allow_population_by_field_name = True
|
|
fields = base_fields
|
|
# ### END ### API Journal Models ### Journal_Base() ###
|