diff --git a/app.py b/app.py index 3ad96f1..e60fb64 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,7 @@ import uuid - import yaml -from flask import Flask, flash, request, render_template +import datetime +from flask import Flask, flash, request, render_template, redirect, url_for app = Flask(__name__) app.secret_key = 'life is pointless' @@ -24,15 +24,34 @@ def record_order(product_id): } with open(ORDER_DB, 'a') as f: f.write(yaml.dump(orders, default_flow_style=False)) + return order_id @app.route('/', methods=['POST', 'GET']) def index(): context = {} if request.method == 'POST': - flash('Order Placed Successfully', 'success') - # TODO - return render_template('index.jinja', products=PRODUCTS, title='Order Form', **context) + product_number = int(request.form['product']) + amount_paid = float(request.form['paid']) + if product_number > 3: + flash('Please choose a product', 'danger') + elif amount_paid < PRODUCTS[product_number]['price']: + flash('Please pay more money', 'danger') + else: + flash('Order Placed Successfully', 'success') + order_info = { + "amount_paid": amount_paid, + "amount_bought": PRODUCTS[product_number]['price'], + "buyer": request.form['buyer'], + "created": datetime.datetime.now().strftime('%Y-%m-%d') + } + order_id = record_order(order_info) + return redirect(url_for('confirmation', order_id=order_id)) + return render_template( + 'index.jinja', + products=PRODUCTS, + title='Order Form', + **context) @app.route('/confirmation/') @@ -42,9 +61,40 @@ def confirmation(order_id): order = orders.get(order_id) if order is None: - pass # TODO what do we do here? - # TODO other stuff has to be calculated here. - return render_template('confirmation.jinja', order_id=order_id, title='Order Confirmation') + return redirect(url_for('index')) + amount_paid = order['product_id']['amount_paid'] + item_price = order['product_id']['amount_bought'] + change_due = round(amount_paid - item_price, 2) + change_left = change_due * 100 + denominations = [] + for item in DENOMINATIONS: + numberOfDenominations = int(change_left/item['value']) + denominations.append({'name': item['name'], 'count': numberOfDenominations}) + change_left -= item['value'] * numberOfDenominations + return render_template( + 'confirmation.jinja', + order_id=order_id, + title='Order Confirmation', + amount_paid=amount_paid, + item_price=item_price, + change_due=change_due, + denominations=denominations) + + +@app.route('/orders') +def orders(): + with open(ORDER_DB) as f: + orders_list = yaml.load(f) + order_info = [] + for order_id, order in orders_list.items(): + order_info.append({'order_id': order_id, + 'created': order['product_id']['created'], + 'buyer': order['product_id']['buyer'], + 'amount_bought': order['product_id']['amount_bought']}) + return render_template( + 'orders.jinja', + orders=order_info, + title='Order List') if __name__ == '__main__': diff --git a/templates/confirmation.jinja b/templates/confirmation.jinja index 5b818e7..68b8ab0 100644 --- a/templates/confirmation.jinja +++ b/templates/confirmation.jinja @@ -15,6 +15,22 @@
{{ change_due }}
Change Denominations
-
+
+ + + + + + + + {% for denomination in denominations %} + + + + + {% endfor %} + +
ChangeCount
{{ denomination['name'] }}{{ denomination['count'] }}
+ Order List {% endblock %} diff --git a/templates/index.jinja b/templates/index.jinja index abf8331..a8b295c 100644 --- a/templates/index.jinja +++ b/templates/index.jinja @@ -6,7 +6,7 @@
+
@@ -38,7 +38,15 @@
+ Order List {% endblock %} diff --git a/templates/orders.jinja b/templates/orders.jinja new file mode 100644 index 0000000..f5aa92c --- /dev/null +++ b/templates/orders.jinja @@ -0,0 +1,24 @@ +{% extends 'base.jinja' %} + +{% block main %} + + + + + + + + + + + {% for order in orders %} + + + + + + + {% endfor %} + +
Order IDBuyer's NameAmount BoughtCreated
{{ order['order_id'] }}{{ order['buyer'] }}{{ order['amount_bought'] }}{{ order['created'] }}
+{% endblock %} \ No newline at end of file