|
@@ -7,23 +7,25 @@ from passlib.apps import custom_app_context as pwd_context
|
|
|
from itsdangerous import (TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired)
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
-app.config['SECRET_KEY'] = 'geunyeorang cheoeum daehoa sijag hajamaja'
|
|
|
-app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://ruvadmin:ge9BQ7fT8bVBgm1B@localhost/ruvapp'
|
|
|
+#Configuration for Database ORM
|
|
|
+app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://groceryadmin:VKov2q3XTtqj6w9o@localhost/groceryapp'
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
|
|
|
db = SQLAlchemy(app)
|
|
|
-auth = HTTPBasicAuth()
|
|
|
-
|
|
|
-
|
|
|
-class MJSONEncoder(JSONEncoder):
|
|
|
|
|
|
- def default(self, obj):
|
|
|
- if isinstance(obj, decimal.Decimal):
|
|
|
- # Convert decimal instances to strings.
|
|
|
- return str(obj)
|
|
|
- return super(MJSONEncoder, self).default(obj)
|
|
|
+app.config['SECRET_KEY'] = 'geunyeorang cheoeum daehoa sijag hajamaja'
|
|
|
+auth = HTTPBasicAuth()
|
|
|
|
|
|
|
|
|
-app.json_encoder = MJSONEncoder
|
|
|
+#This helper class will be used to facilitate the printing of database objects as strings
|
|
|
+# class MJSONEncoder(JSONEncoder):
|
|
|
+# def default(self, obj):
|
|
|
+# if isinstance(obj, decimal.Decimal):
|
|
|
+# # Convert decimal instances to strings.
|
|
|
+# return str(obj)
|
|
|
+# return super(MJSONEncoder, self).default(obj)
|
|
|
+#
|
|
|
+#
|
|
|
+# app.json_encoder = MJSONEncoder
|
|
|
|
|
|
|
|
|
class User(db.Model):
|
|
@@ -59,25 +61,6 @@ class User(db.Model):
|
|
|
return user
|
|
|
|
|
|
|
|
|
-class Roof(db.Model):
|
|
|
- __tablename__ = "roofs"
|
|
|
- id = db.Column(db.Integer, primary_key=True)
|
|
|
- length = db.Column(db.DECIMAL(10, 3))
|
|
|
- width = db.Column(db.DECIMAL(10, 3))
|
|
|
- slope = db.Column(db.Float)
|
|
|
- price = db.Column(db.DECIMAL(10, 2))
|
|
|
- address = db.Column(db.VARCHAR(255))
|
|
|
-
|
|
|
- def serialize(self):
|
|
|
- return {
|
|
|
- 'id': self.id,
|
|
|
- 'length': re.sub("[^0-9^.]", "", str(self.length)),
|
|
|
- 'width': re.sub("[^0-9^.]", "", str(self.width)),
|
|
|
- 'slope': re.sub("[^0-9^.]", "", str(self.slope)),
|
|
|
- 'price': re.sub("[^0-9^.]", "", str(self.price)),
|
|
|
- 'address': self.address.encode("utf-8"),
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
@auth.verify_password
|
|
|
def verify_password(email_or_token, password):
|
|
@@ -94,9 +77,57 @@ def verify_password(email_or_token, password):
|
|
|
return True
|
|
|
|
|
|
|
|
|
-@app.route('/')
|
|
|
+@app.route('/', methods = ['GET', 'POST'])
|
|
|
def index():
|
|
|
- return render_template('index.html')
|
|
|
+ if request.method == 'POST':
|
|
|
+ if request.form['submit'] == 'calculate_btn':
|
|
|
+ weight1 = request.form['weight1']
|
|
|
+ weight2 = request.form['weight2']
|
|
|
+ print (weight1)
|
|
|
+ print (weight2)
|
|
|
+ # my_total = calculate(weight1, weight2)
|
|
|
+ # print (my_total)
|
|
|
+ my_total = 69
|
|
|
+
|
|
|
+ return my_total
|
|
|
+ if request.method == 'GET':
|
|
|
+ return render_template('index.html')
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+@app.route('/calculate_total/', methods = ['GET'])
|
|
|
+def calculate():
|
|
|
+ print (request)
|
|
|
+ print (request.view_args)
|
|
|
+ item1 = (request.args.get('item1'))
|
|
|
+ item2 = (request.args.get('item2'))
|
|
|
+ price1 = (request.args.get('price1'))
|
|
|
+ price2 = (request.args.get('price2'))
|
|
|
+ weight1 = (request.args.get('weight1'))
|
|
|
+ weight2 = (request.args.get('weight2'))
|
|
|
+
|
|
|
+ # total2 = float(total)
|
|
|
+ # new_total = 1.0 + total2
|
|
|
+ # print(new_total)
|
|
|
+
|
|
|
+ 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(result)
|
|
|
+
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+def calculate(weight1, weight2):
|
|
|
+ total = weight1 + weight2
|
|
|
+ return total
|
|
|
|
|
|
|
|
|
@app.route('/login', methods=['POST'])
|
|
@@ -152,38 +183,6 @@ def login():
|
|
|
return jsonify({'email': user.email, 'authToken': token.decode('ascii')}), 201, {'Location': url_for('get_user', id=user.id, _external=True)}
|
|
|
|
|
|
|
|
|
-@app.route('/roof/add', methods=['POST'])
|
|
|
-@auth.login_required
|
|
|
-def add_roof():
|
|
|
- print ('Requesting roof addition')
|
|
|
- if request.headers['Content-Type'] == 'application/json':
|
|
|
- print ('155')
|
|
|
- print (request.json)
|
|
|
- length = request.json.get('length')
|
|
|
- width = request.json.get('width')
|
|
|
- slope = request.json.get('slope')
|
|
|
- address = request.json.get('address')
|
|
|
- price = request.json.get('price')
|
|
|
-
|
|
|
- if length is None or width is None or slope is None or address is None or price is None:
|
|
|
- print ('Something not set')
|
|
|
- abort(400)
|
|
|
- if Roof.query.filter_by(address=address).first() is not None:
|
|
|
- roof = Roof(address=address, price=price)
|
|
|
- print ('Found a roof')
|
|
|
- if roof is not None:
|
|
|
- print ('Roof is not None')
|
|
|
- print (str(roof.serialize()))
|
|
|
- return jsonify({'Roof': roof.serialize()}), 201
|
|
|
- print ('Make new roof')
|
|
|
- roof = Roof(address=address, length=length, width=width, slope=slope, price=price)
|
|
|
- db.session.add(roof)
|
|
|
- db.session.commit()
|
|
|
- print ('Created roof==> ' + str(roof.serialize()))
|
|
|
- return jsonify({'Roof': roof.serialize()}), 201, {
|
|
|
- 'Location': url_for('get_roof', address=roof.address, _external=True)}
|
|
|
-
|
|
|
-
|
|
|
@app.route('/users/<int:id>')
|
|
|
@auth.login_required
|
|
|
def get_user(id):
|
|
@@ -193,15 +192,6 @@ def get_user(id):
|
|
|
return jsonify({'email': user.email, 'password': user.password_hash})
|
|
|
|
|
|
|
|
|
-@app.route('/roofs/<int:id>')
|
|
|
-@auth.login_required
|
|
|
-def get_roof(id):
|
|
|
- roof = Roof.query.get(id)
|
|
|
- if not roof:
|
|
|
- abort(400)
|
|
|
- return jsonify({'Roof': roof.serialize()})
|
|
|
-
|
|
|
-
|
|
|
@app.route('/token')
|
|
|
@auth.login_required
|
|
|
def get_auth_token():
|
|
@@ -215,43 +205,6 @@ def get_resource():
|
|
|
return jsonify({'data': 'Hello, %s!' % g.user.email})
|
|
|
|
|
|
|
|
|
-@app.route('/roofs/all', methods=['GET'])
|
|
|
-@auth.login_required
|
|
|
-def get_roofs():
|
|
|
-
|
|
|
- roofs = Roof.query.all()
|
|
|
- rStr = ''
|
|
|
- mJson = ''
|
|
|
- rlist = [None] * 10
|
|
|
- i = 0
|
|
|
- for roof in roofs:
|
|
|
-
|
|
|
- # rStr += str(jsonify({i: (roof.serialize())}))
|
|
|
- mJson += '{"roof":' + str(roof.serialize()).replace("'", '"') + '},'
|
|
|
- rStr += (str([(str(i) + ":" + str((roof.serialize())))]))
|
|
|
- rObj = (["\""+str(i)+"\"", str(roof.serialize())])
|
|
|
- rlist.append(["roof", rObj])
|
|
|
- i += 1
|
|
|
- # rStr = ("[" + rStr[:-1] + "]")
|
|
|
- mJson = '{"Roofs":[' + str((mJson[:-1])) + ']}'
|
|
|
- # rjson = json.dumps(rStr.replace('\\n', '\n').replace('\"', '"'))
|
|
|
-
|
|
|
- if not roofs:
|
|
|
- abort(400)
|
|
|
- # return jsonify({'Roofs': rStr.replace("\\", "")}), 201
|
|
|
- # return jsonify({'Roofs': str(rStr).replace('\\n', '\n').replace('\"', '"')}), 201
|
|
|
- return (mJson.replace('\\"', '"')), 201
|
|
|
-
|
|
|
-
|
|
|
-def json_list(list):
|
|
|
- lst = []
|
|
|
- for pn in list:
|
|
|
- d = {}
|
|
|
- d['roof']=pn
|
|
|
- lst.append(d)
|
|
|
- return json.dumps(lst)
|
|
|
-
|
|
|
-
|
|
|
if __name__ == '__main__':
|
|
|
app.debug = True
|
|
|
# app.debug = False
|