119 lines
7.3 KiB
Python
119 lines
7.3 KiB
Python
import datetime
|
|
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Response, status
|
|
from pydantic import BaseModel, EmailStr, Field
|
|
from typing import Dict, List, Optional, Set, Union
|
|
|
|
from app.lib_general import *
|
|
from app.config import settings
|
|
from app.db_sql import *
|
|
|
|
from app.routers.api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
|
|
|
from app.methods.order_line_methods import get_order_line_rec_list, load_order_line_obj, load_order_line_obj_full_detail
|
|
|
|
from app.models.response_models import Resp_Body_Base, mk_resp
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# ### BEGIN ### API Order Line ### get_obj_id_order_line_list() ###
|
|
# Updated 2022-01-04
|
|
@router.get('/{obj_type}/{obj_id}/order/line/list', response_model=Resp_Body_Base)
|
|
async def get_obj_id_order_line_list(
|
|
obj_type: str = Query(..., min_length=4, max_length=25), # Expects account or order or person
|
|
obj_id: str = Query(..., min_length=11, max_length=22),
|
|
from_datetime: datetime.datetime = None,
|
|
to_datetime: datetime.datetime = None,
|
|
prod_type: str = 'all', # all, cont_edu_cert, event, fundraising, membership, etc
|
|
status: str = 'closed', # open, locked, reopened?, closed (complete), canceled, other
|
|
full_detail: bool = False, # Uses a different view: v_order_line_full_detail
|
|
# enabled: str = 'enabled',
|
|
limit: int = 50,
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
create_export: Optional[bool] = False,
|
|
x_account_id: str = Header(..., min_length=11, max_length=22),
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if obj_type in ['account', 'order', 'person']:
|
|
if obj_id := redis_lookup_id_random(record_id_random=obj_id, table_name=obj_type): pass
|
|
else: return mk_resp(data=None, status_code=404, response=response)
|
|
else:
|
|
log.warning('Likely bad request')
|
|
return mk_resp(data=False, status_code=400, response=response) # Bad Request
|
|
|
|
if order_line_rec_list_result := get_order_line_rec_list(
|
|
for_obj_type = obj_type,
|
|
for_obj_id = obj_id,
|
|
from_datetime = from_datetime,
|
|
to_datetime = to_datetime,
|
|
prod_type = prod_type,
|
|
status = status,
|
|
full_detail = full_detail,
|
|
# enabled = enabled,
|
|
limit = limit,
|
|
):
|
|
order_line_result_list = []
|
|
data_dict_list_for_export = []
|
|
for order_line_rec in order_line_rec_list_result:
|
|
if not full_detail:
|
|
if load_order_line_result := load_order_line_obj(
|
|
order_line_id = order_line_rec.get('order_line_id', None),
|
|
limit = limit,
|
|
by_alias = by_alias,
|
|
exclude_unset = exclude_unset,
|
|
# model_as_dict = model_as_dict,
|
|
):
|
|
order_line_result_list.append(load_order_line_result)
|
|
else:
|
|
order_line_result_list.append(None)
|
|
else: # Uses a different view: v_order_line_full_detail
|
|
if load_order_line_result := load_order_line_obj_full_detail(
|
|
order_line_rec = order_line_rec,
|
|
by_alias = by_alias,
|
|
exclude_unset = exclude_unset,
|
|
model_as_dict = False,
|
|
):
|
|
if create_export:
|
|
data_dict = load_order_line_result.dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
|
data_dict_list_for_export.append(data_dict)
|
|
order_line_result_list.append(load_order_line_result)
|
|
else:
|
|
order_line_result_list.append(None)
|
|
response_data = order_line_result_list
|
|
elif isinstance(order_line_rec_list_result, list) or order_line_rec_list_result is None: # Empty list or None
|
|
log.info('No results')
|
|
return mk_resp(data=None, status_code=404, response=response) # Not Found
|
|
else:
|
|
log.warning('Likely bad request')
|
|
return mk_resp(data=False, status_code=400, response=response) # Bad Request
|
|
|
|
if create_export:
|
|
# column_name_li = ['order_id_random', 'order_line_id_random', '', 'product_name', 'quantity', 'amount', 'dollar_amount', 'person_email']
|
|
|
|
# column_name_li = ['order_line_id_random', 'order_id_random', 'product_id_random', 'product_type', 'product_name', 'product_unit_price', 'product_recurring', 'curr_product_id_random', 'curr_product_type', 'curr_product_type_name', 'curr_product_name', 'name', 'quantity', 'amount', 'dollar_amount', 'recurring', 'message', 'person_id_random', 'person_given_name', 'person_family_name', 'person_display_name', 'person_full_name', 'person_contact_email', 'person_contact_cc_email', 'person_contact_phone_mobile', 'person_contact_phone_home', 'person_contact_phone_office', 'person_contact_phone_land', 'person_contact_phone_fax', 'person_contact_phone_other', 'person_contact_address_name', 'person_contact_address_organization_name', 'person_contact_address_line_1', 'person_contact_address_line_2', 'person_contact_address_line_3', 'person_contact_address_city', 'person_contact_address_country_subdivision_code', 'person_contact_address_state_province', 'person_contact_address_postal_code', 'person_contact_address_country_alpha_2_code', 'person_contact_address_country_name', 'person_contact_address_country', 'order_status', 'order_created_on', 'order_updated_on', 'created_on', 'updated_on']
|
|
|
|
column_name_li = ['order_line_id_random', 'order_id_random', 'product_id_random', 'product_type', 'product_name', 'quantity', 'amount', 'dollar_amount', 'message', 'person_id_random', 'person_given_name', 'person_family_name', 'person_display_name', 'person_full_name', 'person_contact_email', 'person_contact_cc_email', 'person_contact_address_name', 'person_contact_address_organization_name', 'person_contact_address_line_1', 'person_contact_address_line_2', 'person_contact_address_line_3', 'person_contact_address_city', 'person_contact_address_country_subdivision_code', 'person_contact_address_state_province', 'person_contact_address_postal_code', 'person_contact_address_country_alpha_2_code', 'person_contact_address_country_name', 'person_contact_address_country', 'order_status', 'order_created_on', 'order_updated_on']
|
|
|
|
|
|
# column_name_li = []
|
|
datetime_format='%Y-%m-%d_%H%M'
|
|
|
|
# current_datetime = datetime.datetime.now() # Servers timezone (Eastern)
|
|
current_datetime_utc = datetime.datetime.utcnow()
|
|
current_datetime_utc = current_datetime_utc.strftime(datetime_format)
|
|
filename = f'order_line_list_{current_datetime_utc}'
|
|
if result := create_export_file(data_dict_list=data_dict_list_for_export, column_name_li=column_name_li, subdir_path='order_line', filename=filename, export_type='Excel'):
|
|
tmp_file_path = result
|
|
else:
|
|
log.error('Something went wrong while creating or saving the export file')
|
|
tmp_file_path = result
|
|
else: tmp_file_path = None
|
|
|
|
return mk_resp(data=response_data, tmp_file_path=tmp_file_path, response=response)
|
|
# ### END ### API Order Line ### get_obj_id_order_line_list() ###
|