|
@@ -1,14 +1,15 @@
|
|
|
# File Name: app.py
|
|
|
-# Author Name: In-young Choung
|
|
|
-# Date: March 1, 2017
|
|
|
+# Modified Date: April 15, 2017
|
|
|
# Description: This python file contains database configuration and creates database tables and fields using flask_sqlalchemy.
|
|
|
# Users' values passed from Javascript are inserted into the database table.
|
|
|
-# It also has mapping request to interact with specific html files and
|
|
|
-# it reads a file of itemlist.text and prints out to the console.
|
|
|
+# It also has mapping request to interact with specific html files.
|
|
|
+# Method Level Comment: Python Convention (inside function)
|
|
|
+__author__ = "Inyoung Choung"
|
|
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
from flask import Flask, render_template, request
|
|
|
-
|
|
|
+from implementation import Impl
|
|
|
+import datetime
|
|
|
|
|
|
# passing the whole app code to Flask
|
|
|
app = Flask(__name__)
|
|
@@ -26,13 +27,12 @@ class Calc(db.Model):
|
|
|
|
|
|
# database fields
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
- result = db.Column(db.String(64))
|
|
|
- timestampe = db.Column(db.TIMESTAMP)
|
|
|
+ result = db.Column(db.String(255))
|
|
|
+ timestamp = db.Column(db.TIMESTAMP)
|
|
|
|
|
|
# adding constraints to delete all data in Item when calc is deleted.
|
|
|
item = db.relationship('Item', backref='calc', cascade='all, delete-orphan', lazy='dynamic')
|
|
|
|
|
|
-
|
|
|
# database ORM item table - 9 columns
|
|
|
class Item(db.Model):
|
|
|
__tablename__ = "items"
|
|
@@ -41,74 +41,65 @@ class Item(db.Model):
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
item_type = db.Column(db.String(64))
|
|
|
brand_name = db.Column(db.String(64))
|
|
|
- price = db.Column(db.Integer)
|
|
|
- weight = db.Column(db.Integer)
|
|
|
+ price = db.Column(db.DECIMAL)
|
|
|
+ weight = db.Column(db.DECIMAL)
|
|
|
weight_type = db.Column(db.String(5))
|
|
|
- discount = db.Column(db.Integer)
|
|
|
+ discount = db.Column(db.DECIMAL)
|
|
|
discount_type = db.Column(db.String(20))
|
|
|
calc_id = db.Column(db.Integer, db.ForeignKey(Calc.id))
|
|
|
|
|
|
-
|
|
|
# create all the tables defined as classes
|
|
|
-db.create_all()
|
|
|
+# db.create_all()
|
|
|
|
|
|
|
|
|
-# mapping request to URLs
|
|
|
@app.route('/', methods = ['GET', 'POST'])
|
|
|
def index():
|
|
|
+ """
|
|
|
+ mapping request to URLs
|
|
|
+ :return: render_template('index.html')
|
|
|
+ """
|
|
|
if request.method == 'GET':
|
|
|
# renders to html file
|
|
|
return render_template('index.html')
|
|
|
|
|
|
|
|
|
-# end point where python gets user data from javascript
|
|
|
-@app.route('/calculate', methods = ['GET'])
|
|
|
+# These variables are declared as global.
|
|
|
+global newWeightForSecondItem
|
|
|
+global list
|
|
|
+
|
|
|
+
|
|
|
+@app.route('/calculate', methods=['GET'])
|
|
|
def calculate():
|
|
|
- # get value from html
|
|
|
- itemtype = (request.args.get('param1'))
|
|
|
- brandN1 = (request.args.get('param2'))
|
|
|
- brandN2 = (request.args.get('param3'))
|
|
|
- price1 = (request.args.get('param4'))
|
|
|
- price2 = (request.args.get('param5'))
|
|
|
- weight1 = (request.args.get('param6'))
|
|
|
- weightT1 = (request.args.get('param7'))
|
|
|
- weight2 = (request.args.get('param8'))
|
|
|
- weightT2 = (request.args.get('param9'))
|
|
|
- discount1 = (request.args.get('param10'))
|
|
|
- discountT1 = (request.args.get('param11'))
|
|
|
- discount2 = (request.args.get('param12'))
|
|
|
- discountT2 = (request.args.get('param13'))
|
|
|
-
|
|
|
-
|
|
|
- #TODO: calculate function
|
|
|
- # total2 = float(total)
|
|
|
- # new_total = 1.0 + total2
|
|
|
- # print(new_total)
|
|
|
- #
|
|
|
- # weight_types = ['lb', 'kg', 'g']
|
|
|
- #
|
|
|
- # item1_byWeight = float(price1) / float(weight1);
|
|
|
- # item2_byWeight = float(price2) / float(weight2);
|
|
|
- #
|
|
|
- # if (item1_byWeight < item2_byWeight) :
|
|
|
- # difference = float(item2_byWeight) - float(item1_byWeight)
|
|
|
- # result = (item1 + "is cheapter by " + difference)
|
|
|
- # else :
|
|
|
- # difference = float(item1_byWeight) - float(item2_byWeight)
|
|
|
- # result = (item2 + "is cheapter by " + difference)
|
|
|
- #
|
|
|
- # print(param1 + param2)
|
|
|
- # result = param1 + param2
|
|
|
-
|
|
|
- # returns a row from db
|
|
|
- # item_from_db = Item.query.filter_by(item_type=itemtype).first();
|
|
|
-
|
|
|
- # if item_from_db is not None:
|
|
|
- # print("already exists in the database.")
|
|
|
- # else:
|
|
|
+ """
|
|
|
+ end point where python gets user data from javascript.
|
|
|
+ :return: 'Successful'
|
|
|
+ """
|
|
|
+ # get value from javascript.
|
|
|
+ itemtype = str((request.args.get('param1')))
|
|
|
+ brandN1 = str((request.args.get('param2')))
|
|
|
+ brandN2 = str((request.args.get('param3')))
|
|
|
+ price1 = str((request.args.get('param4')))
|
|
|
+ price2 = str((request.args.get('param5')))
|
|
|
+ weight1 = str((request.args.get('param6')))
|
|
|
+ weightT1 = str((request.args.get('param7')))
|
|
|
+ weight2 = str((request.args.get('param8')))
|
|
|
+ weightT2 = str((request.args.get('param9')))
|
|
|
+ discount1 = str((request.args.get('param10')))
|
|
|
+ discountT1 = str((request.args.get('param11')))
|
|
|
+ discount2 = str((request.args.get('param12')))
|
|
|
+ discountT2 = str((request.args.get('param13')))
|
|
|
+
|
|
|
+ # instantiate an object of Impl and pass arguments.
|
|
|
+ implObj = Impl(itemtype, brandN1, brandN2, price1, price2, weight1, weightT1, weight2,
|
|
|
+ weightT2, discount1,discountT1, discount2, discountT2)
|
|
|
|
|
|
try:
|
|
|
- # define array variables to be ready to match with db columns
|
|
|
+ # save data into calc table of the database.
|
|
|
+ calc = Calc(result = implObj.get_result(), timestamp = '{:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))
|
|
|
+ # insert a new row into the database.
|
|
|
+ db.session.add(calc)
|
|
|
+
|
|
|
+ # define array variables to be ready to match with db columns.
|
|
|
brand_names = [brandN1, brandN2]
|
|
|
prices = [price1, price2]
|
|
|
weights = [weight1, weight2]
|
|
@@ -116,43 +107,46 @@ def calculate():
|
|
|
discounts = [discount1, discount2]
|
|
|
discount_types = [discountT1, discountT2]
|
|
|
|
|
|
- # iteration variable for loop
|
|
|
+ # iteration variable for loop.
|
|
|
i = 0
|
|
|
-
|
|
|
- # loop twice to save two compared items into database
|
|
|
+ # loop twice to save two compared items into item table of the database.
|
|
|
for i in range(0, 2):
|
|
|
- item = Item(item_type = itemtype, brand_name = brand_names[i], price =prices[i],
|
|
|
- weight=weights[i] , weight_type = weight_types[i], discount = discounts[i] ,
|
|
|
- discount_type = discount_types[i])
|
|
|
+ item = Item(item_type=itemtype, brand_name=brand_names[i], price=prices[i],
|
|
|
+ weight=weights[i], weight_type=weight_types[i], discount=discounts[i],
|
|
|
+ discount_type=discount_types[i])
|
|
|
|
|
|
- # insert a new row into the database
|
|
|
+ # insert a new row into the database.
|
|
|
db.session.add(item)
|
|
|
|
|
|
- # write it to db disk once the loop is done
|
|
|
+ # write it to db disk once the loop is done.
|
|
|
db.session.commit()
|
|
|
-
|
|
|
- # catch the exception and print it
|
|
|
+ # initialize empty list to collect item objects.
|
|
|
+ list = []
|
|
|
+ # loop for the result of the query to display brand_name column rows of Item table.
|
|
|
+ for row in db.session.query(Item, Item.brand_name).all():
|
|
|
+ # add brand name into the list
|
|
|
+ list.append(row.brand_name)
|
|
|
+
|
|
|
+ # sorts the item list by the alphabetical order.
|
|
|
+ implObj.sort(list)
|
|
|
+
|
|
|
+ # calls writefile function and pass the sorted list.
|
|
|
+ implObj.writefile(None, list)
|
|
|
+ implObj.readfile()
|
|
|
+
|
|
|
+ return 'Successful'
|
|
|
+ # catch the exception and print it.
|
|
|
except ValueError:
|
|
|
print(ValueError)
|
|
|
print("failed to create new row")
|
|
|
+ return -1
|
|
|
|
|
|
|
|
|
-# File I/O - prints a list of grocery items
|
|
|
-path = "C:\In-young Choung\Computer Programming\Self Programming Files\grocerycalc\itemlist.txt"
|
|
|
-# open a file for writing and create it if it doesn't exist.
|
|
|
-f = open(path,"w+")
|
|
|
-
|
|
|
-# open the file in the defined path and "r" read it
|
|
|
-f = open(path, "r")
|
|
|
-if f.mode == 'r':
|
|
|
- # use read function to read the entire file.
|
|
|
- contents = f.read()
|
|
|
- #print the file content in the console
|
|
|
- print(contents)
|
|
|
-
|
|
|
-
|
|
|
-# main method
|
|
|
if __name__ == '__main__':
|
|
|
+ # main method that starts the application.
|
|
|
+
|
|
|
+ # debugging purpose.
|
|
|
app.debug = True
|
|
|
- # app.debug = False
|
|
|
app.run()
|
|
|
+
|
|
|
+
|