-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathagrinet.py
284 lines (220 loc) · 7.73 KB
/
agrinet.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
from datetime import datetime
import redis
from flask import Flask, render_template
from flask_json import FlaskJSON, as_json
from app_util import crossdomain
from log_configuration import logger
def initialize_flask():
# Define the Flask App
flask_app = Flask(__name__)
# Setup the Flask JSON Plugin
FlaskJSON(flask_app)
# Detect If Running in Development mode or on server
flask_app.debug = False if "ENV" in os.environ else True
# Retrieve data from the environment and add it to Flask App environment
redis_conn_uri = os.getenv("REDISCLOUD_URL")
# Setup the Flask Redis Plugin
if redis_conn_uri:
flask_app.config["REDIS_URL"] = redis_conn_uri
redis_client = redis.StrictRedis.from_url(redis_conn_uri)
else:
redis_client = redis.StrictRedis()
return flask_app, redis_client
app, redis_client = initialize_flask()
def process_query(target_class, query, req):
if len(req.args) > 0:
# convert to case insensitive search for crop name
if req.args.get("crop"):
query_by_crop = str(req.args.get("crop"))
print(query_by_crop)
elif req.args.get("commodity"):
query_by_commodity = str(req.args.get("commodity"))
print(query_by_commodity)
elif req.args.get("category"):
query_by_category = str(req.args.get("category"))
print(query_by_category)
elif req.args.get("date"):
try: # check if the date is either in one of the two supported formats
query_by_date = datetime.strptime(
str(req.args.get("date")), "%Y-%m-%dT%H:%M:%S"
)
except ValueError:
query_by_date = datetime.strptime(str(req.args.get("date")), "%Y-%m-%d")
print(query_by_date)
return query
@app.route("/api/admin/crops")
@as_json
def clean_up_and_fetch_crops():
from models import delete_crop_records
from pushserver import run
delete_crop_records()
return run(notify=False, override=True)
# Display Pages
@app.route("/")
@crossdomain(origin="*")
def home():
links = []
return render_template("main.html", links=links)
@app.route("/about")
def about():
return render_template("about.html")
# Meta API
# Returns the list of crops available
@app.route("/crops")
@crossdomain(origin="*")
@as_json
def crops_list():
"""Returns a List of all of the crops (commodities) within the database."""
from models import get_distinct_commodity
return get_distinct_commodity()
@app.route("/crops/categories") #
@crossdomain(origin="*")
@as_json
def crop_categories():
"""Returns the list of categories to which crops can belong."""
from models import get_distinct_categories
return get_distinct_categories()
@app.route("/crops/categories/<category>")
@crossdomain(origin="*")
@as_json
def crops_by_category(category=None):
"""Returns the list of crops which belong to the specified category."""
from models import get_distinct_commodity_by_category
return get_distinct_commodity_by_category(category)
# Daily Crop API
@app.route("/crops/daily") #
@crossdomain(origin="*")
@as_json
def crops_all_daily():
"""Returns the daily prices of all the crops."""
from models import get_daily
try:
daily_crops = get_daily()
if not daily_crops:
daily_crops = fetch_latest()["crops"]
return daily_crops
except Exception:
return None, 500
@app.route("/crops/daily/<cid>")
@crossdomain(origin="*")
@as_json
def crops_id(cid=None):
"""Returns the daily price for a specific crop identified by its ID."""
from models import get_daily_by_id
try:
return get_daily_by_id(int(cid))
except Exception:
return None, 404
@app.route("/crops/daily/dates") # returns all the daily dates available
@crossdomain(origin="*")
@as_json
def daily_dates_list():
"""returns all the daily dates available or returns the commodities for the date specified"""
from models import get_daily_dates
dates = get_daily_dates()
return dates or []
@app.route("/crops/daily/dates/<request_date>") # returns all the daily dates available
@crossdomain(origin="*")
@as_json
def get_daily_crops_by_date(request_date=None):
"""returns all the daily dates available or returns the commodities for the date specified"""
app.logger.warning(
f"No longer support more than the current date. The date {request_date} will be ignored"
)
return crops_all_daily()
@app.route("/crops/daily/recent") # Returns the daily prices of the most recent entry
# Returns the most recent daily price of the specified commodity
@app.route("/crops/daily/recent/<crop>")
@crossdomain(origin="*")
@as_json
def most_recent_daily_data(crop=None):
"""Returns the daily prices of the most recent entry
or the most recent daily price of the specified commodity."""
from models import get_most_recent_daily
if crop: # If we have a crop that we want to obtain
try:
return crop_daily_commodity(crop)
except Exception:
return None, 404
else:
try:
daily_crops = get_most_recent_daily()
if not daily_crops:
logger.info("Unable to retrieve records. Retrieving records")
daily_crops = fetch_latest()["crops"]
return daily_crops
except Exception:
return None, 500
@app.route("/crops/daily/category") # list categories available to daily crops
@app.route("/crops/daily/category/<category>") # get daily crops by categories
@crossdomain(origin="*")
@as_json
def crop_daily_categories(category=None):
from models import get_distinct_categories, get_distinct_commodity_by_category
if category:
try:
return get_distinct_commodity_by_category(category)
except Exception:
return None, 404
else:
return get_distinct_categories()
@app.route("/crops/daily/commodity/<commodity>")
@crossdomain(origin="*")
@as_json
def crop_daily_commodity(commodity=None):
from models import get_daily_recent_by_commodity
try:
crops = get_daily_recent_by_commodity(commodity)
if crops:
return crops
else:
return None, 404
except Exception:
return None, 404
@app.route("/latest")
@crossdomain(origin="*")
@as_json
def fetch_latest():
from pushserver import run
return run(notify=False)
# ***** Fish-related endpoints
@app.route("/fishes")
@crossdomain(origin="*")
@as_json
def fish_list():
from models import get_distinct_fish_commodity
return get_distinct_fish_commodity()
@app.route("/fishes/daily/recent")
@app.route("/fishes/daily/recent/<fish>")
@crossdomain(origin="*")
@as_json
def most_recent_daily_fish_merged(fish=None):
"""
if fish specified, returns the most recent daily price of the specified comodity
else Returns the daily prices of the most recent entry
"""
from models import get_daily_recent_by_commodity_fish, get_most_recent_daily_fish
if fish: # If we have a crop that we want to obtain
fishes = get_daily_recent_by_commodity_fish(fish)
if not fishes:
return None, 404
else:
fishes = get_most_recent_daily_fish()
return fishes
@app.route("/fishes/markets")
@crossdomain(origin="*")
@as_json
def market_list():
return [
{"name": "Port of Spain Fish Market", "code": "POSWFM"},
{"name": "Orange Valley Fish Market", "code": "OVWFM"},
]
@app.route("/fishes/daily/recent/market/<market>")
@crossdomain(origin="*")
@as_json
def most_recent_daily_fish_by_market(market):
return []
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8000))
app.run(host="0.0.0.0", port=port, threaded=True)