Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 58 additions & 8 deletions app.py
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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/<order_id>')
Expand All @@ -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__':
Expand Down
18 changes: 17 additions & 1 deletion templates/confirmation.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@
<dd>{{ change_due }}</dd>

<dt>Change Denominations</dt>
<dd><!-- TODO --></dd>
<dd><table class="table">
<thead>
<tr>
<th>Change</th>
<th>Count</th>
</tr>
</thead>
<tbody>
{% for denomination in denominations %}
<tr>
<td>{{ denomination['name'] }}</td>
<td>{{ denomination['count'] }}</td>
</tr>
{% endfor %}
</tbody>
</table></dd>
</dl>
<a href="/orders">Order List</a>
{% endblock %}
14 changes: 11 additions & 3 deletions templates/index.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<label for="product" class="col-sm-2 form-control-label">Product:</label>
<div class="col-sm-8">
<select class="form-control" id="product" name="product">
<option value="">-----</option>
<option value=4>-----</option>
{% for product in products %}
<option value="{{ loop.index - 1 }}">{{ product.name }}</option>
{% endfor %}
Expand All @@ -15,7 +15,7 @@
<div class="col-sm-2 form-control-label">
Price:
<span id="product-price">
TODO
0
</span>
</div>
</div>
Expand All @@ -29,7 +29,7 @@
<label for="paid" class="col-sm-2 form-control-label">Amount Paid:</label>
<div class="col-sm-10">
<!-- could we use a better input type here? -->
<input type="text" class="form-control" id="paid" name="paid" placeholder="Amount Paid" value="{{ paid }}" required>
<input type="number" step=".01" class="form-control" id="paid" name="paid" placeholder="Amount Paid" value="{{ paid }}" required>
</div>
</div>
<div class="form-group row">
Expand All @@ -38,7 +38,15 @@
</div>
</div>
</form>
<a href="/orders">Order List</a>
<script>
var products = {{ products|tojson|safe }};
document.querySelector('#product').addEventListener('change',function(){
if (this.selectedIndex === 0){
document.querySelector('#product-price').innerHTML = 0;
} else {
document.querySelector('#product-price').innerHTML = products[this.selectedIndex - 1].price;
}
});
</script>
{% endblock %}
24 changes: 24 additions & 0 deletions templates/orders.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends 'base.jinja' %}

{% block main %}
<table class="table">
<thead>
<tr>
<th>Order ID</th>
<th>Buyer's Name</th>
<th>Amount Bought</th>
<th>Created</th>
</tr>
</thead>
<tbody>
{% for order in orders %}
<tr>
<td>{{ order['order_id'] }}</td>
<td>{{ order['buyer'] }}</td>
<td>{{ order['amount_bought'] }}</td>
<td>{{ order['created'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}