Working finally getting this to interact with the actual front end.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import datetime
|
||||
import datetime, pytz, time
|
||||
#from datetime import datetime, time, timedelta
|
||||
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, status
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
@@ -68,6 +68,74 @@ async def patch_user_obj(
|
||||
return result
|
||||
|
||||
|
||||
# This will look up a user based on the auth key given
|
||||
# This can only be done once per key. It will be deleted if found
|
||||
# A new one will need to be requested for a particular user each time
|
||||
@router.get('/authenticate/auth_key/{auth_key}', response_model=Resp_Body_Base)
|
||||
async def auth_key_get_user_obj(
|
||||
auth_key: str = Query(..., min_length=11, max_length=22),
|
||||
x_account_id: str = Header(...),
|
||||
by_alias: Optional[bool] = True,
|
||||
exclude_unset: Optional[bool] = True,
|
||||
exclude_none: Optional[bool] = True,
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if sql_select_result := sql_select(table_name='user', field_name='auth_key', field_value=auth_key):
|
||||
log.debug(sql_select_result)
|
||||
|
||||
resp_data = {}
|
||||
resp_data['user_id_random'] = sql_select_result.get('id_random')
|
||||
resp_data['username'] = sql_select_result.get('username')
|
||||
resp_data['enable'] = sql_select_result.get('enable')
|
||||
resp_data['enable_from'] = sql_select_result.get('enable_from')
|
||||
resp_data['enable_to'] = sql_select_result.get('enable_to')
|
||||
try:
|
||||
user_obj = User_Base(**sql_select_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(user_obj)
|
||||
log.debug(user_obj.get('enable_from', None))
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
|
||||
current_utc_datetime = datetime.datetime.now(datetime.timezone.utc)
|
||||
|
||||
log.debug(user_obj.get('enable_from', None).astimezone(pytz.UTC))
|
||||
user_enable_from = user_obj.get('enable_from', None).astimezone(pytz.UTC)
|
||||
log.debug(user_enable_from)
|
||||
|
||||
log.debug(user_obj.get('enable_to', None))
|
||||
user_enable_to = user_obj.get('enable_to', None).astimezone(pytz.UTC)
|
||||
log.debug(user_enable_to)
|
||||
|
||||
if resp_data['enable']: pass
|
||||
else:
|
||||
log.info('The user account has been disabled')
|
||||
if user_enable_from <= current_utc_datetime:
|
||||
log.info('Enable from datetime is valid')
|
||||
else:
|
||||
log.info('Enable from datetime is in the future. Please wait.')
|
||||
if user_enable_to >= current_utc_datetime:
|
||||
log.info('Enable to datetime is valid')
|
||||
else:
|
||||
log.info('Enable to datetime is in the past. Your user account has been disabled.')
|
||||
|
||||
update_data = {}
|
||||
update_data['id'] = sql_select_result.get('id')
|
||||
update_data['auth_key'] = None
|
||||
|
||||
if sql_update_resp := sql_update(table_name='user', data=update_data):
|
||||
log.info('The user record was updated with a NULL auth_key')
|
||||
else:
|
||||
log.info('The user record was not updated with a NULL auth_key')
|
||||
log.debug(sql_update_resp)
|
||||
|
||||
return mk_resp(data=user_obj)
|
||||
else:
|
||||
return mk_resp(data=None, status_code=404)
|
||||
|
||||
|
||||
@router.get('/list', response_model=Resp_Body_Base)
|
||||
async def get_user_obj_li(
|
||||
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
||||
|
||||
Reference in New Issue
Block a user