-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (42 loc) · 1.66 KB
/
Copy pathmain.py
File metadata and controls
59 lines (42 loc) · 1.66 KB
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
from flask import Flask, render_template,jsonify,json, request
from HTMLParser import HTMLParser
from bs4 import BeautifulSoup
from models import queries as q
from datetime import datetime
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/')
def index():
return render_template('view.html'),200
# to get cereal name and mandi
@app.route('/get_names/<entity>')
def get_name(entity):
if entity == "cereal":
cereals = q.get_names(cereal = True)
return jsonify(cereals=cereals), 200
if entity == "mandi":
mandis = q.get_names(mandi = True)
return jsonify(mandis = mandis), 200
# to get price(if single date given) or price range(if both) for given mandi and cereal
@app.route('/get_price',methods = ['GET','POST'])
def get_price():
_data = request.get_json() or json.loads(request.data) if request.data else {}
if not _data.get("cereal") and not _data.get("mandi"):
return "Please provide cereal AND mandi", 400
if not _data.get("date1") and not _data.get("date2"):
return "Please provide a date", 400
if _data.get("date1") and _data.get("date2"):
d1 = datetime.strptime(_data.get("date1"), '%m/%d/%Y').date()
d2 = datetime.strptime(_data.get("date2"), '%m/%d/%Y').date()
if d1 > d2:
return "Please select date in proper order",400
dates = [_data.get("date1"),_data.get("date2")]
price_data = q.get_price(_data.get("cereal"),_data.get("mandi"),dates)
else:
if _data.get("date1"):
price_data = q.get_price(_data.get("cereal"),_data.get("mandi"),_data.get('date1'))
else:
price_data = q.get_price(_data.get("cereal"),_data.get("mandi"),_data.get('date2'))
return jsonify(price_data = price_data)
if __name__ == "__main__":
app.run()