from datetime import datetime, time, timedelta from fastapi import APIRouter, Depends, Header, HTTPException, status from pydantic import BaseModel, EmailStr, Field from typing import Dict, List, Optional, Set, Union from ..lib_general import * from ..log import * from app.config import settings from app.db import * from .journal_models import * router = APIRouter() @router.post( "/", response_model=JournalOut, response_model_exclude_unset=True, summary='Create a new journal account', status_code=status.HTTP_201_CREATED ) async def create_journal(journal: JournalIn, x_account_id: str = Header(...)): """ Create a new journal account """ journal = dict(journal) table_name = 'journal' # Look up the journal['account_id_random'] and match to a record ID from Redis if account_id := redis_lookup_id_random(table_name='account', record_id_random=x_account_id): journal['account_id'] = account_id journal.pop('account_id_random') else: print('Something went wrong with the id_random lookup.') raise HTTPException(status_code=500) if result := sql_insert(table_name=table_name, record=journal, id_random_length=16): print(type(result)) if type(result) == int: # isinstance(result, int): # Select the new record to return as a response. if new_journal := dict(sql_select(table_name=table_name, record_id=result)): return new_journal else: print('New journal record was not found.') raise HTTPException(status_code=400) else: print('There is likely a duplicate record. A new record was not created.') raise HTTPException(status_code=400) else: print('No journal record was not created') raise HTTPException(status_code=400) #@router.patch('/{id_random}', response_model=JournalOut, dependencies=[Depends(get_account_header)]) #async def update_journal(id_random: str, journal: JournalIn, x_account_id: str = Header(...)): #async def update_journal(id_random: str, journal: JournalIn): @router.patch( '/{id_random}', response_model=JournalOut, summary='Update a journal account' ) async def update_journal(id_random: str, journal: JournalIn, x_account_id: str = Depends(get_account_header)): """ Update a journal account """ journal = {} journal['id_random'] = id_random journal['account_id_random'] = x_account_id journal['title'] = 'tit' journal['summary'] = 'sum' #journal['created_on'] = datetime.now() journal['default_private'] = True journal['default_public'] = False journal['default_personal'] = False journal['default_professional'] = False return journal @router.delete('/{id_random}', response_model=bool) async def delete_journal(id_random: str, x_account_id: str = Depends(get_account_header)): """ Delete a journal account """ return True return False @router.get('/', response_model=List[JournalOut]) @router.get('/list_all', response_model=List[JournalOut]) async def list_journals(): """ Get a list of journals """ log.setLevel(logging.DEBUG) log.debug(str(locals().keys())+' | '+str(locals().values())) log.debug(locals()) journals = [{'journalname': 'test.journal.1'}, {'journalname': 'test.journal.2'}, {'journalname': 'Scott.Idem'}] log.info('Getting all journals...') sql = """ SELECT * FROM `journal` /*WHERE id=1*/ """ #records = sql_select(sql=sql, as_list=True) records = sql_select(table_name='v_journal', as_list=True) if records: log.info('Got the journal list') return records else: log.info('No journal records found') raise HTTPException(status_code=404) @router.get( '/{journal_id_random}', response_model=JournalOut, summary='Get a journal with an id (id_random)' ) async def get_journal_id(journal_id_random: str, x_account_id: str = Header(...)): """ Get a journal with an id (id_random) """ log.setLevel(logging.WARN) # DEBUG, INFO, WARN, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if account_id := redis_lookup_id_random(table_name='account', record_id_random=x_account_id): #journal['account_id'] = account_id #journal.pop('account_id_random') pass else: log.warning('Something went wrong with the id_random lookup.') raise HTTPException(status_code=500) if journal_id := redis_lookup_id_random(table_name='journal', record_id_random=journal_id_random): #journal['journal_id'] = journal_id #journal.pop('account_id_random') pass else: log.warning('Something went wrong with the id_random lookup.') raise HTTPException(status_code=500) record = sql_select(table_name='v_journal', record_id=journal_id) if record: log.info('Got the journal') return record else: log.info('No journal record found') raise HTTPException(status_code=404)