-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (49 loc) · 1.78 KB
/
app.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
####### Imports #######
import json
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS, cross_origin
from flask_restful import Resource, Api
from item import Item
from cart import Cart
####### Setup/Config #######
app = Flask(__name__)
cors = CORS(app)
SECRET_KEY = 'SECRET_KEY'
app.config['SECRET_KEY'] = SECRET_KEY
api = Api(app)
####### Initialize Cart #######
cart = Cart()
####### Route Decorators #######
@app.route('/', methods=['POST', 'GET', 'DELETE'])
@cross_origin()
def index():
if (request.method == 'POST'):
itemData = request.get_json()
if (itemData['action'] == 'add'):
# Initialize Item
item = Item(itemData['id'], itemData['item_name'],
itemData['item_price'], itemData['item_amount'])
cart.addItem(item)
elif (itemData['action'] == 'update'):
# Update Item
item = Item(itemData['id'], itemData['item_name'],
itemData['item_price'], itemData['item_amount'])
cart.updateItem(item)
elif (itemData['action'] == 'discount'):
discountRate = itemData['discountRate']
cart.discount = int(discountRate)
elif (itemData['action'] == 'tax'):
taxRate = itemData['taxRate']
cart.tax = int(taxRate)
cart.calculateTotal()
rtdict = {"netTotal": cart.subtotal, "grandTotal": cart.total}
return (json.dumps(rtdict))
if (request.method == "DELETE"):
cart.removeItem()
cart.calculateTotal()
rtdict = {"netTotal": cart.subtotal, "grandTotal": cart.total}
return (json.dumps(rtdict))
return render_template("index.html")
####### Run Applications #######
if __name__ == '__main__':
app.run(debug=True)