| @@ 5870-5993 (lines=124) @@ | ||
| 5867 | resp.text = json.dumps(meta_result) |
|
| 5868 | ||
| 5869 | ||
| 5870 | class EnergyStorageContainerImport: |
|
| 5871 | def __init__(self): |
|
| 5872 | """"Initializes Class""" |
|
| 5873 | pass |
|
| 5874 | ||
| 5875 | @staticmethod |
|
| 5876 | def on_options(req, resp): |
|
| 5877 | resp.status = falcon.HTTP_200 |
|
| 5878 | ||
| 5879 | @staticmethod |
|
| 5880 | @user_logger |
|
| 5881 | def on_post(req, resp): |
|
| 5882 | """Handles POST requests""" |
|
| 5883 | admin_control(req) |
|
| 5884 | try: |
|
| 5885 | raw_json = req.stream.read().decode('utf-8') |
|
| 5886 | except Exception as ex: |
|
| 5887 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5888 | title='API.BAD_REQUEST', |
|
| 5889 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5890 | ||
| 5891 | new_values = json.loads(raw_json) |
|
| 5892 | ||
| 5893 | if 'name' not in new_values.keys() or \ |
|
| 5894 | not isinstance(new_values['name'], str) or \ |
|
| 5895 | len(str.strip(new_values['name'])) == 0: |
|
| 5896 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5897 | description='API.INVALID_ENERGY_STORAGE_CONTAINER_NAME') |
|
| 5898 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
| 5899 | if config.utc_offset[0] == '-': |
|
| 5900 | timezone_offset = -timezone_offset |
|
| 5901 | name = str.strip(new_values['name']) + \ |
|
| 5902 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
|
| 5903 | ||
| 5904 | if 'rated_capacity' not in new_values.keys() or \ |
|
| 5905 | not (isinstance(new_values['rated_capacity'], float) or |
|
| 5906 | isinstance(new_values['rated_capacity'], int)) or \ |
|
| 5907 | new_values['rated_capacity'] < 0.0: |
|
| 5908 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5909 | description='API.INVALID_RATED_CAPACITY') |
|
| 5910 | rated_capacity = new_values['rated_capacity'] |
|
| 5911 | ||
| 5912 | if 'rated_power' not in new_values.keys() or \ |
|
| 5913 | not (isinstance(new_values['rated_power'], float) or |
|
| 5914 | isinstance(new_values['rated_power'], int)) or \ |
|
| 5915 | new_values['rated_power'] < 0.0: |
|
| 5916 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5917 | description='API.INVALID_RATED_POWER') |
|
| 5918 | rated_power = new_values['rated_power'] |
|
| 5919 | ||
| 5920 | if 'contact' not in new_values.keys() or \ |
|
| 5921 | 'id' not in new_values['contact'].keys() or \ |
|
| 5922 | not isinstance(new_values['contact']['id'], int) or \ |
|
| 5923 | new_values['contact']['id'] <= 0: |
|
| 5924 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5925 | description='API.INVALID_CONTACT_ID') |
|
| 5926 | contact_id = new_values['contact']['id'] |
|
| 5927 | ||
| 5928 | if 'cost_center' not in new_values.keys() or \ |
|
| 5929 | 'id' not in new_values['cost_center'].keys() or \ |
|
| 5930 | not isinstance(new_values['cost_center']['id'], int) or \ |
|
| 5931 | new_values['cost_center']['id'] <= 0: |
|
| 5932 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5933 | description='API.INVALID_COST_CENTER_ID') |
|
| 5934 | cost_center_id = new_values['cost_center']['id'] |
|
| 5935 | ||
| 5936 | if 'description' in new_values.keys() and \ |
|
| 5937 | new_values['description'] is not None and \ |
|
| 5938 | len(str(new_values['description'])) > 0: |
|
| 5939 | description = str.strip(new_values['description']) |
|
| 5940 | else: |
|
| 5941 | description = None |
|
| 5942 | ||
| 5943 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5944 | cursor = cnx.cursor() |
|
| 5945 | ||
| 5946 | cursor.execute(" SELECT name " |
|
| 5947 | " FROM tbl_energy_storage_containers " |
|
| 5948 | " WHERE name = %s ", (name,)) |
|
| 5949 | if cursor.fetchone() is not None: |
|
| 5950 | cursor.close() |
|
| 5951 | cnx.close() |
|
| 5952 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5953 | description='API.ENERGY_STORAGE_CONTAINER_NAME_IS_ALREADY_IN_USE') |
|
| 5954 | ||
| 5955 | cursor.execute(" SELECT name " |
|
| 5956 | " FROM tbl_contacts " |
|
| 5957 | " WHERE id = %s ", |
|
| 5958 | (new_values['contact']['id'],)) |
|
| 5959 | row = cursor.fetchone() |
|
| 5960 | if row is None: |
|
| 5961 | cursor.close() |
|
| 5962 | cnx.close() |
|
| 5963 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5964 | description='API.CONTACT_NOT_FOUND') |
|
| 5965 | ||
| 5966 | cursor.execute(" SELECT name " |
|
| 5967 | " FROM tbl_cost_centers " |
|
| 5968 | " WHERE id = %s ", |
|
| 5969 | (new_values['cost_center']['id'],)) |
|
| 5970 | row = cursor.fetchone() |
|
| 5971 | if row is None: |
|
| 5972 | cursor.close() |
|
| 5973 | cnx.close() |
|
| 5974 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5975 | description='API.COST_CENTER_NOT_FOUND') |
|
| 5976 | ||
| 5977 | add_values = (" INSERT INTO tbl_energy_storage_containers " |
|
| 5978 | " (name, uuid, rated_capacity, rated_power, contact_id, cost_center_id, description) " |
|
| 5979 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
|
| 5980 | cursor.execute(add_values, (name, |
|
| 5981 | str(uuid.uuid4()), |
|
| 5982 | rated_capacity, |
|
| 5983 | rated_power, |
|
| 5984 | contact_id, |
|
| 5985 | cost_center_id, |
|
| 5986 | description)) |
|
| 5987 | new_id = cursor.lastrowid |
|
| 5988 | cnx.commit() |
|
| 5989 | cursor.close() |
|
| 5990 | cnx.close() |
|
| 5991 | ||
| 5992 | resp.status = falcon.HTTP_201 |
|
| 5993 | resp.location = '/energystoragecontainers/' + str(new_id) |
|
| 5994 | ||
| @@ 5260-5383 (lines=124) @@ | ||
| 5257 | resp.text = json.dumps(meta_result) |
|
| 5258 | ||
| 5259 | ||
| 5260 | class HybridPowerStationImport: |
|
| 5261 | def __init__(self): |
|
| 5262 | """"Initializes Class""" |
|
| 5263 | pass |
|
| 5264 | ||
| 5265 | @staticmethod |
|
| 5266 | def on_options(req, resp): |
|
| 5267 | resp.status = falcon.HTTP_200 |
|
| 5268 | ||
| 5269 | @staticmethod |
|
| 5270 | @user_logger |
|
| 5271 | def on_post(req, resp): |
|
| 5272 | """Handles POST requests""" |
|
| 5273 | admin_control(req) |
|
| 5274 | try: |
|
| 5275 | raw_json = req.stream.read().decode('utf-8') |
|
| 5276 | except Exception as ex: |
|
| 5277 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 5278 | title='API.BAD_REQUEST', |
|
| 5279 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 5280 | ||
| 5281 | new_values = json.loads(raw_json) |
|
| 5282 | ||
| 5283 | if 'name' not in new_values.keys() or \ |
|
| 5284 | not isinstance(new_values['name'], str) or \ |
|
| 5285 | len(str.strip(new_values['name'])) == 0: |
|
| 5286 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5287 | description='API.INVALID_HYBRID_POWER_STATION_NAME') |
|
| 5288 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
| 5289 | if config.utc_offset[0] == '-': |
|
| 5290 | timezone_offset = -timezone_offset |
|
| 5291 | name = str.strip(new_values['name']) + \ |
|
| 5292 | (datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds') |
|
| 5293 | ||
| 5294 | if 'rated_capacity' not in new_values.keys() or \ |
|
| 5295 | not (isinstance(new_values['rated_capacity'], float) or |
|
| 5296 | isinstance(new_values['rated_capacity'], int)) or \ |
|
| 5297 | new_values['rated_capacity'] < 0.0: |
|
| 5298 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5299 | description='API.INVALID_RATED_CAPACITY') |
|
| 5300 | rated_capacity = new_values['rated_capacity'] |
|
| 5301 | ||
| 5302 | if 'rated_power' not in new_values.keys() or \ |
|
| 5303 | not (isinstance(new_values['rated_power'], float) or |
|
| 5304 | isinstance(new_values['rated_power'], int)) or \ |
|
| 5305 | new_values['rated_power'] < 0.0: |
|
| 5306 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5307 | description='API.INVALID_RATED_POWER') |
|
| 5308 | rated_power = new_values['rated_power'] |
|
| 5309 | ||
| 5310 | if 'contact' not in new_values.keys() or \ |
|
| 5311 | 'id' not in new_values['contact'].keys() or \ |
|
| 5312 | not isinstance(new_values['contact']['id'], int) or \ |
|
| 5313 | new_values['contact']['id'] <= 0: |
|
| 5314 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5315 | description='API.INVALID_CONTACT_ID') |
|
| 5316 | contact_id = new_values['contact']['id'] |
|
| 5317 | ||
| 5318 | if 'cost_center' not in new_values.keys() or \ |
|
| 5319 | 'id' not in new_values['cost_center'].keys() or \ |
|
| 5320 | not isinstance(new_values['cost_center']['id'], int) or \ |
|
| 5321 | new_values['cost_center']['id'] <= 0: |
|
| 5322 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5323 | description='API.INVALID_COST_CENTER_ID') |
|
| 5324 | cost_center_id = new_values['cost_center']['id'] |
|
| 5325 | ||
| 5326 | if 'description' in new_values.keys() and \ |
|
| 5327 | new_values['description'] is not None and \ |
|
| 5328 | len(str(new_values['description'])) > 0: |
|
| 5329 | description = str.strip(new_values['description']) |
|
| 5330 | else: |
|
| 5331 | description = None |
|
| 5332 | ||
| 5333 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 5334 | cursor = cnx.cursor() |
|
| 5335 | ||
| 5336 | cursor.execute(" SELECT name " |
|
| 5337 | " FROM tbl_hybrid_power_stations " |
|
| 5338 | " WHERE name = %s ", (name,)) |
|
| 5339 | if cursor.fetchone() is not None: |
|
| 5340 | cursor.close() |
|
| 5341 | cnx.close() |
|
| 5342 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 5343 | description='API.HYBRID_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
|
| 5344 | ||
| 5345 | cursor.execute(" SELECT name " |
|
| 5346 | " FROM tbl_contacts " |
|
| 5347 | " WHERE id = %s ", |
|
| 5348 | (new_values['contact']['id'],)) |
|
| 5349 | row = cursor.fetchone() |
|
| 5350 | if row is None: |
|
| 5351 | cursor.close() |
|
| 5352 | cnx.close() |
|
| 5353 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5354 | description='API.CONTACT_NOT_FOUND') |
|
| 5355 | ||
| 5356 | cursor.execute(" SELECT name " |
|
| 5357 | " FROM tbl_cost_centers " |
|
| 5358 | " WHERE id = %s ", |
|
| 5359 | (new_values['cost_center']['id'],)) |
|
| 5360 | row = cursor.fetchone() |
|
| 5361 | if row is None: |
|
| 5362 | cursor.close() |
|
| 5363 | cnx.close() |
|
| 5364 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 5365 | description='API.COST_CENTER_NOT_FOUND') |
|
| 5366 | ||
| 5367 | add_values = (" INSERT INTO tbl_hybrid_power_stations " |
|
| 5368 | " (name, uuid, rated_capacity, rated_power, contact_id, cost_center_id, description) " |
|
| 5369 | " VALUES (%s, %s, %s, %s, %s, %s, %s) ") |
|
| 5370 | cursor.execute(add_values, (name, |
|
| 5371 | str(uuid.uuid4()), |
|
| 5372 | rated_capacity, |
|
| 5373 | rated_power, |
|
| 5374 | contact_id, |
|
| 5375 | cost_center_id, |
|
| 5376 | description)) |
|
| 5377 | new_id = cursor.lastrowid |
|
| 5378 | cnx.commit() |
|
| 5379 | cursor.close() |
|
| 5380 | cnx.close() |
|
| 5381 | ||
| 5382 | resp.status = falcon.HTTP_201 |
|
| 5383 | resp.location = '/hybridpowerstations/' + str(new_id) |
|
| 5384 | ||