-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder_management.py
70 lines (64 loc) · 2.42 KB
/
order_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
This module is responsible for handling customer orders inputted by the
user via the user interface.
"""
import time
import datetime
import json
from flask import request
from inventory import get_inventory
from logs import log_warning, log_info
def order_bottles() -> None:
"""
This function fetches the order details using request and saves
it as a dictionary. This is then appended to the orders json file.
"""
order = {
'timestamp': str(time.strftime('%H:%M:%S')) + ' ' + \
str(datetime.date.today()),
'beer_type': request.args.get('beer_type'),
'quantity': float(request.args.get('quantity')),
'date_required': request.args.get('date_required'),
}
with open('orders.json', 'r') as orders_file:
try:
orders = json.load(orders_file)
except ValueError as warning:
orders = []
log_warning(warning)
orders.append(order)
with open('orders.json', 'w') as orders_file:
json.dump(orders, orders_file, indent=2)
log_info('New Customer Order: ' + str(order))
def get_orders() -> None:
"""
This function fetches and returns orders from the orders json file.
"""
with open('orders.json', 'r') as orders_file:
try:
orders = json.load(orders_file)
except:
orders = {}
return orders
def dispatch_order() -> None:
"""
This function dispatches an order when requested by the user and
adjusts the inventory accordingly. The order is identifed by the
timestamp it was entered by the user.
"""
timestamp = request.args.get('timestamp')
orders = get_orders()
for order in orders:
if order['timestamp'] == timestamp:
inventory = get_inventory()
for beer in inventory:
if order['beer_type'] == beer['beer_type'] and \
order['quantity'] <= beer['quantity']:
beer['quantity'] -= order['quantity']
with open('inventory.json', 'w') as inventory_file:
json.dump(inventory, inventory_file, indent=2)
orders.remove(order)
with open('orders.json', 'w') as orders_file:
json.dump(orders, orders_file, indent=2)
break
log_info('Customer Order Dispatched: ' + str(order))