In this module, you will develop the necessary utility functions for the /checkout
endpoint.
At the top of this file, import Order
and OrderItems
from the chalicelib/models/
directory.
from chalicelib.models.order import Order
from chalicelib.models.order_item import OrderItem
Now, proceed to add the following functions.
This function builds the necessary DynamoDB request parameters to checkout the current user’s shopping cart.
def checkout(order_pk, address):
d = datetime.utcnow().isoformat()
shopping_cart = get_shopping_cart(order_pk)
# Create Order and Order Items
order_items = create_order_items(d, shopping_cart)
order = create_order(d, order_items, order_pk, address)
# Create DynamoDB request params
order_params = {
'operation': 'create',
'payload': {
'Item': order.get_order_info()}}
order_items_params = {
'operation': 'batch_put',
'payload': {
'order_items': order_items}}
# Call DynamoDB
perform_standard_operation(order_params)
perform_batch_put(order_items_params)
print('Checkout successful. Order no: {}.'.format('ORDER#' + str(sha256(d.encode()).hexdigest())))
return clear_shopping_cart(order_pk)
This function returns a list of order items generated from the current user’s shopping cart.
def create_order_items(date, cart):
order_item_sk = 'ORDER#' + str(sha256(date.encode()).hexdigest())
order_items = parse_order_items(order_item_sk, cart)
return order_items
This function parses the current user’s shopping cart, creates an OrderItem
object (see chalicelib/models/orderitem.py
) for each item in the shopping cart, appends them to a list, and returns the resulting list.
def parse_order_items(sk, item_dict):
"""
Parses the body from HTTP request to checkout items
:param sk: the order id that represents which order this item is part of
:param item_dict: list of dictionary objects containing order item information
:return: order_items: list of OrderItem objects
"""
order_items = []
for key, value in item_dict.items():
product_name = key
quantity = value['quantity']
total_item_price = value['total_item_price']
pk = 'ITEM#' + str(sha256(datetime.now().isoformat().encode()).hexdigest())
order_item = OrderItem(item_id=pk, order_id=sk, product_name=product_name, quantity=quantity, total_item_price=total_item_price)
order_items.append(order_item.get_order_item_info())
return order_items
This function creates an Order
object (see chalicelib/models/order.py
) out of the list of order items generated by the create_order_items()
function.
def create_order(date, order_items, order_pk, address):
order_sk = 'ORDER#' + str(sha256(date.encode()).hexdigest())
order_date = datetime.now().isoformat()
order_status = 'PLACED'
total_cart_price = calculate_total_cart_price(order_items)
order = Order(user_id=order_pk, order_id=order_sk, order_status=order_status, order_date=order_date, address=address, total_cart_price=total_cart_price)
return order
This function calculates the total price of the current user’s shopping cart.
def calculate_total_cart_price(order_items):
"""
Calculate the total price of the shopping cart.
:param order_items: list of OrderItem objects
:return: total_cart_price: the total price of the shopping cart
"""
total_cart_price = Decimal(0.0)
for item in order_items:
total_cart_price += item['total_item_price']
return total_cart_price
This function serves to perform a batch write of all the order items generated by the create_order_items()
function.
def perform_batch_put(params):
"""
Performs a batch put on DynamoDB table based on the parameter
payload that was passed.
:param params: the parameters to pass for DynamoDB batch put
:return: None
"""
print(params['payload']['order_items'])
with table.batch_writer() as batch:
for order_item in params['payload']['order_items']:
batch.put_item(order_item)