Just lots of work and tweeks
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from __future__ import annotations
|
||||
import datetime
|
||||
import datetime, pytz
|
||||
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
|
||||
@@ -20,8 +20,10 @@ from app.models.person_models import Person_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Person Methods ### load_person_obj() ###
|
||||
# Updated 2021-12-15
|
||||
def load_person_obj(
|
||||
person_id: int|str,
|
||||
auth_key: str = None,
|
||||
limit: int = 1000,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
@@ -57,16 +59,78 @@ def load_person_obj(
|
||||
inc_user: bool = False,
|
||||
inc_user_role_list: bool = False,
|
||||
) -> Person_Base|dict|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
|
||||
else: return False
|
||||
else: return None
|
||||
|
||||
if person_rec := sql_select(table_name='v_person', record_id=person_id): pass
|
||||
else: return False
|
||||
if auth_key:
|
||||
sql = f"""
|
||||
SELECT *
|
||||
FROM `v_person` AS person
|
||||
WHERE person.id = :person_id
|
||||
AND person.allow_auth_key = 1
|
||||
AND person.auth_key = :auth_key
|
||||
LIMIT 1
|
||||
;
|
||||
"""
|
||||
log.debug(sql)
|
||||
|
||||
data = {}
|
||||
data['person_id'] = person_id
|
||||
data['auth_key'] = auth_key
|
||||
log.debug(data)
|
||||
|
||||
if person_rec := sql_select(sql=sql, data=data):
|
||||
# Only wipe the key if the last update to person record was more than X minutes
|
||||
updated_on = person_rec.get('updated_on')
|
||||
updated_on_string = updated_on.isoformat()
|
||||
log.debug(updated_on_string)
|
||||
|
||||
eastern = pytz.timezone('US/Eastern')
|
||||
|
||||
updated_on_localized = eastern.localize(updated_on)
|
||||
log.debug(updated_on_localized.isoformat())
|
||||
|
||||
# updated_on_utc = person_rec.get('updated_on').replace(tzinfo=datetime.timezone.utc)
|
||||
# updated_on_tz = person_rec.get('updated_on').replace(tzinfo=eastern)
|
||||
# updated_on_tz_string = updated_on_tz.isoformat()
|
||||
# log.debug(updated_on_tz_string)
|
||||
|
||||
current_datetime_utc = datetime.datetime.utcnow()
|
||||
current_datetime_utc_string = current_datetime_utc.isoformat()
|
||||
log.debug(current_datetime_utc_string)
|
||||
|
||||
# datetime_difference = current_datetime_utc - updated_on_localized
|
||||
# total_seconds = datetime_difference.total_seconds()
|
||||
# log.debug(total_seconds)
|
||||
|
||||
current_datetime_utc_localize = pytz.utc.localize(current_datetime_utc)
|
||||
current_datetime_utc_localize_string = current_datetime_utc_localize.isoformat()
|
||||
|
||||
# test_datetime_utc = datetime.datetime.utcnow()- datetime.timedelta(seconds=120)
|
||||
datetime_difference = current_datetime_utc_localize - updated_on_localized
|
||||
total_seconds = datetime_difference.total_seconds()
|
||||
log.debug(total_seconds)
|
||||
|
||||
if total_seconds > 7200: # 7200 seconds = 2 hours
|
||||
log.warning('The authorization key has expired')
|
||||
|
||||
update_person_data = {}
|
||||
update_person_data['id'] = person_id
|
||||
update_person_data['auth_key'] = None # secrets.token_urlsafe(default_num_bytes)
|
||||
|
||||
if person_rec_update_result := sql_update(table_name='person', data=update_person_data):
|
||||
log.info('The person record was updated with a new auth_key')
|
||||
else:
|
||||
log.warning('The authorization key is still valid')
|
||||
|
||||
else: return person_rec # None or False
|
||||
else:
|
||||
if person_rec := sql_select(table_name='v_person', record_id=person_id): pass
|
||||
else: return person_rec # None or False
|
||||
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(person_rec)
|
||||
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user