-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
273 lines (244 loc) · 12.1 KB
/
app.py
File metadata and controls
273 lines (244 loc) · 12.1 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
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
from flask import Flask, jsonify
from flask import abort
from flask import make_response
from flask import request
from flask import url_for
from sqlalchemy import create_engine
from sqlalchemy import Column, String, Integer, DateTime, REAL, JSON, Boolean
from sqlalchemy.engine.url import URL
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime
import json
app = Flask(__name__)
# app.config['SQLALCHEMY_DATABASE_URI'] = "postgres://nutrient:[email protected]:5432/nutrientdb"
db_string = "postgres://nutrient:[email protected]:5432/nutrientdb"
db = create_engine(db_string)
Base = declarative_base()
class NutrientApp(Base):
__tablename__ = 'NutrientApp'
id = Column(Integer, primary_key=True)
latitude = Column(String, nullable=False)
longitude = Column(String, nullable=False)
type = Column(String, nullable=False)
reference_measurements = Column(JSON, nullable=False)
measurement = Column(JSON, nullable=False)
color_method = Column(Integer, nullable=True) # optional
color_correction = Column(String, nullable=True) # optional
mass_concentration = Column(REAL, nullable=False)
light_condition = Column(Integer, nullable=False)
origin_of_water = Column(String, nullable=False)
at_sampling_location = Column(Boolean, nullable=False) # new
temperature = Column(String, nullable=True)
reference_concentrations = Column(JSON, nullable=False) # new
mass_concentration_uncorrected = Column(REAL, nullable=False) # new
error_message = Column(String, nullable=False) # new
label = Column(String, nullable=False)
date = Column(DateTime, nullable=False)
def __repr__(self):
return '<NutrientAPI %r>' % self.type
Session = sessionmaker(db)
session = Session()
Base.metadata.create_all(db) # If there is a table already in the database, by default this command will not create
# session.commit()
print('Initialized the DB')
@app.errorhandler(403)
def not_found(error):
return make_response(jsonify({'error': 'Action not allowed'}), 403)
@app.errorhandler(405)
def not_allowed(error):
return make_response(jsonify({'error': 'Method not allowed'}), 405)
@app.route('/api/v1/samples', methods=['GET'])
def get_samples():
sl = dict()
samples = session.query(NutrientApp)
parameter_types = ['nitrate', 'phosphate', 'custom']
param = request.args.get('type', 'null')
if param == 'null' or param not in parameter_types:
return make_response(jsonify({'error': 'You need to provide parameter type with your request. For example: /api/v1/samples?type=nitrate or phosphate or custom'}), 400)
for s in samples:
sl[s.id] = [s.type, s.latitude, s.longitude, s.measurement, s.origin_of_water, s.mass_concentration, s.date]
json_samples = {"message": "",
"data": {
"type": param,
"samples": [{"latitude": value[1], "longitude":value[2], "measurement": value[3], "origin_of_water": value[4], "mass_concentration":value[5], "date":value[6]} for key, value in sl.items() if value[0] == param]
}}
return jsonify(json_samples)
@app.route('/api/v1/samples/collect_data', methods=['GET', 'POST'])
def collect_data():
reply = {'message': 'Sample Saved.', 'data': {'missing_param': 'nothing'}, 'success': 1}
print(request.json)
if not request.json:
abort(400)
# This block is to check missing parameters
if 'latitude' not in request.json:
reply['data']['missing_param'] = 'latitude'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'longitude' not in request.json:
reply['data']['missing_param'] = 'longitude'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'type' not in request.json:
reply['data']['missing_param'] = 'type'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'reference_measurements' not in request.json:
reply['data']['missing_param'] = 'reference_measurements'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'measurement' not in request.json:
reply['data']['missing_param'] = 'measurement'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'mass_concentration' not in request.json:
reply['data']['missing_param'] = 'mass_concentration'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'light_condition' not in request.json:
reply['data']['missing_param'] = 'light_condition'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'origin_of_water' not in request.json:
reply['data']['missing_param'] = 'origin_of_water'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'at_sampling_location' not in request.json:
reply['data']['missing_param'] = 'at_sampling_location'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'reference_concentrations' not in request.json:
reply['data']['missing_param'] = 'reference_concentrations'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'mass_concentration_uncorrected' not in request.json:
reply['data']['missing_param'] = 'mass_concentration_uncorrected'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'error_message' not in request.json:
reply['data']['missing_param'] = 'error_message'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
if 'label' not in request.json:
reply['data']['missing_param'] = 'label'
reply['success'] = 0
reply['message'] = 'Sample Not Saved'
abort(400, reply)
# This block is to check types of parameters
if 'latitude' in request.json and type(request.json['latitude']) is not str:
reply['message'] = 'latitude value is in bad format. It should be a string.'
reply['success'] = 0
reply['data']['missing_param'] = 'latitude'
abort(400, reply)
if 'longitude' in request.json and type(request.json['longitude']) is not str:
reply['message'] = 'longitude value is in bad format. It should be a string.'
reply['success'] = 0
reply['data']['missing_param'] = 'longitude'
abort(400, reply)
if 'type' in request.json and type(request.json['type']) is not str:
reply['message'] = 'type value is in bad format. It should be a string.'
reply['success'] = 0
reply['data']['missing_param'] = 'type'
abort(400, reply)
if 'reference_measurements' in request.json and type(request.json['reference_measurements']) is not list:
reply['message'] = 'reference_measurements value is in bad format. It should be a list.'
reply['success'] = 0
reply['data']['missing_param'] = 'reference_measurements'
abort(400, reply)
if 'measurement' in request.json and type(request.json['measurement']) is not list:
reply['message'] = 'measurement value is in bad format. It should be a list.'
reply['success'] = 0
reply['data']['missing_param'] = 'measurement'
abort(400, reply)
if 'color_method' in request.json and type(request.json['color_method']) is not int:
reply['message'] = 'color_method value is in bad format. It should be a integer.'
reply['success'] = 0
reply['data']['missing_param'] = 'color_method'
abort(400, reply)
if 'color_correction' in request.json and type(request.json['color_correction']) is not str:
reply['message'] = 'color_correction value is in bad format. It should be a string.'
reply['success'] = 0
reply['data']['missing_param'] = 'color_correction'
abort(400, reply)
if 'mass_concentration' in request.json and type(request.json['mass_concentration']) is not float:
reply['message'] = 'mass_concentration value is in bad format. It should be a double.'
reply['success'] = 0
reply['data']['missing_param'] = 'mass_concentration'
abort(400, reply)
if 'light_condition' in request.json and type(request.json['light_condition']) is not int:
reply['message'] = 'light_condition value is in bad format. It should be a integer.'
reply['success'] = 0
reply['data']['missing_param'] = 'light_condition'
abort(400, reply)
if 'origin_of_water' in request.json and type(request.json['origin_of_water']) is not str:
reply['message'] = 'origin_of_water value is in bad format. It should be a string.'
reply['success'] = 0
reply['data']['missing_param'] = 'origin_of_water'
abort(400, reply)
if 'at_sampling_location' in request.json and type(request.json['at_sampling_location']) is not bool:
reply['message'] = 'at_sampling_location value is in bad format. It should be a boolean.'
reply['success'] = 0
reply['data']['missing_param'] = 'at_sampling_location'
abort(400, reply)
if 'temperature' in request.json and type(request.json['temperature']) is not str:
reply['message'] = 'temperature value is in bad format. It should be a string.'
reply['success'] = 0
reply['data']['missing_param'] = 'temperature'
abort(400, reply)
if 'reference_concentrations' in request.json and type(request.json['reference_concentrations']) is not list:
reply['message'] = 'reference_concentrations value is in bad format. It should be a list.'
reply['success'] = 0
reply['data']['missing_param'] = 'reference_concentrations'
abort(400, reply)
if 'mass_concentration_uncorrected' in request.json and type(request.json['mass_concentration_uncorrected']) is not float:
reply['message'] = 'mass_concentration_uncorrected value is in bad format. It should be a double.'
reply['success'] = 0
reply['data']['missing_param'] = 'mass_concentration_uncorrected'
abort(400, reply)
if 'error_message' in request.json and type(request.json['error_message']) is not str:
reply['error_message'] = 'error_message value is in bad format. It should be a string.'
reply['success'] = 0
reply['data']['missing_param'] = 'error_message'
abort(400, reply)
if 'label' in request.json and type(request.json['label']) is not str:
reply['label'] = 'label value is in bad format. It should be a string.'
reply['success'] = 0
reply['data']['missing_param'] = 'label'
abort(400, reply)
lt = request.json['latitude']
ln = request.json['longitude']
ty = request.json['type']
rm = request.json['reference_measurements']
me = request.json['measurement']
cm = request.json.get('color_method', -1)
cc = request.json.get('color_correction', 'null')
mc = request.json['mass_concentration']
lc = request.json['light_condition']
oow = request.json['origin_of_water']
asl = request.json['at_sampling_location']
tmp = request.json.get('temperature', 'null')
rc = request.json['reference_concentrations']
mcu = request.json['mass_concentration_uncorrected']
em = request.json['error_message']
l = request.json['label']
dt = datetime.now()
na = NutrientApp(latitude=lt, longitude=ln, type=ty, reference_measurements=rm, measurement=me, color_method=cm, color_correction=cc, mass_concentration=mc, light_condition=lc,origin_of_water=oow, temperature=tmp, date=dt, reference_concentrations=rc, mass_concentration_uncorrected=mcu, error_message=em, at_sampling_location=asl, label=l)
session.add(na)
session.commit()
return jsonify(reply)
if __name__ == '__main__':
app.run(debug=True)
# db.init_app(app)