Working on file uploads and event files.
This commit is contained in:
@@ -37,7 +37,7 @@ def create_event_file_obj(event_file_obj_new:Event_File_Base):
|
||||
# ### BEGIN ### API Event File Methods ### load_event_file_obj() ###
|
||||
def load_event_file_obj(
|
||||
event_file_id: int|str,
|
||||
limit: int = 1000,
|
||||
limit: int = 100,
|
||||
model_as_dict: bool = True,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = False,
|
||||
|
||||
@@ -234,7 +234,7 @@ def create_hosted_file_link(
|
||||
hosted_file_link_data['link_to_type'] = link_to_type # Should this be renamed to "link_to_type" for clarity?
|
||||
hosted_file_link_data['link_to_id'] = link_to_id # Should this be renamed to "link_to_id" for clarity?
|
||||
|
||||
hosted_file_link_data['test'] = 'test'
|
||||
# hosted_file_link_data['test'] = 'test'
|
||||
|
||||
# NOTE: Currently sql_insert does not handle all successful inserts correctly. If there is not an autonum ID then it will return 0 as the ID.
|
||||
if hosted_file_link_data_in_result := sql_insert(data=hosted_file_link_data, table_name='hosted_file_link', id_random_length=0):
|
||||
|
||||
@@ -380,148 +380,54 @@ def get_membership_person_rec_list(
|
||||
# ### END ### API Membership Person Methods ### get_membership_person_rec_list() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Membership Person Methods ### save_membership_person_obj() ###
|
||||
def save_membership_person_obj(order_obj_new:Membership_Person_Base=None):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# ### BEGIN ### API Membership Person Methods ### create_update_membership_person_obj() ###
|
||||
# Updated 2021-08-25
|
||||
def create_update_membership_person_obj(
|
||||
membership_person_dict_obj: Membership_Person_Base|dict,
|
||||
membership_person_id: int|str = None,
|
||||
person_id: int|str = None,
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
|
||||
return_outline: bool = False,
|
||||
) -> int|bool:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if not order_obj_new:
|
||||
return False
|
||||
log.info('Checking requirements...')
|
||||
if membership_person_id:
|
||||
log.info(f'Membership Person ID passed. Update existing Membership Person. Membership Person ID: {membership_person_id}')
|
||||
|
||||
log.debug(order_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True))
|
||||
|
||||
order_line_obj_li_curr = [] # Initialize to store order_line list
|
||||
if order_obj_new.id_random:
|
||||
log.info(f'An order.id {order_obj_new.id} or order.id_random {order_obj_new.id_random} was included. We can update an existing order.')
|
||||
log.info(f'Get the current order_line list to compare with what was sent...')
|
||||
data = {}
|
||||
data['order_id_random'] = order_obj_new.id_random
|
||||
|
||||
if order_line_rec_li_curr := sql_select(table_name='v_order_line', data=data, rm_id_random=True, as_list=True):
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(order_line_rec_li_curr)
|
||||
|
||||
for order_line_rec in order_line_rec_li_curr:
|
||||
try:
|
||||
order_line_obj = Order_Line_Base(**order_line_rec)
|
||||
log.debug(order_line_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
order_line_obj_li_curr.append(order_line_obj)
|
||||
if membership_person_id := redis_lookup_id_random(record_id_random=membership_person_id, table_name='membership_person'): pass
|
||||
else:
|
||||
log.info(f'No order_line records were found')
|
||||
log.error('Membership Person ID passed but is invalid. Failed requirement.')
|
||||
return False
|
||||
|
||||
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='event'): pass
|
||||
else:
|
||||
log.error('Missing or invalid Event ID passed. Not required. Ignoring.')
|
||||
log.info(f'Event ID: {person_id}')
|
||||
|
||||
elif order_obj_new.account_id_random and (order_obj_new.person_id_random or order_obj_new.user_id_random):
|
||||
log.info(f'An account.id_random {order_obj_new.account_id_random} was passed. And either a person.id_random {order_obj_new.person_id_random} or user.id_random {order_obj_new.user_id_random} was passed. We can create a new order.')
|
||||
# Because there was not an order ID, assume there are no order lines yet. So no look up.
|
||||
else:
|
||||
log.info('Either an order ID is required to update an order or an account ID along with a person ID or user ID is required to create an order.')
|
||||
return False
|
||||
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(order_line_obj_li_curr)
|
||||
|
||||
if repl_order_line_list: # This will remove any order line list items not sent with the new order information.
|
||||
log.info('Removing any order line list items not sent with the new order information...')
|
||||
for index, order_line_obj_curr in enumerate(order_line_obj_li_curr):
|
||||
log.info(f'Current: order line ID={order_line_obj_curr.id_random} and product ID= {order_line_obj_curr.product_id_random}')
|
||||
matched_product_id = False
|
||||
for order_line_obj_new in order_obj_new.order_line_list:
|
||||
log.debug(f'Checking new: product ID={order_line_obj_new.product_id_random}')
|
||||
|
||||
if order_line_obj_curr.product_id_random == order_line_obj_new.product_id_random:
|
||||
matched_product_id = True
|
||||
log.debug(f'Matched: product ID={order_line_obj_new.product_id_random}')
|
||||
break
|
||||
else:
|
||||
log.debug(f'No match: product ID={order_line_obj_new.product_id_random}')
|
||||
if not matched_product_id: # Was not found in the new order line list sent
|
||||
log.info(f'Current order line product ID did not match any of the new list. DELETE order line ID {order_line_obj_curr.id_random} with product ID {order_line_obj_curr.product_id_random}')
|
||||
|
||||
if order_line_del_result := sql_delete(table_name='order_line', record_id_random=order_line_obj_curr.id_random):
|
||||
log.info(f'Deleted record and now pop the current list item {index}...')
|
||||
order_line_obj_li_curr.pop(index)
|
||||
log.info('Attempting to get Event ID from related object.')
|
||||
from app.methods.event_methods import get_person_id_w_for_type_id
|
||||
if person_id := get_person_id_w_for_type_id(for_type='event_session', for_id=event_session_id): pass
|
||||
elif person_id := get_person_id_w_for_type_id(for_type='event_presentation', for_id=event_presentation_id): pass
|
||||
elif person_id := get_person_id_w_for_type_id(for_type='membership_person', for_id=membership_person_id): pass
|
||||
else:
|
||||
log.info(f'Current order line product ID matched. Keeping order line ID {order_line_obj_curr.id_random} with product ID {order_line_obj_curr.product_id_random}')
|
||||
# NOTE: That this current order line item will be updated below.
|
||||
log.debug(order_line_obj_li_curr)
|
||||
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
|
||||
log.info('Loop through the line list that was sent and compare with what was pulled from the DB')
|
||||
# Loop through the new line list that was sent and compare with the current line list that was pulled from the DB
|
||||
# Only insert if a product ID does not match
|
||||
# Only update if a product ID does match
|
||||
for order_line_obj_new in order_obj_new.order_line_list:
|
||||
log.info(f'New: order line ID={order_line_obj_new.id_random} and product ID= {order_line_obj_new.product_id_random}')
|
||||
matched_product_id = False
|
||||
for index, order_line_obj_curr in enumerate(order_line_obj_li_curr):
|
||||
log.debug(f'Checking current: product ID={order_line_obj_curr.product_id_random}')
|
||||
|
||||
if order_line_obj_new.product_id_random == order_line_obj_curr.product_id_random:
|
||||
matched_product_id = True
|
||||
log.debug(f'Matched: product ID={order_line_obj_curr.product_id_random}')
|
||||
log.info(f'Updating the current line item with the new line item.')
|
||||
order_line_obj_new.id_random = order_line_obj_curr.id_random
|
||||
order_line_obj_new.id = order_line_obj_curr.id
|
||||
order_line_obj_li_curr[index] = order_line_obj_new
|
||||
break
|
||||
else:
|
||||
log.debug(f'No match: product ID={order_line_obj_curr.product_id_random}')
|
||||
|
||||
if not matched_product_id: # Was not found in the current order line list that was pulled from the DB
|
||||
log.info(f'New order line product ID did not match any of the current list. Append order line ID {order_line_obj_new.id_random} with product ID {order_line_obj_new.product_id_random}')
|
||||
log.info('Append to current list...')
|
||||
order_line_obj_li_curr.append(order_line_obj_new) # These will be inserted/updated below
|
||||
|
||||
# Save merged current and new list to the new order object
|
||||
order_obj_new.order_line_list = order_line_obj_li_curr
|
||||
log.debug(order_obj_new)
|
||||
|
||||
# Final loop through to get the new order totals
|
||||
# Calculate totals
|
||||
order_total_amount:int = 0
|
||||
order_total_quantity:int = 0
|
||||
for order_line_obj_new in order_obj_new.order_line_list:
|
||||
order_total_amount += order_line_obj_new.quantity * order_line_obj_new.amount
|
||||
order_total_quantity += order_line_obj_new.quantity
|
||||
|
||||
order_obj_new.total_bill = order_total_amount # "amount" is used by order_cart and Stripe
|
||||
order_obj_new.total_quantity = order_total_quantity
|
||||
order_obj_new.balance = order_total_amount - order_obj_new.total_paid
|
||||
|
||||
log.debug(order_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_line_list', 'cfg', 'created_on', 'updated_on'}))
|
||||
order_obj_data = order_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_line_list', 'cfg', 'created_on', 'updated_on'})
|
||||
|
||||
# SQL INSERT or UPDATE the order record
|
||||
log.info('SQL INSERT or UPDATE the order record')
|
||||
if order_obj_resp := sql_insert_or_update(data=order_obj_data, table_name='order', rm_id_random=True, id_random_length=8): pass
|
||||
else: return False
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(order_obj_resp)
|
||||
|
||||
if isinstance(order_obj_resp, bool) and order_obj_resp:
|
||||
if order_id := order_obj_new.id: pass
|
||||
elif order_id := order_obj_new.id_random: pass
|
||||
elif isinstance(order_obj_resp, int):
|
||||
order_id = order_obj_resp
|
||||
log.error('Unable to get Event ID from related object.')
|
||||
False
|
||||
else:
|
||||
return False
|
||||
log.debug(f'Order ID={order_id}')
|
||||
log.info('No Membership Person ID passed. Create new Membership Person. Required: Account ID, Event ID')
|
||||
|
||||
# Loop through the order_line list to SQL INSERT or UPDATE the records
|
||||
log.info('Loop through the order_line list to SQL INSERT or UPDATE the records')
|
||||
for order_line_obj_new in order_obj_new.order_line_list:
|
||||
log.info(f"New order_line: order_line_id_random={order_line_obj_new.id_random}; product_id_random={order_line_obj_new.product_id_random}")
|
||||
log.debug(order_line_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=False, exclude={'order_line_id_random', 'product_type_id', 'product_type', 'created_on', 'updated_on'}))
|
||||
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='event'): pass
|
||||
else:
|
||||
log.error('Missing or invalid Event ID passed. Failed requirement.')
|
||||
log.info(f'Event ID: {person_id}')
|
||||
|
||||
order_line_obj_data = order_line_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'order_line_id_random', 'product_type_id', 'product_type', 'created_on', 'updated_on'})
|
||||
|
||||
order_line_obj_data['order_id'] = order_id
|
||||
|
||||
if order_line_obj_resp := sql_insert_or_update(sql=None, data=order_line_obj_data, table_name='order_line', rm_id_random=True, id_random_length=8): pass
|
||||
else: return False
|
||||
log.debug(order_line_obj_resp)
|
||||
return order_id
|
||||
# ### END ### API Membership Person Methods ### save_membership_person_obj() ###
|
||||
log.info('Attempting to get Event ID from related object.')
|
||||
from app.methods.event_methods import get_person_id_w_for_type_id
|
||||
if person_id := get_person_id_w_for_type_id(for_type='event_session', for_id=event_session_id): pass
|
||||
elif person_id := get_person_id_w_for_type_id(for_type='event_presentation', for_id=event_presentation_id): pass
|
||||
else:
|
||||
log.error('Unable to get Event ID from related object.')
|
||||
False
|
||||
# ### END ### API Membership Person Methods ### create_update_membership_person_obj() ###
|
||||
|
||||
@@ -15,6 +15,7 @@ from app.models.organization_models import Organization_Base
|
||||
# from app.models.user_models import User_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Person Models ### Person_Base() ###
|
||||
class Person_Base(BaseModel):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
@@ -188,5 +189,6 @@ class Person_Base(BaseModel):
|
||||
underscore_attrs_are_private = True
|
||||
allow_population_by_field_name = True
|
||||
fields = base_fields
|
||||
# ### END ### API Person Models ### Person_Base() ###
|
||||
|
||||
Person_Base.update_forward_refs()
|
||||
|
||||
@@ -20,24 +20,24 @@ router = APIRouter()
|
||||
|
||||
@router.post('', response_model=Resp_Body_Base)
|
||||
async def post_event_file_obj(
|
||||
obj: Event_File_Base,
|
||||
event_file_obj: Event_File_Base,
|
||||
x_account_id: str = Header(...),
|
||||
return_obj: Optional[bool] = True,
|
||||
by_alias: Optional[bool] = True,
|
||||
exclude_unset: Optional[bool] = True,
|
||||
response: Response = Response,
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
obj_type = 'event_file'
|
||||
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
|
||||
event_file_obj_data_dict = event_file_obj.dict(by_alias=False, exclude_unset=True)
|
||||
result = post_obj_template(
|
||||
obj_type=obj_type,
|
||||
data=obj_data_dict,
|
||||
return_obj=True,
|
||||
by_alias=True,
|
||||
exclude_unset=True,
|
||||
obj_type = obj_type,
|
||||
data = event_file_obj_data_dict,
|
||||
return_obj = True,
|
||||
by_alias = True,
|
||||
exclude_unset = True,
|
||||
)
|
||||
return result
|
||||
|
||||
@@ -52,7 +52,7 @@ async def patch_event_file_obj(
|
||||
exclude_unset: Optional[bool] = True,
|
||||
response: Response = Response,
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
obj_type = 'event_file'
|
||||
@@ -70,6 +70,62 @@ async def patch_event_file_obj(
|
||||
return result
|
||||
|
||||
|
||||
# ### BEGIN ### API Event File ### get_event_file_lookup() ###
|
||||
# Updated 2021-10-06
|
||||
@router.get('/lookup', response_model=Resp_Body_Base)
|
||||
async def event_file_lookup(
|
||||
hosted_file_id: str = Query(..., min_length=11, max_length=22),
|
||||
for_type: str = Query(..., min_length=5, max_length=15),
|
||||
for_id: str = Query(..., min_length=11, max_length=22),
|
||||
inc_hosted_file: bool = False,
|
||||
x_account_id: str = Header(...),
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
response: Response = Response,
|
||||
):
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if hosted_file_id := redis_lookup_id_random(record_id_random=hosted_file_id, table_name='hosted_file'): pass
|
||||
else: return mk_resp(data=False, status_code=404, status_message=f'Hosted File ID was not found. Hosted File ID: {hosted_file_id}', response=response) # Not Found
|
||||
|
||||
if for_id := redis_lookup_id_random(record_id_random=for_id, table_name=for_type): pass
|
||||
else: return mk_resp(data=False, status_code=404, status_message=f'For ID was not found. For Type: {for_type}, For ID: {for_id}', response=response) # Not Found
|
||||
|
||||
data = {}
|
||||
data['hosted_file_id'] = hosted_file_id
|
||||
data['for_type'] = for_type
|
||||
data['for_id'] = for_id
|
||||
log.debug(data)
|
||||
|
||||
sql = f"""
|
||||
SELECT id AS 'event_file_id', id_random AS 'event_file_id_random'
|
||||
FROM `event_file` AS `event_file`
|
||||
WHERE `event_file`.hosted_file_id = :hosted_file_id
|
||||
AND `event_file`.for_type = :for_type
|
||||
AND `event_file`.for_id = :for_id
|
||||
"""
|
||||
log.debug(sql)
|
||||
|
||||
if event_file_obj_result := sql_select(data=data, sql=sql):
|
||||
log.debug(event_file_obj_result)
|
||||
event_file_id = event_file_obj_result.get('event_file_id', None)
|
||||
if event_file_obj := load_event_file_obj(
|
||||
event_file_id = event_file_id,
|
||||
inc_hosted_file = inc_hosted_file,
|
||||
enabled = 'all', # NOTE: This should not be hardcoded
|
||||
model_as_dict = False,
|
||||
):
|
||||
event_file_dict = event_file_obj.dict(by_alias = by_alias, exclude_unset=exclude_unset)
|
||||
else:
|
||||
return mk_resp(data=False, status_code=400, response=response) # Bad Request
|
||||
else:
|
||||
log.debug(event_file_obj_result)
|
||||
return mk_resp(data=None, status_code=404, status_message=f'An Event File was not found. Hosted File ID: {hosted_file_id}, For Type: {for_type}, For ID: {for_id}', response=response) # Not Found
|
||||
return mk_resp(data=event_file_dict, response=response)
|
||||
# ### END ### API Event File ### get_event_file_lookup() ###
|
||||
|
||||
|
||||
@router.delete('/{obj_id}', response_model=Resp_Body_Base)
|
||||
async def delete_event_file_obj(
|
||||
obj_id: str = Query(..., min_length=1, max_length=22),
|
||||
|
||||
@@ -133,10 +133,10 @@ async def upload_files(
|
||||
|
||||
# NOTE: Currently sql_insert does not handel all successful inserts correctly. If there is not an autonum ID then it will return 0 as the ID.
|
||||
if create_hosted_file_link(
|
||||
account_id=account_id,
|
||||
hosted_file_id=hosted_file_id,
|
||||
link_to_type=link_to_type,
|
||||
link_to_id=link_to_id,
|
||||
account_id = account_id,
|
||||
hosted_file_id = hosted_file_id,
|
||||
link_to_type = link_to_type,
|
||||
link_to_id = link_to_id,
|
||||
): pass # This if statement should be improved
|
||||
else:
|
||||
# This if statement should be improved
|
||||
|
||||
Reference in New Issue
Block a user