123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- from flask_sqlalchemy import SQLAlchemy
- from flask import Flask, render_template, request
- app = Flask(__name__)
- app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://groceryadmin:VKov2q3XTtqj6w9o@localhost/groceryapp'
- app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
- db = SQLAlchemy(app)
- class Calc(db.Model):
- __tablename__="calc"
-
- id = db.Column(db.Integer, primary_key=True)
- result = db.Column(db.String(64))
- timestampe = db.Column(db.TIMESTAMP)
-
- item = db.relationship('Item', backref='calc', cascade='all, delete-orphan', lazy='dynamic')
- class Item(db.Model):
- __tablename__ = "items"
-
- 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)
- weight_type = db.Column(db.String(5))
- discount = db.Column(db.Integer)
- discount_type = db.Column(db.String(20))
- calc_id = db.Column(db.Integer, db.ForeignKey(Calc.id))
- db.create_all()
- @app.route('/', methods = ['GET', 'POST'])
- def index():
- if request.method == 'GET':
-
- return render_template('index.html')
- @app.route('/calculate', methods = ['GET'])
- def calculate():
-
- 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'))
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- try:
-
- brand_names = [brandN1, brandN2]
- prices = [price1, price2]
- weights = [weight1, weight2]
- weight_types = [weightT1, weightT2]
- discounts = [discount1, discount2]
- discount_types = [discountT1, discountT2]
-
- i = 0
-
- 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])
-
- db.session.add(item)
-
- db.session.commit()
-
- except ValueError:
- print(ValueError)
- print("failed to create new row")
- path = "C:\In-young Choung\Computer Programming\Self Programming Files\grocerycalc\itemlist.txt"
- f = open(path,"w+")
- f = open(path, "r")
- if f.mode == 'r':
-
- contents = f.read()
-
- print(contents)
- if __name__ == '__main__':
- app.debug = True
-
- app.run()
|