Moving things to use the common_route_params. Rewriting most of things order and order_cart related!
This commit is contained in:
@@ -18,7 +18,7 @@ from . import config
|
||||
from app.log import log, logging
|
||||
|
||||
# Import the routers here first:
|
||||
from app.routers import api_crud, api, importing, account, activity_log, address, archive, archive_content, contact, cont_edu_cert, cont_edu_cert_person, event, event_badge, event_badge_template, event_exhibit, event_file, event_importing, event_location, event_person, event_person_detail, event_person_tracking, event_presentation, event_presenter, event_registration, event_session, flask_cfg, hosted_file, journal, journal_entry, log_client_viewing, lookup, membership_cfg, membership_group, membership_person_group, membership_person, membership_person_profile, membership_type, membership_person_type, order, order_line, order_cart, organization, page, person, person_user, post, post_comment, product, site, site_domain, user, websockets#, e_impexium
|
||||
from app.routers import api_crud, api, importing, account, activity_log, address, archive, archive_content, contact, cont_edu_cert, cont_edu_cert_person, event, event_badge, event_badge_template, event_exhibit, event_file, event_importing, event_location, event_person, event_person_detail, event_person_tracking, event_presentation, event_presenter, event_registration, event_session, flask_cfg, hosted_file, journal, journal_entry, log_client_viewing, lookup, membership_cfg, membership_group, membership_person_group, membership_person, membership_person_profile, membership_type, membership_person_type, order, order_v3, order_line, order_cart, organization, page, person, person_user, post, post_comment, product, site, site_domain, user, websockets#, e_impexium
|
||||
|
||||
from app.db_sql import db
|
||||
|
||||
@@ -245,6 +245,11 @@ app.include_router(
|
||||
# prefix='/order',
|
||||
tags=['Order'],
|
||||
)
|
||||
app.include_router(
|
||||
order_v3.router,
|
||||
# prefix='/order',
|
||||
tags=['Order v3'],
|
||||
)
|
||||
app.include_router(
|
||||
order_line.router,
|
||||
# prefix='/order',
|
||||
|
||||
@@ -176,12 +176,15 @@ def save_order_obj(order_obj_new:Order_Base, repl_order_line_li:bool=False):
|
||||
# Updated 2021-11-19
|
||||
def load_order_obj(
|
||||
order_id: int|str,
|
||||
inc_address: bool = False,
|
||||
inc_contact: bool = False,
|
||||
inc_order_cfg: bool = False,
|
||||
inc_order_line_list: bool = False,
|
||||
inc_person: bool = False,
|
||||
inc_user: bool = False,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
limit: int = 1000,
|
||||
limit: int = 500,
|
||||
offset: int = 0,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
model_as_dict: bool = False,
|
||||
@@ -204,6 +207,20 @@ def load_order_obj(
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
|
||||
# Updated 2022-01-18
|
||||
if inc_order_cfg:
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
if order_cfg_result := load_order_cfg_obj(
|
||||
account_id = order_rec.get('account_id'),
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
model_as_dict = True,
|
||||
):
|
||||
order_obj.cfg = order_cfg_result
|
||||
else: order_obj.cfg = {} # None
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(order_cfg_result)
|
||||
|
||||
# Updated 2021-06-18
|
||||
if inc_order_line_list:
|
||||
if order_line_rec_list_result := get_order_line_rec_list(
|
||||
@@ -215,7 +232,7 @@ def load_order_obj(
|
||||
for order_line_rec in order_line_rec_list_result:
|
||||
order_line_result_list.append(
|
||||
load_order_line_obj(
|
||||
order_line_id = order_line_rec.get('order_line_id', None),
|
||||
order_line_id = order_line_rec.get('order_line_id'),
|
||||
limit = limit,
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
@@ -223,52 +240,43 @@ def load_order_obj(
|
||||
)
|
||||
)
|
||||
order_obj.order_line_list = order_line_result_list
|
||||
else: order_obj.order_line_list = []
|
||||
else: order_obj.order_line_list = [] # None
|
||||
|
||||
# Updated 2021-08-07
|
||||
if inc_order_cfg:
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
if order_cfg_result := load_order_cfg_obj(
|
||||
account_id = order_rec.get('account_id', None),
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
model_as_dict = True,
|
||||
):
|
||||
order_obj.cfg = order_cfg_result
|
||||
else: order_obj.cfg = None
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(order_cfg_result)
|
||||
|
||||
# Updated 2021-06-22
|
||||
# Updated 2022-01-18
|
||||
if inc_person:
|
||||
from app.methods.person_methods import load_person_obj
|
||||
if person_result := load_person_obj(
|
||||
person_id = order_rec.get('person_id', None),
|
||||
person_id = order_rec.get('person_id'),
|
||||
inc_address = inc_address,
|
||||
inc_contact = inc_contact,
|
||||
inc_user = inc_user, # NOTE:
|
||||
enabled = enabled,
|
||||
limit = limit,
|
||||
offset = offset,
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
model_as_dict = model_as_dict,
|
||||
enabled = enabled,
|
||||
):
|
||||
order_obj.person = person_result
|
||||
else: order_obj.person = None
|
||||
else: order_obj.person = {} # None
|
||||
pass
|
||||
|
||||
# Updated 2021-06-22
|
||||
if inc_user:
|
||||
log.warning(f'This is being deprecated? load_order_obj() inc_user')
|
||||
from app.methods.user_methods import load_user_obj
|
||||
if user_result := load_user_obj(
|
||||
user_id = order_rec.get('user_id', None),
|
||||
limit = limit,
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
model_as_dict = model_as_dict,
|
||||
enabled = enabled,
|
||||
):
|
||||
order_obj.user = user_result
|
||||
else: order_obj.user = None
|
||||
pass
|
||||
# NOTE: Phasing out! Use *inc_user* under load_person_obj() instead.
|
||||
# if inc_user:
|
||||
# log.warning(f'This is being deprecated? load_order_obj() inc_user')
|
||||
# from app.methods.user_methods import load_user_obj
|
||||
# if user_result := load_user_obj(
|
||||
# user_id = order_rec.get('user_id', None),
|
||||
# limit = limit,
|
||||
# by_alias = by_alias,
|
||||
# exclude_unset = exclude_unset,
|
||||
# model_as_dict = model_as_dict,
|
||||
# enabled = enabled,
|
||||
# ):
|
||||
# order_obj.user = user_result
|
||||
# else: order_obj.user = None
|
||||
# pass
|
||||
|
||||
if model_as_dict:
|
||||
return order_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
||||
@@ -287,18 +295,24 @@ def get_order_rec_list(
|
||||
# balance_gt: int = 0, # $0 to $99999
|
||||
status: str = 'closed', # started, in progress, complete, all
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
limit: int = 1000,
|
||||
limit: int = 500,
|
||||
offset: int = 0,
|
||||
) -> list|bool:
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
||||
else: return False
|
||||
# if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
||||
# else: return False
|
||||
|
||||
data = {}
|
||||
data[f'{for_obj_type}_id'] = for_obj_id
|
||||
# data['for_obj_type'] = for_obj_type
|
||||
sql_obj_type_id = f'`order`.{for_obj_type}_id = :{for_obj_type}_id'
|
||||
if for_obj_type == 'account' and for_obj_id:
|
||||
data['account_id'] = redis_lookup_id_random(record_id_random=for_obj_id, table_name='account')
|
||||
sql_account_person_user = f"""`order`.account_id = :account_id"""
|
||||
elif for_obj_type == 'person' and for_obj_id:
|
||||
data['person_id'] = redis_lookup_id_random(record_id_random=for_obj_id, table_name='person')
|
||||
sql_account_person_user = f"""`order`.person_id = :person_id"""
|
||||
else:
|
||||
return False
|
||||
|
||||
# allowed_status_li = ['started', 'in progress', 'complete', 'all'] # OLD list
|
||||
# allowed_status_li = ['open', 'locked', 'reopened', 'closed', 'canceled', 'other'] # NEW list
|
||||
@@ -333,22 +347,8 @@ def get_order_rec_list(
|
||||
else:
|
||||
sql_from_to_datetime = ''
|
||||
|
||||
# if enabled in ['enabled', 'disabled', 'all']:
|
||||
# if enabled == 'enabled':
|
||||
# data['enable'] = True
|
||||
# sql_enabled = f'AND `order`.enable = :enable'
|
||||
# elif enabled == 'disabled':
|
||||
# data['enable'] = False
|
||||
# sql_enabled = f'AND `order`.enable = :enable'
|
||||
# elif enabled == 'all':
|
||||
# sql_enabled = ''
|
||||
sql_enabled = ''
|
||||
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
sql_limit = f'LIMIT :limit'
|
||||
else:
|
||||
sql_limit = ''
|
||||
sql_enabled, data['enable'] = sql_enable_part(table_name='contact', enabled=enabled) # Reasonably safe return str and bool
|
||||
sql_limit = sql_limit_offset_part(limit=limit, offset=offset) # Reasonably safe return str
|
||||
|
||||
log.debug(data)
|
||||
|
||||
|
||||
299
app/models/order_line_models_v3.py
Normal file
299
app/models/order_line_models_v3.py
Normal file
@@ -0,0 +1,299 @@
|
||||
from __future__ import annotations
|
||||
import datetime, hashlib, logging, os, pytz, redis, secrets
|
||||
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator
|
||||
|
||||
from app.db_sql import redis_lookup_id_random, get_id_random
|
||||
from app.lib_general import log, logging
|
||||
|
||||
from .common_field_schema import base_fields, default_num_bytes
|
||||
|
||||
|
||||
class Order_Line_Base(BaseModel):
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
id_random: Optional[str] = Field(
|
||||
**base_fields['order_line_id_random'],
|
||||
alias = 'order_line_id_random',
|
||||
default_factory = lambda:secrets.token_urlsafe(default_num_bytes),
|
||||
)
|
||||
id: Optional[int] = Field(
|
||||
alias = 'order_line_id'
|
||||
)
|
||||
|
||||
order_id_random: Optional[str]
|
||||
order_id: Optional[int]
|
||||
|
||||
product_id_random: str
|
||||
product_id: Optional[int]
|
||||
|
||||
account_id_random: Optional[str]
|
||||
account_id: Optional[int]
|
||||
|
||||
account_name: Optional[str]
|
||||
|
||||
product_for_type: Optional[str] # Copied from product record
|
||||
product_for_id_random: Optional[str] # Copied from product record NOPE
|
||||
product_for_id: Optional[int] # Copied from product record
|
||||
|
||||
product_type_id: Optional[int] # Copied from product record
|
||||
product_type: Optional[str] # WARNING: Copied from product record; dup from look up? probably not use?
|
||||
product_type_code: Optional[str] # Copied from product record; from look up
|
||||
product_type_name: Optional[str] # Copied from product record; from look up
|
||||
|
||||
product_name: Optional[str] # Copied from product record
|
||||
product_description: Optional[str] # Copied from product record
|
||||
product_unit_price: Optional[int] # Copied from product record
|
||||
product_recurring: Optional[bool] # Copied from product record
|
||||
|
||||
curr_product_id_random: str # Should be the same as product_id_random above
|
||||
curr_product_id: Optional[int] # Should be the same as product_id above
|
||||
|
||||
# NOTE: This is reversed with for_id_random
|
||||
curr_product_for_type: Optional[str] # Dynamic from v_order_line
|
||||
curr_product_for_id: Optional[int] # Dynamic from v_order_line
|
||||
curr_product_for_id_random: Optional[str] # Dynamic from v_order_line NOPE
|
||||
|
||||
curr_product_type_id: Optional[int] # Dynamic from v_order_line
|
||||
curr_product_type: Optional[str] # Dynamic from v_order_line
|
||||
curr_product_type_code: Optional[str] # Dynamic from v_order_line
|
||||
curr_product_type_name: Optional[str] # Dynamic from v_order_line
|
||||
|
||||
curr_product_name: Optional[str] # Dynamic from v_order_line
|
||||
curr_product_description: Optional[str] # Dynamic from v_order_line
|
||||
curr_product_unit_price: Optional[int] # Dynamic from v_order_line
|
||||
curr_product_max_quantity: Optional[int] # Dynamic from v_order_line
|
||||
curr_product_recurring: Optional[bool] # Dynamic from v_order_line
|
||||
|
||||
for_person_id: Optional[int]
|
||||
for_person_id_random: Optional[str]
|
||||
for_person_given_name: Optional[str] # Dynamic from v_order_line
|
||||
for_person_family_name: Optional[str] # Dynamic from v_order_line
|
||||
for_person_display_name: Optional[str] # Dynamic from v_order_line
|
||||
for_person_full_name: Optional[str] # Dynamic from v_order_line
|
||||
|
||||
name: Optional[str] # Should be the same as product_name above
|
||||
quantity: int = Field(0, ge=0, lt=150)
|
||||
amount: int = Field(0, ge=0, lt=1500000)
|
||||
dollar_amount: Optional[str]
|
||||
recurring: Optional[bool] = False
|
||||
message: Optional[str]
|
||||
|
||||
notes: Optional[str]
|
||||
|
||||
created_on: Optional[datetime.datetime] = None
|
||||
updated_on: Optional[datetime.datetime] = None
|
||||
|
||||
# Including convenience data
|
||||
# This is only for convenience. Probably going to keep unless it causes a problem.
|
||||
order_status: Optional[str]
|
||||
order_notes: Optional[str]
|
||||
order_created_on: Optional[datetime.datetime] = None
|
||||
order_updated_on: Optional[datetime.datetime] = None
|
||||
|
||||
# Including other related objects
|
||||
# product: Optional[Union[Product_Base, None]] # Future use?
|
||||
# for_person: Optional[Union[Person_Base, None]] # Future use?
|
||||
|
||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||
|
||||
#@validator('order_line_id_random', always=True)
|
||||
def order_line_id_random_copy(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['id_random']:
|
||||
return values['id_random']
|
||||
return None
|
||||
|
||||
@validator('id', always=True)
|
||||
def order_line_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['id_random']:
|
||||
log.debug(values['id_random'])
|
||||
return redis_lookup_id_random(record_id_random=values['id_random'], table_name='order_line')
|
||||
return None
|
||||
|
||||
@validator('order_id', always=True)
|
||||
def order_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['order_id_random']:
|
||||
return redis_lookup_id_random(record_id_random=values['order_id_random'], table_name='order')
|
||||
return None
|
||||
|
||||
@validator('product_id', always=True)
|
||||
def product_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values.get('product_id_random', None):
|
||||
return redis_lookup_id_random(record_id_random=values['product_id_random'], table_name='product')
|
||||
return None
|
||||
|
||||
@validator('product_for_id', always=True)
|
||||
def product_for_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['product_for_id_random'] and values['product_for_type']:
|
||||
return redis_lookup_id_random(record_id_random=values['product_for_id_random'], table_name=values['product_for_type'])
|
||||
return None
|
||||
|
||||
# @validator('curr_product_for_id_random', always=True)
|
||||
# def curr_product_for_id_random_lookup(cls, v, values, **kwargs):
|
||||
# log.setLevel(logging.DEBUG)
|
||||
# log.debug(locals())
|
||||
|
||||
# if values['curr_product_for_id'] and values['curr_product_for_type']:
|
||||
# return redis_lookup_id(record_id=values['curr_product_for_id'], table_name=values['curr_product_for_type'])
|
||||
# return None
|
||||
|
||||
@validator('curr_product_for_id_random', always=True)
|
||||
def curr_product_for_id_random_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.DEBUG)
|
||||
log.debug(locals())
|
||||
|
||||
if values['curr_product_for_id'] and values['curr_product_for_type']:
|
||||
return get_id_random(record_id=values['curr_product_for_id'], table_name=values['curr_product_for_type'])
|
||||
return None
|
||||
|
||||
# @validator('curr_product_for_id', always=True)
|
||||
# def curr_product_for_id_lookup(cls, v, values, **kwargs):
|
||||
# log.setLevel(logging.WARNING)
|
||||
# log.debug(locals())
|
||||
|
||||
# if values['curr_product_for_id_random'] and values['curr_product_for_type']:
|
||||
# return redis_lookup_id_random(record_id_random=values['curr_product_for_id_random'], table_name=values['curr_product_for_type'])
|
||||
# return None
|
||||
|
||||
@validator('account_id', always=True)
|
||||
def account_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values.get('account_id_random', None):
|
||||
return redis_lookup_id_random(record_id_random=values['account_id_random'], table_name='account')
|
||||
return None
|
||||
|
||||
class Config:
|
||||
underscore_attrs_are_private = True
|
||||
allow_population_by_field_name = True
|
||||
fields = base_fields
|
||||
|
||||
|
||||
class Order_Line_DB_Base(BaseModel):
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
id_random: Optional[str] = Field(
|
||||
alias = 'order_line_id_random',
|
||||
)
|
||||
id: Optional[int] = Field(
|
||||
alias = 'order_line_id'
|
||||
)
|
||||
|
||||
order_id_random: Optional[str]
|
||||
order_id: Optional[int]
|
||||
|
||||
product_id_random: str
|
||||
product_id: Optional[int]
|
||||
|
||||
product_for_type: Optional[str] # Copied from product record
|
||||
product_for_id_random: Optional[str] # Copied from product record NOPE
|
||||
product_for_id: Optional[int] # Copied from product record
|
||||
|
||||
product_type_id: Optional[int] # Copied from product record
|
||||
|
||||
product_name: Optional[str] # Copied from product record
|
||||
product_unit_price: Optional[int] # Copied from product record
|
||||
product_recurring: Optional[bool] # Copied from product record
|
||||
|
||||
for_person_id: Optional[int]
|
||||
for_person_id_random: Optional[str]
|
||||
|
||||
name: Optional[str] # Should be the same as product_name above
|
||||
quantity: int = Field(0, ge=0, lt=150)
|
||||
amount: int = Field(0, ge=0, lt=1500000)
|
||||
recurring: Optional[bool] = False
|
||||
recurring_period: Optional[int]
|
||||
|
||||
message: Optional[str]
|
||||
|
||||
notes: Optional[str]
|
||||
|
||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||
|
||||
class Config:
|
||||
underscore_attrs_are_private = True
|
||||
allow_population_by_field_name = True
|
||||
|
||||
|
||||
class Order_Line_Full_Detail_Base(Order_Line_Base):
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
person_id: Optional[int]
|
||||
person_id_random: Optional[str]
|
||||
person_given_name: Optional[str]
|
||||
person_family_name: Optional[str]
|
||||
person_display_name: Optional[str]
|
||||
person_full_name: Optional[str]
|
||||
|
||||
person_contact_id: Optional[int]
|
||||
person_contact_id_random: Optional[str]
|
||||
|
||||
person_contact_for_type: Optional[str]
|
||||
person_contact_for_id: Optional[int]
|
||||
|
||||
person_contact_name: Optional[str]
|
||||
person_contact_email: Optional[str]
|
||||
person_contact_cc_email: Optional[str]
|
||||
|
||||
person_contact_phone_mobile: Optional[str]
|
||||
person_contact_phone_home: Optional[str]
|
||||
person_contact_phone_office: Optional[str]
|
||||
person_contact_phone_land: Optional[str]
|
||||
person_contact_phone_fax: Optional[str]
|
||||
person_contact_phone_other: Optional[str]
|
||||
|
||||
person_contact_address_id: Optional[int]
|
||||
person_contact_address_id_random: Optional[str]
|
||||
|
||||
person_contact_address_for_type: Optional[str]
|
||||
person_contact_address_for_id: Optional[int]
|
||||
|
||||
person_contact_address_name: Optional[str]
|
||||
person_contact_address_organization_name: Optional[str]
|
||||
person_contact_address_line_1: Optional[str]
|
||||
person_contact_address_line_2: Optional[str]
|
||||
person_contact_address_line_3: Optional[str]
|
||||
person_contact_address_city: Optional[str]
|
||||
person_contact_address_country_subdivision_code: Optional[str]
|
||||
person_contact_address_country_subdivision_name: Optional[str] # From country subdivision lookup table
|
||||
person_contact_address_state_province: Optional[str] # Avoid using
|
||||
person_contact_address_postal_code: Optional[str]
|
||||
person_contact_address_country_alpha_2_code: Optional[str]
|
||||
person_contact_address_country_name: Optional[str] # From country lookup table
|
||||
person_contact_address_country: Optional[str] # Avoid using
|
||||
|
||||
person_contact_address_lu_time_zone_id: Optional[str]
|
||||
|
||||
# Including convenience data
|
||||
# This is only for convenience. Probably going to keep unless it causes a problem.
|
||||
person_email: Optional[str]
|
||||
person_cc_email: Optional[str]
|
||||
# Maybe add timezone in the future?
|
||||
|
||||
class Config:
|
||||
underscore_attrs_are_private = True
|
||||
allow_population_by_field_name = True
|
||||
|
||||
|
||||
|
||||
Order_Line_Base.update_forward_refs()
|
||||
169
app/models/order_models_v3.py
Normal file
169
app/models/order_models_v3.py
Normal file
@@ -0,0 +1,169 @@
|
||||
from __future__ import annotations
|
||||
import datetime, hashlib, logging, os, pytz, redis, secrets
|
||||
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator
|
||||
|
||||
from app.db_sql import redis_lookup_id_random
|
||||
from app.lib_general import log, logging
|
||||
|
||||
from app.models.common_field_schema import base_fields, default_num_bytes
|
||||
from app.models.order_cfg_models import Order_Cfg_Base
|
||||
from app.models.order_line_models import Order_Line_Base
|
||||
from app.models.person_models import Person_Base
|
||||
|
||||
|
||||
class Order_Base(BaseModel):
|
||||
log.setLevel(logging.INFO)
|
||||
log.debug(locals())
|
||||
|
||||
id_random: Optional[str] = Field(
|
||||
**base_fields['order_id_random'],
|
||||
alias = 'order_id_random',
|
||||
)
|
||||
id: Optional[int] = Field(
|
||||
alias = 'order_id'
|
||||
)
|
||||
|
||||
account_id_random: Optional[str]
|
||||
account_id: Optional[int] # NOTE: This is not really optional
|
||||
|
||||
person_id_random: Optional[str]
|
||||
person_id: Optional[int]
|
||||
|
||||
total_quantity: Optional[int] = Field(0, ge=0, lt=150)
|
||||
total_bill: Optional[int] = Field(0, ge=0, lt=1500000) # NOTE: This is total_amount in the order_cart
|
||||
total_paid: Optional[int] = Field(0, ge=0, lt=1500000)
|
||||
balance: Optional[int] = Field(0, ge=-1500000, lt=1500000) # Balance needs to be calculated
|
||||
status: Optional[str] # open, locked, reopened, closed, canceled, other
|
||||
# open = building or creating cart, payment failed and no partial payment already processed
|
||||
# locked = processing payment, partial payment processed, admin lock? (no changes allowed)
|
||||
# closed = successful payment(s) with no balance, complete (no changes allowed)
|
||||
# canceled = order canceled; something odd happened (no changes allowed)
|
||||
# other = other reasons (no changes allowed)
|
||||
checkout_status: Optional[str] # canceled, waiting, success, failed, unknown
|
||||
# none = no checkout attempted
|
||||
# canceled = checkout attempted but canceled for some reason
|
||||
# waiting = the payment is processing or awaiting some type of authorization
|
||||
# success = payment successful (their may still be a balance)
|
||||
# failed = the payment failed for some reason
|
||||
# unknown = unknown response...
|
||||
|
||||
notes: Optional[str]
|
||||
|
||||
created_on: Optional[datetime.datetime] = None
|
||||
updated_on: Optional[datetime.datetime] = None
|
||||
|
||||
# Including other related objects
|
||||
cfg: Optional[Order_Cfg_Base]
|
||||
order_line_list: Optional[list[Order_Line_Base]]
|
||||
person: Optional[Person_Base]
|
||||
|
||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||
|
||||
@validator('id', always=True)
|
||||
def order_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if id_random := values.get('id_random'):
|
||||
return redis_lookup_id_random(record_id_random=id_random, table_name='order')
|
||||
elif v:
|
||||
return get_id_random(v, table_name='order')
|
||||
return None
|
||||
|
||||
@validator('account_id', always=True)
|
||||
def account_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if id_random := values.get('id_random'):
|
||||
return redis_lookup_id_random(record_id_random=id_random, table_name='account')
|
||||
elif v:
|
||||
return get_id_random(v, table_name='account')
|
||||
return None
|
||||
|
||||
|
||||
@validator('person_id', always=True)
|
||||
def person_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if id_random := values.get('id_random'):
|
||||
return redis_lookup_id_random(record_id_random=id_random, table_name='person')
|
||||
elif v:
|
||||
return get_id_random(v, table_name='person')
|
||||
return None
|
||||
|
||||
class Config:
|
||||
underscore_attrs_are_private = True
|
||||
allow_population_by_field_name = True
|
||||
fields = base_fields
|
||||
|
||||
|
||||
class Order_DB_Base(BaseModel):
|
||||
log.setLevel(logging.INFO)
|
||||
log.debug(locals())
|
||||
|
||||
account_id_random: Optional[str]
|
||||
account_id: Optional[int] # NOTE: This is not really optional
|
||||
|
||||
person_id_random: Union[str, None] = None
|
||||
person_id: Union[int, None] = None
|
||||
|
||||
user_id_random: Union[str, None] = None
|
||||
user_id: Union[int, None] = None
|
||||
|
||||
total_quantity: Optional[int] = Field(0, ge=0, lt=150)
|
||||
total_bill: Optional[int] = Field(0, ge=0, lt=1500000) # NOTE: This is total_amount in the order_cart
|
||||
total_paid: Optional[int] = Field(0, ge=0, lt=1500000)
|
||||
balance: Optional[int] = Field(0, ge=-1500000, lt=1500000) # Balance needs to be calculated
|
||||
status: Optional[str] = 'open' # open, locked, reopened, closed, canceled, other
|
||||
# open = building or creating cart, payment failed and no partial payment already processed
|
||||
# locked = processing payment, partial payment processed, admin lock? (no changes allowed)
|
||||
# closed = successful payment(s) with no balance, complete (no changes allowed)
|
||||
# canceled = order canceled; something odd happened (no changes allowed)
|
||||
# other = other reasons (no changes allowed)
|
||||
checkout_status: Union[str, None] = None # canceled, waiting, success, failed, unknown
|
||||
# none = no checkout attempted
|
||||
# canceled = checkout attempted but canceled for some reason
|
||||
# waiting = the payment is processing or awaiting some type of authorization
|
||||
# success = payment successful (their may still be a balance)
|
||||
# failed = the payment failed for some reason
|
||||
# unknown = unknown response...
|
||||
|
||||
notes: Optional[str]
|
||||
|
||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||
|
||||
@validator('account_id', always=True)
|
||||
def account_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['account_id_random']:
|
||||
return redis_lookup_id_random(record_id_random=values['account_id_random'], table_name='account')
|
||||
return None
|
||||
|
||||
@validator('person_id', always=True)
|
||||
def person_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['person_id_random']:
|
||||
return redis_lookup_id_random(record_id_random=values['person_id_random'], table_name='person')
|
||||
return None
|
||||
|
||||
@validator('user_id', always=True)
|
||||
def user_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['user_id_random']:
|
||||
return redis_lookup_id_random(record_id_random=values['user_id_random'], table_name='user')
|
||||
return None
|
||||
|
||||
class Config:
|
||||
underscore_attrs_are_private = True
|
||||
allow_population_by_field_name = True
|
||||
fields = base_fields
|
||||
@@ -353,11 +353,22 @@ async def get_account_cfg_obj(
|
||||
|
||||
if sys_module:
|
||||
if sys_module == 'membership':
|
||||
membership_cfg_obj = load_account_cfg_obj(account_id=account_id, inc_membership_cfg=True).dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
||||
membership_cfg_obj = load_account_cfg_obj(
|
||||
account_id = account_id,
|
||||
inc_membership_cfg = True
|
||||
).dict(
|
||||
by_alias = commons.by_alias,
|
||||
exclude_unset = commons.exclude_unset
|
||||
)
|
||||
data = membership_cfg_obj
|
||||
return mk_resp(data=data, response=commons.response)
|
||||
else:
|
||||
if account_cfg_obj := load_account_cfg_obj(account_id=account_id).dict(by_alias=by_alias, exclude_unset=exclude_unset):
|
||||
if account_cfg_obj := load_account_cfg_obj(
|
||||
account_id = account_id
|
||||
).dict(
|
||||
by_alias = commons.by_alias,
|
||||
exclude_unset = commons.exclude_unset
|
||||
):
|
||||
data = account_cfg_obj
|
||||
return mk_resp(data=data, response=commons.response)
|
||||
else:
|
||||
|
||||
630
app/routers/order_v3.py
Normal file
630
app/routers/order_v3.py
Normal file
@@ -0,0 +1,630 @@
|
||||
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_methods import get_order_rec_list, load_order_obj, save_order_obj
|
||||
|
||||
from app.models.response_models import Resp_Body_Base, mk_resp
|
||||
from app.models.order_models_v3 import Order_Base
|
||||
from app.models.order_line_models_v3 import Order_Line_Base
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Routers ### post_order_obj() ###
|
||||
# Updated 2022-01-18
|
||||
@router.post('/v3/order', response_model=Resp_Body_Base)
|
||||
@router.post('/v3/person/{person_id}/order', response_model=Resp_Body_Base)
|
||||
async def post_order_obj(
|
||||
order_obj: Order_Base,
|
||||
person_id: str = Query(..., min_length=11, max_length=22),
|
||||
|
||||
inc_address: bool = False,
|
||||
inc_contact: bool = False,
|
||||
inc_person: bool = False,
|
||||
inc_order_line_list: bool = True,
|
||||
return_obj: bool = True,
|
||||
|
||||
commons: Common_Route_Params = Depends(common_route_params),
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# ### SECTION ### Secondary data validation
|
||||
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
|
||||
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The person ID was invalid or not found.')
|
||||
|
||||
# ### SECTION ### Process data
|
||||
if order_id := create_order(
|
||||
account_id = commons.x_account_id,
|
||||
person_id = person_id,
|
||||
order_dict_obj = order_obj,
|
||||
): pass
|
||||
else:
|
||||
log.warning('Likely bad request')
|
||||
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
||||
|
||||
# ### SECTION ### Return successful results
|
||||
if return_obj:
|
||||
order_obj = load_order_obj(
|
||||
order_id = order_id,
|
||||
inc_address = inc_address,
|
||||
inc_contact = inc_contact,
|
||||
inc_person = inc_person,
|
||||
).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
|
||||
data = order_obj
|
||||
else:
|
||||
order_id_random = get_id_random(record_id=order_id, table_name='order')
|
||||
data = {}
|
||||
data['order_id'] = order_id
|
||||
data['order_id_random'] = order_id_random
|
||||
return mk_resp(data=data, response=commons.response)
|
||||
# ### END ### API Order Routers ### post_order_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Routers ### patch_order_obj() ###
|
||||
# Updated 2022-01-18
|
||||
@router.patch('/v3/order/{order_id}', response_model=Resp_Body_Base)
|
||||
# @router.patch('/v3/person/{person_id}/order/{order_id}', response_model=Resp_Body_Base)
|
||||
async def patch_order_obj(
|
||||
order_obj: Order_Base,
|
||||
order_id: str = Query(..., min_length=11, max_length=22),
|
||||
person_id: str = Query(None, min_length=11, max_length=22),
|
||||
|
||||
inc_address: bool = False,
|
||||
inc_contact: bool = False,
|
||||
inc_person: bool = False,
|
||||
inc_order_line_list: bool = True,
|
||||
return_obj: Optional[bool] = True,
|
||||
|
||||
commons: Common_Route_Params = Depends(common_route_params),
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# ### SECTION ### Secondary data validation
|
||||
order_id_random = order_id # This is used later for the response data
|
||||
# person_id_random = person_id # This is used later for the response data
|
||||
|
||||
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
||||
elif order_id is None: pass
|
||||
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order ID was invalid or not found.')
|
||||
|
||||
# if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
|
||||
# else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The person ID was invalid or not found.')
|
||||
|
||||
# ### SECTION ### Process data
|
||||
if order_update_result := update_order(
|
||||
order_id = order_id,
|
||||
order_dict_obj = order_obj,
|
||||
# person_id = person_id,
|
||||
): pass
|
||||
else:
|
||||
log.warning('Likely bad request')
|
||||
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
||||
|
||||
# ### SECTION ### Return successful results
|
||||
if return_obj:
|
||||
order_obj = load_order_obj(
|
||||
order_id = order_id,
|
||||
inc_address = inc_address,
|
||||
inc_contact = inc_contact,
|
||||
inc_order_line_list = inc_order_line_list,
|
||||
inc_person = inc_person,
|
||||
).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
|
||||
data = order_obj
|
||||
else:
|
||||
data = {}
|
||||
data['order_id'] = order_id
|
||||
data['order_id_random'] = order_id_random
|
||||
return mk_resp(data=data, response=commons.response)
|
||||
# ### END ### API Order Routers ### patch_order_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Routers ### delete_order_obj() ###
|
||||
# Updated 2022-01-18
|
||||
@router.delete('/order/{order_id}', response_model=Resp_Body_Base)
|
||||
async def delete_order_obj(
|
||||
order_id: str = Query(..., min_length=11, max_length=22),
|
||||
commons: Common_Route_Params = Depends(common_route_params),
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
obj_type = 'order'
|
||||
result = delete_obj_template(
|
||||
obj_type = obj_type,
|
||||
obj_id = obj_id,
|
||||
)
|
||||
return result
|
||||
# ### END ### API Order Routers ### delete_order_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Routers ### patch_order_obj_add_line() ###
|
||||
# Updated 2022-01-18
|
||||
@router.patch('/v3/order/{order_id}/line/add', response_model=Resp_Body_Base)
|
||||
async def patch_order_obj_add_line(
|
||||
order_obj: Order_Base,
|
||||
order_id: str = Query(..., min_length=11, max_length=22),
|
||||
|
||||
# inc_order: bool = False,
|
||||
inc_order_line_list: bool = True,
|
||||
return_obj: Optional[bool] = True,
|
||||
|
||||
commons: Common_Route_Params = Depends(common_route_params),
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# ### SECTION ### Secondary data validation
|
||||
order_id_random = order_id # This is used later for the response data
|
||||
|
||||
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
||||
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order ID was invalid or not found.')
|
||||
|
||||
# ### SECTION ### Process data
|
||||
if order_line_add_result := add_order_line(
|
||||
order_id = order_id,
|
||||
order_line_dict_obj = order_line_obj,
|
||||
): pass
|
||||
else:
|
||||
log.warning('Likely bad request')
|
||||
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
||||
|
||||
# ### SECTION ### Return successful results
|
||||
if return_obj:
|
||||
order_obj = load_order_obj(
|
||||
order_id = order_id,
|
||||
inc_order_line_list = inc_order_line_list,
|
||||
).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
|
||||
data = order_obj
|
||||
else:
|
||||
data = {}
|
||||
data['order_id'] = order_id
|
||||
data['order_id_random'] = order_id_random
|
||||
return mk_resp(data=data, response=commons.response)
|
||||
# ### END ### API Order Routers ### patch_order_obj_add_line() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Routers ### patch_order_obj_update_line() ###
|
||||
# Updated 2022-01-18
|
||||
@router.patch('/v3/order/{order_id}/line/{order_line_id}/update', response_model=Resp_Body_Base)
|
||||
async def patch_order_obj_update_line(
|
||||
order_obj: Order_Line_Base,
|
||||
order_id: str = Query(..., min_length=11, max_length=22),
|
||||
order_line_id: str = Query(..., min_length=11, max_length=22),
|
||||
|
||||
# inc_order: bool = False,
|
||||
inc_order_line_list: bool = True,
|
||||
return_obj: Optional[bool] = True,
|
||||
|
||||
commons: Common_Route_Params = Depends(common_route_params),
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# ### SECTION ### Secondary data validation
|
||||
order_id_random = order_id # This is used later for the response data
|
||||
order_line_id_random = order_line_id # This is used later for the response data
|
||||
|
||||
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
||||
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order ID was invalid or not found.')
|
||||
|
||||
if order_line_id := redis_lookup_id_random(record_id_random=order_line_id, table_name='order_line'): pass
|
||||
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order line ID was invalid or not found.')
|
||||
|
||||
# ### SECTION ### Process data
|
||||
if order_line_update_result := update_order_line(
|
||||
order_line_id = order_line_id,
|
||||
order_line_dict_obj = order_line_obj,
|
||||
): pass
|
||||
else:
|
||||
log.warning('Likely bad request')
|
||||
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
||||
|
||||
# ### SECTION ### Return successful results
|
||||
if return_obj:
|
||||
order_obj = load_order_obj(
|
||||
order_id = order_id,
|
||||
inc_order_line_list = inc_order_line_list,
|
||||
).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
|
||||
data = order_obj
|
||||
else:
|
||||
data = {}
|
||||
data['order_id'] = order_id
|
||||
data['order_id_random'] = order_id_random
|
||||
return mk_resp(data=data, response=commons.response)
|
||||
# ### END ### API Order Routers ### patch_order_obj_update_line() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Routers ### patch_order_obj_remove_line() ###
|
||||
# Updated 2022-01-18
|
||||
@router.patch('/v3/order/{order_id}/line/{order_line_id}/remove', response_model=Resp_Body_Base)
|
||||
async def patch_order_obj_remove_line(
|
||||
order_obj: Order_Line_Base,
|
||||
order_id: str = Query(..., min_length=11, max_length=22),
|
||||
order_line_id: str = Query(..., min_length=11, max_length=22),
|
||||
|
||||
# inc_order: bool = False,
|
||||
inc_order_line_list: bool = True,
|
||||
return_obj: Optional[bool] = True,
|
||||
|
||||
commons: Common_Route_Params = Depends(common_route_params),
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# ### SECTION ### Secondary data validation
|
||||
order_id_random = order_id # This is used later for the response data
|
||||
order_line_id_random = order_line_id # This is used later for the response data
|
||||
|
||||
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
||||
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order ID was invalid or not found.')
|
||||
|
||||
if order_line_id := redis_lookup_id_random(record_id_random=order_line_id, table_name='order_line'): pass
|
||||
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order line ID was invalid or not found.')
|
||||
|
||||
# ### SECTION ### Process data
|
||||
if order_line_remove_result := remove_order_line(
|
||||
order_line_id = order_line_id,
|
||||
): pass
|
||||
else:
|
||||
log.warning('Likely bad request')
|
||||
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
||||
|
||||
# ### SECTION ### Return successful results
|
||||
if return_obj:
|
||||
order_obj = load_order_obj(
|
||||
order_id = order_id,
|
||||
inc_order_line_list = inc_order_line_list,
|
||||
).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset)
|
||||
data = order_obj
|
||||
else:
|
||||
data = {}
|
||||
data['order_id'] = order_id
|
||||
data['order_id_random'] = order_id_random
|
||||
return mk_resp(data=data, response=commons.response)
|
||||
# ### END ### API Order Routers ### patch_order_obj_remove_line() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Routers ### get_order_obj_li() ###
|
||||
# Updated 2022-01-18
|
||||
@router.get('/v3/{for_obj_type}/{for_obj_id}/order/list', response_model=Resp_Body_Base)
|
||||
async def get_order_obj_li(
|
||||
for_obj_type: str = Query(..., min_length=2, max_length=50),
|
||||
for_obj_id: str = Query(..., min_length=11, max_length=22),
|
||||
order_status: str = 'complete',
|
||||
from_datetime: datetime.datetime = None,
|
||||
to_datetime: datetime.datetime = None,
|
||||
inc_order_line_list: bool = False,
|
||||
inc_order_cfg: bool = False,
|
||||
inc_person_obj: bool = False,
|
||||
|
||||
commons: Common_Route_Params = Depends(common_route_params),
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if obj_type_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
||||
else: return mk_resp(data=False, status_code=404, response=commons.response) # Not Found
|
||||
|
||||
if order_rec_list_result := get_order_rec_list(
|
||||
for_obj_type = for_obj_type,
|
||||
for_obj_id = for_obj_id,
|
||||
from_datetime = from_datetime,
|
||||
to_datetime = to_datetime,
|
||||
status = status,
|
||||
enabled = commons.enabled,
|
||||
limit = commons.limit,
|
||||
offset = commons.offset,
|
||||
):
|
||||
order_result_list = []
|
||||
for order_rec in order_rec_list_result:
|
||||
if load_order_result := load_order_obj(
|
||||
order_id = order_rec.get('order_id', None),
|
||||
inc_address = inc_address,
|
||||
inc_contact = inc_contact,
|
||||
inc_order_line_list = inc_order_line_list,
|
||||
inc_person = inc_person,
|
||||
enabled = commons.enabled,
|
||||
limit = commons.limit,
|
||||
by_alias = commons.by_alias,
|
||||
exclude_unset = commons.exclude_unset,
|
||||
# model_as_dict = model_as_dict,
|
||||
):
|
||||
order_result_list.append(load_order_result)
|
||||
else:
|
||||
order_result_list.append(None)
|
||||
response_data = order_result_list
|
||||
elif isinstance(order_rec_list_result, list) or order_rec_list_result is None: # Empty list or None
|
||||
log.info('No results')
|
||||
return mk_resp(data=None, status_code=404, response=commons.response) # Not Found
|
||||
else:
|
||||
log.warning('Likely bad request')
|
||||
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
||||
|
||||
return mk_resp(data=response_data, response=commons.response)
|
||||
# ### END ### API Order Routers ### get_order_obj_li() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Routes ### get_order_obj() ###
|
||||
# NOTE 2021-08-09: Use with rework of order_cart
|
||||
# Updated 2021-08-07
|
||||
@router.get('/order/{order_id}', response_model=Resp_Body_Base)
|
||||
async def get_order_obj(
|
||||
order_id: str = Query(..., min_length=11, max_length=22),
|
||||
limit: int = 500,
|
||||
enabled: str = 'enabled',
|
||||
inc_order_line_list: bool = False,
|
||||
inc_order_cfg: bool = False,
|
||||
inc_person: bool = False,
|
||||
x_account_id: str = Header(...),
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
response: Response = Response,
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if order_obj := load_order_obj(
|
||||
order_id = order_id,
|
||||
limit = limit,
|
||||
enabled = enabled,
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
# model_as_dict = model_as_dict,
|
||||
inc_order_line_list = inc_order_line_list,
|
||||
inc_order_cfg = inc_order_cfg,
|
||||
inc_person = inc_person,
|
||||
):
|
||||
log.debug(order_obj)
|
||||
data = order_obj.dict(by_alias=by_alias, exclude_unset=False) # NOTE NOTE NOTE NOTE exclude_unset is forced to False for now. Will return more fields than is ideal. Need to create another Order_Line_Base. Probably Order_Line_OUT_Base
|
||||
return mk_resp(data=data)
|
||||
else:
|
||||
return mk_resp(data=False, status_code=404, response=response) # Not Found
|
||||
# ### END ### API Order Routes ### get_order_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order ### get_obj_id_order_list() ###
|
||||
# Updated 2021-12-13
|
||||
@router.get('/{obj_type}/{obj_id}/order/list', response_model=Resp_Body_Base)
|
||||
async def get_obj_id_order_list(
|
||||
obj_type: str = Query(..., min_length=4, max_length=25), # Expects account or person
|
||||
obj_id: str = Query(..., min_length=11, max_length=22),
|
||||
from_datetime: datetime.datetime = None,
|
||||
to_datetime: datetime.datetime = None,
|
||||
inc_order_cfg: bool = False,
|
||||
inc_order_line_list: bool = False,
|
||||
# w_prod_type: str = 'all', # all, cont_edu_cert, event, fundraising, membership, etc
|
||||
inc_person: bool = False,
|
||||
status: str = 'closed', # open, locked, reopened?, closed (complete), canceled, other
|
||||
enabled: str = 'enabled',
|
||||
limit: int = 50,
|
||||
by_alias: Optional[bool] = True,
|
||||
exclude_unset: Optional[bool] = True,
|
||||
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', '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_rec_list_result := get_order_rec_list(
|
||||
for_obj_type = obj_type,
|
||||
for_obj_id = obj_id,
|
||||
from_datetime = from_datetime,
|
||||
to_datetime = to_datetime,
|
||||
status = status,
|
||||
enabled = enabled,
|
||||
limit = limit,
|
||||
):
|
||||
order_result_list = []
|
||||
for order_rec in order_rec_list_result:
|
||||
if load_order_result := load_order_obj(
|
||||
order_id = order_rec.get('order_id', None),
|
||||
inc_order_cfg = inc_order_cfg,
|
||||
inc_order_line_list = inc_order_line_list,
|
||||
inc_person = inc_person,
|
||||
enabled = enabled,
|
||||
limit = limit,
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
# model_as_dict = model_as_dict,
|
||||
):
|
||||
order_result_list.append(load_order_result)
|
||||
else:
|
||||
order_result_list.append(None)
|
||||
response_data = order_result_list
|
||||
elif isinstance(order_rec_list_result, list) or order_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
|
||||
|
||||
return mk_resp(data=response_data, response=response)
|
||||
# ### END ### API Order ### get_obj_id_order_list() ###
|
||||
|
||||
|
||||
# # ### BEGIN ### API Order ### get_account_obj_order_list() ###
|
||||
# # Updated 2021-11-19
|
||||
# @router.get('/account/{account_id}/order/list', response_model=Resp_Body_Base)
|
||||
# async def get_account_id_order_list(
|
||||
# account_id: str = Query(..., min_length=11, max_length=22),
|
||||
# limit: int = 50,
|
||||
# enabled: str = 'enabled',
|
||||
# from_datetime: datetime.datetime = None,
|
||||
# to_datetime: datetime.datetime = None,
|
||||
# inc_order_cfg: bool = False,
|
||||
# inc_order_line_list: bool = False,
|
||||
# # inc_order_w_prod_type: str = 'all', # all, membership, fundraising, event, etc
|
||||
# status: str = 'closed', # open, locked, reopened?, closed (complete), canceled, other
|
||||
# x_account_id: str = Header(...),
|
||||
# by_alias: Optional[bool] = True,
|
||||
# exclude_unset: Optional[bool] = True,
|
||||
# response: Response = Response,
|
||||
# ):
|
||||
# log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(locals())
|
||||
|
||||
# if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
||||
# else: return mk_resp(data=None, status_code=404, response=response)
|
||||
|
||||
# # Updated 2021-06-28
|
||||
# if order_rec_list_result := get_order_rec_list(
|
||||
# for_obj_type = 'account',
|
||||
# for_obj_id = account_id,
|
||||
# limit = limit,
|
||||
# enabled = enabled,
|
||||
# from_datetime = from_datetime,
|
||||
# to_datetime = to_datetime,
|
||||
# status = status,
|
||||
# ):
|
||||
# order_result_list = []
|
||||
# for order_rec in order_rec_list_result:
|
||||
# if load_order_result := load_order_obj(
|
||||
# order_id = order_rec.get('order_id', None),
|
||||
# limit = limit,
|
||||
# enabled = enabled,
|
||||
# by_alias = by_alias,
|
||||
# exclude_unset = exclude_unset,
|
||||
# # model_as_dict = model_as_dict,
|
||||
# inc_order_cfg = inc_order_cfg,
|
||||
# inc_order_line_list = inc_order_line_list,
|
||||
# ):
|
||||
# order_result_list.append(load_order_result)
|
||||
# else:
|
||||
# order_result_list.append(None)
|
||||
# response_data = order_result_list
|
||||
# else:
|
||||
# return mk_resp(data=False, status_code=400, response=response) # Bad Request
|
||||
|
||||
# return mk_resp(data=response_data)
|
||||
# # ### END ### API Order ### get_account_obj_order_list() ###
|
||||
|
||||
|
||||
# # ### BEGIN ### API Order ### get_person_obj_order_list() ###
|
||||
# # Updated 2021-11-19
|
||||
# @router.get('/person/{person_id}/order/list', response_model=Resp_Body_Base)
|
||||
# async def get_person_id_order_list(
|
||||
# person_id: str = Query(..., min_length=11, max_length=22),
|
||||
# limit: int = 50,
|
||||
# enabled: str = 'enabled',
|
||||
# from_datetime: datetime.datetime = None,
|
||||
# to_datetime: datetime.datetime = None,
|
||||
# inc_order_cfg: bool = False,
|
||||
# inc_order_line_list: bool = False,
|
||||
# # inc_order_w_prod_type: str = 'all', # all, membership, fundraising, event, etc
|
||||
# status: str = 'closed', # open, locked, reopened?, closed (complete), canceled, other
|
||||
# x_account_id: str = Header(...),
|
||||
# by_alias: Optional[bool] = True,
|
||||
# exclude_unset: Optional[bool] = True,
|
||||
# response: Response = Response,
|
||||
# ):
|
||||
# log.setLevel(logging.WARNING) # 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 mk_resp(data=None, status_code=404, response=response)
|
||||
|
||||
# # Updated 2021-06-28
|
||||
# if order_rec_list_result := get_order_rec_list(
|
||||
# for_obj_type = 'person',
|
||||
# for_obj_id = person_id,
|
||||
# limit = limit,
|
||||
# enabled = enabled,
|
||||
# from_datetime = from_datetime,
|
||||
# to_datetime = to_datetime,
|
||||
# status = status,
|
||||
# ):
|
||||
# order_result_list = []
|
||||
# for order_rec in order_rec_list_result:
|
||||
# if load_order_result := load_order_obj(
|
||||
# order_id = order_rec.get('order_id', None),
|
||||
# limit = limit,
|
||||
# enabled = enabled,
|
||||
# by_alias = by_alias,
|
||||
# exclude_unset = exclude_unset,
|
||||
# # model_as_dict = model_as_dict,
|
||||
# inc_order_cfg = inc_order_cfg,
|
||||
# inc_order_line_list = inc_order_line_list,
|
||||
# ):
|
||||
# order_result_list.append(load_order_result)
|
||||
# else:
|
||||
# order_result_list.append(None)
|
||||
# response_data = order_result_list
|
||||
# else:
|
||||
# return mk_resp(data=False, status_code=400, response=response) # Bad Request
|
||||
|
||||
# return mk_resp(data=response_data)
|
||||
# # ### END ### API Order ### get_person_obj_order_list() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Order ### get_person_id_order_cart() ###
|
||||
# NOTE 2021-08-09: Use with rework of order_cart. The most recent (hopefully only one) "open" order for a person.
|
||||
# Updated 2021-11-19
|
||||
@router.get('/person/{person_id}/order/cart', response_model=Resp_Body_Base)
|
||||
async def get_person_id_order_cart(
|
||||
person_id: str = Query(..., min_length=11, max_length=22),
|
||||
enabled: str = 'enabled',
|
||||
inc_order_line_list: bool = False,
|
||||
inc_order_cfg: bool = False,
|
||||
x_account_id: str = Header(...),
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
response: Response = Response,
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# Query to get the one "open" order status for a person ID
|
||||
|
||||
return False
|
||||
# ### END ### API Order ### get_person_id_order_cart() ###
|
||||
|
||||
|
||||
@router.delete('/order/{obj_id}', response_model=Resp_Body_Base)
|
||||
async def delete_order_obj(
|
||||
obj_id: str = Query(..., min_length=1, max_length=22),
|
||||
x_account_id: str = Header(...),
|
||||
response: Response = Response,
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
obj_type = 'order'
|
||||
result = delete_obj_template(
|
||||
obj_type=obj_type,
|
||||
obj_id=obj_id,
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
# NOTE 2021-08-09: Use with rework of order_cart
|
||||
# NOTE: The router needs to have the prefix (/order) removed.
|
||||
@router.delete('/order/{order_id}/line/<order_line_id>/remove', response_model=Resp_Body_Base)
|
||||
async def delete_order_line_obj_NOT_SURE(
|
||||
order_id: str = Query(..., min_length=1, max_length=22),
|
||||
obj: Order_Base = None,
|
||||
x_account_id: Optional[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.debug(locals())
|
||||
Reference in New Issue
Block a user