@@ 173-225 (lines=53) @@ | ||
170 | cnx.disconnect() |
|
171 | resp.status = falcon.HTTP_204 |
|
172 | ||
173 | @staticmethod |
|
174 | def on_put(req, resp, id_): |
|
175 | """Handles PUT requests""" |
|
176 | try: |
|
177 | raw_json = req.stream.read().decode('utf-8') |
|
178 | except Exception as ex: |
|
179 | raise falcon.HTTPError(falcon.HTTP_400, 'API.ERROR', ex) |
|
180 | ||
181 | if not id_.isdigit() or int(id_) <= 0: |
|
182 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
183 | description='API.INVALID_GATEWAY_ID') |
|
184 | ||
185 | new_values = json.loads(raw_json) |
|
186 | ||
187 | if 'name' not in new_values['data'].keys() or \ |
|
188 | not isinstance(new_values['data']['name'], str) or \ |
|
189 | len(str.strip(new_values['data']['name'])) == 0: |
|
190 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
191 | description='API.INVALID_GATEWAY_NAME') |
|
192 | name = str.strip(new_values['data']['name']) |
|
193 | ||
194 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
195 | cursor = cnx.cursor() |
|
196 | ||
197 | cursor.execute(" SELECT name " |
|
198 | " FROM tbl_gateways " |
|
199 | " WHERE id = %s ", (id_,)) |
|
200 | if cursor.fetchone() is None: |
|
201 | cursor.close() |
|
202 | cnx.disconnect() |
|
203 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
204 | description='API.GATEWAY_NOT_FOUND') |
|
205 | ||
206 | cursor.execute(" SELECT name " |
|
207 | " FROM tbl_gateways " |
|
208 | " WHERE name = %s AND id != %s ", (name, id_)) |
|
209 | if cursor.fetchone() is not None: |
|
210 | cursor.close() |
|
211 | cnx.disconnect() |
|
212 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
|
213 | description='API.GATEWAY_NAME_IS_ALREADY_IN_USE') |
|
214 | ||
215 | update_row = (" UPDATE tbl_gateways " |
|
216 | " SET name = %s " |
|
217 | " WHERE id = %s ") |
|
218 | cursor.execute(update_row, (name, |
|
219 | id_,)) |
|
220 | cnx.commit() |
|
221 | ||
222 | cursor.close() |
|
223 | cnx.disconnect() |
|
224 | ||
225 | resp.status = falcon.HTTP_200 |
|
226 | ||
227 | ||
228 | class GatewayDataSourceCollection: |
@@ 309-360 (lines=52) @@ | ||
306 | ||
307 | resp.status = falcon.HTTP_204 |
|
308 | ||
309 | @staticmethod |
|
310 | def on_put(req, resp, id_): |
|
311 | """Handles PUT requests""" |
|
312 | if not id_.isdigit() or int(id_) <= 0: |
|
313 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
314 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_ID') |
|
315 | try: |
|
316 | raw_json = req.stream.read().decode('utf-8') |
|
317 | except Exception as ex: |
|
318 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
319 | ||
320 | new_values = json.loads(raw_json) |
|
321 | ||
322 | if 'name' not in new_values['data'].keys() or \ |
|
323 | not isinstance(new_values['data']['name'], str) or \ |
|
324 | len(str.strip(new_values['data']['name'])) == 0: |
|
325 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
326 | description='API.INVALID_ENERGY_FLOW_DIAGRAM_NAME') |
|
327 | name = str.strip(new_values['data']['name']) |
|
328 | ||
329 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
330 | cursor = cnx.cursor() |
|
331 | ||
332 | cursor.execute(" SELECT name " |
|
333 | " FROM tbl_energy_flow_diagrams " |
|
334 | " WHERE id = %s ", (id_,)) |
|
335 | if cursor.fetchone() is None: |
|
336 | cursor.close() |
|
337 | cnx.disconnect() |
|
338 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
339 | description='API.ENERGY_FLOW_DIAGRAM_NOT_FOUND') |
|
340 | ||
341 | cursor.execute(" SELECT name " |
|
342 | " FROM tbl_energy_flow_diagrams " |
|
343 | " WHERE name = %s AND id != %s ", (name, id_)) |
|
344 | if cursor.fetchone() is not None: |
|
345 | cursor.close() |
|
346 | cnx.disconnect() |
|
347 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
|
348 | description='API.ENERGY_FLOW_DIAGRAM_NAME_IS_ALREADY_IN_USE') |
|
349 | ||
350 | update_row = (" UPDATE tbl_energy_flow_diagrams " |
|
351 | " SET name = %s " |
|
352 | " WHERE id = %s ") |
|
353 | cursor.execute(update_row, (name, |
|
354 | id_)) |
|
355 | cnx.commit() |
|
356 | ||
357 | cursor.close() |
|
358 | cnx.disconnect() |
|
359 | ||
360 | resp.status = falcon.HTTP_200 |
|
361 | ||
362 | ||
363 | class EnergyFlowDiagramLinkCollection: |