Skip to content

Commit

Permalink
Merge pull request #4 from manitbaser/master
Browse files Browse the repository at this point in the history
Added Error Handling
  • Loading branch information
manitbaser authored May 9, 2021
2 parents c84f850 + 8cb1159 commit 7c0f88d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
2 changes: 0 additions & 2 deletions blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ def blacklist_product_product(product_id1, product_id2):
blacklisted_df.to_csv("./blacklisted_product_product.csv")

def blacklist_customer_product(wholesaler_id, product_id):
# print(wholesaler_id)
# print(product_id)
blacklisted_df = pd.read_csv("./blacklisted_wholesaler_product.csv", index_col=0)
blacklisted_df[['blacklisted']] = blacklisted_df[['blacklisted']].applymap(literal_eval)
blacklisted_df.loc[blacklisted_df['wholesaler id'] == wholesaler_id, 'blacklisted'].values[0].append(product_id)
Expand Down
36 changes: 32 additions & 4 deletions main_flask.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
from flask import Flask, redirect, url_for, request, render_template
from flask import Flask, redirect, url_for, request, render_template, flash
from blacklist import *
from main import *
import re
import numpy

def isfloat(value):
try:
float(value)
return True
except ValueError:
return False

app = Flask(__name__, template_folder='template', static_folder='static', static_url_path='')
data1 = list(pd.read_csv('./blacklisted_wholesaler_product.csv')['wholesaler id'])
data2 = list(pd.read_csv('./blacklisted_product_product.csv')['product id'])

@app.route('/success/<user_id>/<user_product>/<user_qty>/',methods = ['GET', 'POST'])
def success(user_id, user_product, user_qty):
if request.method =='POST':
if request.form['bc']=='' and request.form['bp']=='':
ret=main_recommend(int(user_id), int(user_product), float(user_qty))
return render_template('results.html', res=ret, user_id=user_id, user_product=user_product, user_qty=user_qty)
if(request.form['bc']!=''):
print(user_qty)
c2p_blacklist = request.form['bc']
if c2p_blacklist=='' or int(c2p_blacklist) not in (data2):
print("Hello")
ret=main_recommend(int(user_id), int(user_product), float(user_qty))
render_template('results.html', res=ret, user_id=user_id, user_product=user_product, user_qty=user_qty, flash_message="Invalid Product ID provided")
blacklist_customer_product(int(user_id), int(c2p_blacklist))
ret=main_recommend(int(user_id), int(user_product), float(user_qty))
return render_template('results.html', res=ret, user_id=user_id, user_product=user_product, user_qty=user_qty)

if(request.form['bp']!=''):
print(request.form['bp'])
p2p_blacklist = request.form['bp']
print(request.args.get("user_product"))
if p2p_blacklist=='' or int(p2p_blacklist) not in (data2):
ret=main_recommend(int(user_id), int(user_product), float(user_qty))
render_template('results.html', res=ret, user_id=user_id, user_product=user_product, user_qty=user_qty, flash_message="Invalid Product ID provided")
blacklist_product_product(int(user_product), int(p2p_blacklist))
ret=main_recommend(int(user_id), int(user_product), float(user_qty))
return render_template('results.html', res=ret, user_id=user_id, user_product=user_product, user_qty=user_qty)
Expand All @@ -31,6 +49,16 @@ def login():
user_id = request.form['nm']
user_product= request.form['pr']
user_qty = request.form['qty']
# Error Handling

if user_id=='' or int(user_id) not in (data1):
return render_template('index.html', flash_message="Invalid User ID provided")
if user_product=='' or int(user_product) not in (data2):
return render_template('index.html', flash_message="Invalid Product ID provided")
if user_qty=='' or isfloat(user_qty)==False:
return render_template('index.html', flash_message="Invalid Quantity provided")
if (float(user_qty)<0):
return render_template('index.html', flash_message="Invalid Quantity provided")
return redirect(url_for('success',user_id = user_id, user_product=user_product, user_qty=user_qty))
else:
return render_template('index.html')
Expand Down
5 changes: 5 additions & 0 deletions template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<p><input type = "text" name = "qty" id="qty"/></p>
<p><input type = "submit" value="Provide Recommendation" class="btn" /></p>
</form>
{% if flash_message %}
<div class="alert alert-warning">
{{ flash_message }}
</div>
{% endif %}
</div>
</div>
<div class="columnside">
Expand Down

0 comments on commit 7c0f88d

Please sign in to comment.