| @@ 11-340 (lines=330) @@ | ||
| 8 | from decimal import Decimal |
|
| 9 | ||
| 10 | ||
| 11 | class HybridPowerStationCollection: |
|
| 12 | def __init__(self): |
|
| 13 | """"Initializes Class""" |
|
| 14 | pass |
|
| 15 | ||
| 16 | @staticmethod |
|
| 17 | def on_options(req, resp): |
|
| 18 | resp.status = falcon.HTTP_200 |
|
| 19 | ||
| 20 | @staticmethod |
|
| 21 | def on_get(req, resp): |
|
| 22 | access_control(req) |
|
| 23 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 24 | cursor = cnx.cursor() |
|
| 25 | ||
| 26 | # query contact dict |
|
| 27 | contact_dict = dict() |
|
| 28 | query = (" SELECT id, name, uuid " |
|
| 29 | " FROM tbl_contacts ") |
|
| 30 | cursor.execute(query) |
|
| 31 | rows_contacts = cursor.fetchall() |
|
| 32 | if rows_contacts is not None and len(rows_contacts) > 0: |
|
| 33 | for row in rows_contacts: |
|
| 34 | contact_dict[row[0]] = {"id": row[0], |
|
| 35 | "name": row[1], |
|
| 36 | "uuid": row[2]} |
|
| 37 | # query cost center dict |
|
| 38 | cost_center_dict = dict() |
|
| 39 | query = (" SELECT id, name, uuid " |
|
| 40 | " FROM tbl_cost_centers ") |
|
| 41 | cursor.execute(query) |
|
| 42 | rows_cost_centers = cursor.fetchall() |
|
| 43 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
|
| 44 | for row in rows_cost_centers: |
|
| 45 | cost_center_dict[row[0]] = {"id": row[0], |
|
| 46 | "name": row[1], |
|
| 47 | "uuid": row[2]} |
|
| 48 | ||
| 49 | # query svg dict |
|
| 50 | svg_dict = dict() |
|
| 51 | query = (" SELECT id, name, uuid " |
|
| 52 | " FROM tbl_svgs ") |
|
| 53 | cursor.execute(query) |
|
| 54 | rows_svgs = cursor.fetchall() |
|
| 55 | if rows_svgs is not None and len(rows_svgs) > 0: |
|
| 56 | for row in rows_svgs: |
|
| 57 | svg_dict[row[0]] = {"id": row[0], |
|
| 58 | "name": row[1], |
|
| 59 | "uuid": row[2]} |
|
| 60 | ||
| 61 | # query point dict |
|
| 62 | point_dict = dict() |
|
| 63 | query = (" SELECT id, name " |
|
| 64 | " FROM tbl_points ") |
|
| 65 | cursor.execute(query) |
|
| 66 | rows_points = cursor.fetchall() |
|
| 67 | if rows_points is not None and len(rows_points) > 0: |
|
| 68 | for row in rows_points: |
|
| 69 | point_dict[row[0]] = {"id": row[0], |
|
| 70 | "name": row[1]} |
|
| 71 | ||
| 72 | query = (" SELECT id, name, uuid, " |
|
| 73 | " address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
|
| 74 | " contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description, " |
|
| 75 | " latitude_point_id, longitude_point_id, svg2_id, svg3_id, svg4_id, svg5_id " |
|
| 76 | " FROM tbl_hybrid_power_stations " |
|
| 77 | " ORDER BY id ") |
|
| 78 | cursor.execute(query) |
|
| 79 | rows_hybrid_power_stations = cursor.fetchall() |
|
| 80 | ||
| 81 | result = list() |
|
| 82 | if rows_hybrid_power_stations is not None and len(rows_hybrid_power_stations) > 0: |
|
| 83 | for row in rows_hybrid_power_stations: |
|
| 84 | meta_result = {"id": row[0], |
|
| 85 | "name": row[1], |
|
| 86 | "uuid": row[2], |
|
| 87 | "address": row[3], |
|
| 88 | "postal_code": row[4], |
|
| 89 | "latitude": row[5], |
|
| 90 | "longitude": row[6], |
|
| 91 | "rated_capacity": row[7], |
|
| 92 | "rated_power": row[8], |
|
| 93 | "contact": contact_dict.get(row[9], None), |
|
| 94 | "cost_center": cost_center_dict.get(row[10], None), |
|
| 95 | "svg": svg_dict.get(row[11], None), |
|
| 96 | "is_cost_data_displayed": bool(row[12]), |
|
| 97 | "phase_of_lifecycle": row[13], |
|
| 98 | "description": row[14], |
|
| 99 | "latitude_point": point_dict.get(row[15], None), |
|
| 100 | "longitude_point": point_dict.get(row[16], None), |
|
| 101 | "svg2": svg_dict.get(row[17], None), |
|
| 102 | "svg3": svg_dict.get(row[18], None), |
|
| 103 | "svg4": svg_dict.get(row[19], None), |
|
| 104 | "svg5": svg_dict.get(row[20], None), |
|
| 105 | "qrcode": 'hybridpowerstation:' + row[2]} |
|
| 106 | result.append(meta_result) |
|
| 107 | ||
| 108 | cursor.close() |
|
| 109 | cnx.close() |
|
| 110 | resp.text = json.dumps(result) |
|
| 111 | ||
| 112 | @staticmethod |
|
| 113 | @user_logger |
|
| 114 | def on_post(req, resp): |
|
| 115 | """Handles POST requests""" |
|
| 116 | admin_control(req) |
|
| 117 | try: |
|
| 118 | raw_json = req.stream.read().decode('utf-8') |
|
| 119 | except Exception as ex: |
|
| 120 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 121 | title='API.BAD_REQUEST', |
|
| 122 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 123 | ||
| 124 | new_values = json.loads(raw_json) |
|
| 125 | ||
| 126 | if 'name' not in new_values['data'].keys() or \ |
|
| 127 | not isinstance(new_values['data']['name'], str) or \ |
|
| 128 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 129 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 130 | description='API.INVALID_HYBRID_POWER_STATION_NAME') |
|
| 131 | name = str.strip(new_values['data']['name']) |
|
| 132 | ||
| 133 | if 'address' not in new_values['data'].keys() or \ |
|
| 134 | not isinstance(new_values['data']['address'], str) or \ |
|
| 135 | len(str.strip(new_values['data']['address'])) == 0: |
|
| 136 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 137 | description='API.INVALID_ADDRESS_VALUE') |
|
| 138 | address = str.strip(new_values['data']['address']) |
|
| 139 | ||
| 140 | if 'postal_code' not in new_values['data'].keys() or \ |
|
| 141 | not isinstance(new_values['data']['postal_code'], str) or \ |
|
| 142 | len(str.strip(new_values['data']['postal_code'])) == 0: |
|
| 143 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 144 | description='API.INVALID_POSTAL_CODE_VALUE') |
|
| 145 | postal_code = str.strip(new_values['data']['postal_code']) |
|
| 146 | ||
| 147 | if 'latitude' not in new_values['data'].keys() or \ |
|
| 148 | not (isinstance(new_values['data']['latitude'], float) or |
|
| 149 | isinstance(new_values['data']['latitude'], int)) or \ |
|
| 150 | new_values['data']['latitude'] < -90.0 or \ |
|
| 151 | new_values['data']['latitude'] > 90.0: |
|
| 152 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 153 | description='API.INVALID_LATITUDE_VALUE') |
|
| 154 | latitude = new_values['data']['latitude'] |
|
| 155 | ||
| 156 | if 'longitude' not in new_values['data'].keys() or \ |
|
| 157 | not (isinstance(new_values['data']['longitude'], float) or |
|
| 158 | isinstance(new_values['data']['longitude'], int)) or \ |
|
| 159 | new_values['data']['longitude'] < -180.0 or \ |
|
| 160 | new_values['data']['longitude'] > 180.0: |
|
| 161 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 162 | description='API.INVALID_LONGITUDE_VALUE') |
|
| 163 | longitude = new_values['data']['longitude'] |
|
| 164 | ||
| 165 | if 'rated_capacity' not in new_values['data'].keys() or \ |
|
| 166 | not (isinstance(new_values['data']['rated_capacity'], float) or |
|
| 167 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
|
| 168 | new_values['data']['rated_capacity'] < 0.0: |
|
| 169 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 170 | description='API.INVALID_RATED_CAPACITY') |
|
| 171 | rated_capacity = new_values['data']['rated_capacity'] |
|
| 172 | ||
| 173 | if 'rated_power' not in new_values['data'].keys() or \ |
|
| 174 | not (isinstance(new_values['data']['rated_power'], float) or |
|
| 175 | isinstance(new_values['data']['rated_power'], int)) or \ |
|
| 176 | new_values['data']['rated_power'] < 0.0: |
|
| 177 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 178 | description='API.INVALID_RATED_POWER') |
|
| 179 | rated_power = new_values['data']['rated_power'] |
|
| 180 | ||
| 181 | if 'contact_id' not in new_values['data'].keys() or \ |
|
| 182 | not isinstance(new_values['data']['contact_id'], int) or \ |
|
| 183 | new_values['data']['contact_id'] <= 0: |
|
| 184 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 185 | description='API.INVALID_CONTACT_ID') |
|
| 186 | contact_id = new_values['data']['contact_id'] |
|
| 187 | ||
| 188 | if 'cost_center_id' not in new_values['data'].keys() or \ |
|
| 189 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
|
| 190 | new_values['data']['cost_center_id'] <= 0: |
|
| 191 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 192 | description='API.INVALID_COST_CENTER_ID') |
|
| 193 | cost_center_id = new_values['data']['cost_center_id'] |
|
| 194 | ||
| 195 | if 'svg_id' not in new_values['data'].keys() or \ |
|
| 196 | not isinstance(new_values['data']['svg_id'], int) or \ |
|
| 197 | new_values['data']['svg_id'] <= 0: |
|
| 198 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 199 | description='API.INVALID_SVG_ID') |
|
| 200 | svg_id = new_values['data']['svg_id'] |
|
| 201 | ||
| 202 | if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
|
| 203 | not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
|
| 204 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 205 | description='API.INVALID_IS_COST_DATA_DISPLAYED') |
|
| 206 | is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
|
| 207 | ||
| 208 | if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
|
| 209 | not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
|
| 210 | len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
|
| 211 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 212 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
|
| 213 | phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
|
| 214 | ||
| 215 | if 'description' in new_values['data'].keys() and \ |
|
| 216 | new_values['data']['description'] is not None and \ |
|
| 217 | len(str(new_values['data']['description'])) > 0: |
|
| 218 | description = str.strip(new_values['data']['description']) |
|
| 219 | else: |
|
| 220 | description = None |
|
| 221 | ||
| 222 | if 'latitude_point_id' in new_values['data'].keys() and \ |
|
| 223 | isinstance(new_values['data']['latitude_point_id'], int) and \ |
|
| 224 | new_values['data']['latitude_point_id'] > 0: |
|
| 225 | latitude_point_id = new_values['data']['latitude_point_id'] |
|
| 226 | else: |
|
| 227 | latitude_point_id = None |
|
| 228 | ||
| 229 | if 'longitude_point_id' in new_values['data'].keys() and \ |
|
| 230 | isinstance(new_values['data']['longitude_point_id'], int) and \ |
|
| 231 | new_values['data']['longitude_point_id'] > 0: |
|
| 232 | longitude_point_id = new_values['data']['longitude_point_id'] |
|
| 233 | else: |
|
| 234 | longitude_point_id = None |
|
| 235 | ||
| 236 | if 'svg2_id' in new_values['data'].keys() and \ |
|
| 237 | isinstance(new_values['data']['svg2_id'], int) and \ |
|
| 238 | new_values['data']['svg2_id'] > 0: |
|
| 239 | svg2_id = new_values['data']['svg2_id'] |
|
| 240 | else: |
|
| 241 | svg2_id = None |
|
| 242 | ||
| 243 | if 'svg3_id' in new_values['data'].keys() and \ |
|
| 244 | isinstance(new_values['data']['svg3_id'], int) and \ |
|
| 245 | new_values['data']['svg3_id'] > 0: |
|
| 246 | svg3_id = new_values['data']['svg3_id'] |
|
| 247 | else: |
|
| 248 | svg3_id = None |
|
| 249 | ||
| 250 | if 'svg4_id' in new_values['data'].keys() and \ |
|
| 251 | isinstance(new_values['data']['svg4_id'], int) and \ |
|
| 252 | new_values['data']['svg4_id'] > 0: |
|
| 253 | svg4_id = new_values['data']['svg4_id'] |
|
| 254 | else: |
|
| 255 | svg4_id = None |
|
| 256 | ||
| 257 | if 'svg5_id' in new_values['data'].keys() and \ |
|
| 258 | isinstance(new_values['data']['svg5_id'], int) and \ |
|
| 259 | new_values['data']['svg5_id'] > 0: |
|
| 260 | svg5_id = new_values['data']['svg5_id'] |
|
| 261 | else: |
|
| 262 | svg5_id = None |
|
| 263 | ||
| 264 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 265 | cursor = cnx.cursor() |
|
| 266 | ||
| 267 | cursor.execute(" SELECT name " |
|
| 268 | " FROM tbl_hybrid_power_stations " |
|
| 269 | " WHERE name = %s ", (name,)) |
|
| 270 | if cursor.fetchone() is not None: |
|
| 271 | cursor.close() |
|
| 272 | cnx.close() |
|
| 273 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 274 | description='API.HYBRID_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
|
| 275 | ||
| 276 | cursor.execute(" SELECT name " |
|
| 277 | " FROM tbl_contacts " |
|
| 278 | " WHERE id = %s ", |
|
| 279 | (contact_id,)) |
|
| 280 | row = cursor.fetchone() |
|
| 281 | if row is None: |
|
| 282 | cursor.close() |
|
| 283 | cnx.close() |
|
| 284 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 285 | description='API.CONTACT_NOT_FOUND') |
|
| 286 | ||
| 287 | cursor.execute(" SELECT name " |
|
| 288 | " FROM tbl_cost_centers " |
|
| 289 | " WHERE id = %s ", |
|
| 290 | (cost_center_id,)) |
|
| 291 | row = cursor.fetchone() |
|
| 292 | if row is None: |
|
| 293 | cursor.close() |
|
| 294 | cnx.close() |
|
| 295 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 296 | description='API.COST_CENTER_NOT_FOUND') |
|
| 297 | ||
| 298 | cursor.execute(" SELECT name " |
|
| 299 | " FROM tbl_svgs " |
|
| 300 | " WHERE id = %s ", |
|
| 301 | (svg_id,)) |
|
| 302 | row = cursor.fetchone() |
|
| 303 | if row is None: |
|
| 304 | cursor.close() |
|
| 305 | cnx.close() |
|
| 306 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 307 | description='API.SVG_NOT_FOUND') |
|
| 308 | ||
| 309 | add_values = (" INSERT INTO tbl_hybrid_power_stations " |
|
| 310 | " (name, uuid, address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
|
| 311 | " contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description, " |
|
| 312 | " latitude_point_id, longitude_point_id, svg2_id, svg3_id, svg4_id, svg5_id ) " |
|
| 313 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
|
| 314 | cursor.execute(add_values, (name, |
|
| 315 | str(uuid.uuid4()), |
|
| 316 | address, |
|
| 317 | postal_code, |
|
| 318 | latitude, |
|
| 319 | longitude, |
|
| 320 | rated_capacity, |
|
| 321 | rated_power, |
|
| 322 | contact_id, |
|
| 323 | cost_center_id, |
|
| 324 | svg_id, |
|
| 325 | is_cost_data_displayed, |
|
| 326 | phase_of_lifecycle, |
|
| 327 | description, |
|
| 328 | latitude_point_id, |
|
| 329 | longitude_point_id, |
|
| 330 | svg2_id, |
|
| 331 | svg3_id, |
|
| 332 | svg4_id, |
|
| 333 | svg5_id)) |
|
| 334 | new_id = cursor.lastrowid |
|
| 335 | cnx.commit() |
|
| 336 | cursor.close() |
|
| 337 | cnx.close() |
|
| 338 | ||
| 339 | resp.status = falcon.HTTP_201 |
|
| 340 | resp.location = '/hybridpowerstations/' + str(new_id) |
|
| 341 | ||
| 342 | ||
| 343 | class HybridPowerStationItem: |
|
| @@ 10-339 (lines=330) @@ | ||
| 7 | import config |
|
| 8 | ||
| 9 | ||
| 10 | class EnergyStoragePowerStationCollection: |
|
| 11 | def __init__(self): |
|
| 12 | """"Initializes Class""" |
|
| 13 | pass |
|
| 14 | ||
| 15 | @staticmethod |
|
| 16 | def on_options(req, resp): |
|
| 17 | resp.status = falcon.HTTP_200 |
|
| 18 | ||
| 19 | @staticmethod |
|
| 20 | def on_get(req, resp): |
|
| 21 | access_control(req) |
|
| 22 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 23 | cursor = cnx.cursor() |
|
| 24 | ||
| 25 | # query contact dict |
|
| 26 | contact_dict = dict() |
|
| 27 | query = (" SELECT id, name, uuid " |
|
| 28 | " FROM tbl_contacts ") |
|
| 29 | cursor.execute(query) |
|
| 30 | rows_contacts = cursor.fetchall() |
|
| 31 | if rows_contacts is not None and len(rows_contacts) > 0: |
|
| 32 | for row in rows_contacts: |
|
| 33 | contact_dict[row[0]] = {"id": row[0], |
|
| 34 | "name": row[1], |
|
| 35 | "uuid": row[2]} |
|
| 36 | # query cost center dict |
|
| 37 | cost_center_dict = dict() |
|
| 38 | query = (" SELECT id, name, uuid " |
|
| 39 | " FROM tbl_cost_centers ") |
|
| 40 | cursor.execute(query) |
|
| 41 | rows_cost_centers = cursor.fetchall() |
|
| 42 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
|
| 43 | for row in rows_cost_centers: |
|
| 44 | cost_center_dict[row[0]] = {"id": row[0], |
|
| 45 | "name": row[1], |
|
| 46 | "uuid": row[2]} |
|
| 47 | ||
| 48 | # query svg dict |
|
| 49 | svg_dict = dict() |
|
| 50 | query = (" SELECT id, name, uuid " |
|
| 51 | " FROM tbl_svgs ") |
|
| 52 | cursor.execute(query) |
|
| 53 | rows_svgs = cursor.fetchall() |
|
| 54 | if rows_svgs is not None and len(rows_svgs) > 0: |
|
| 55 | for row in rows_svgs: |
|
| 56 | svg_dict[row[0]] = {"id": row[0], |
|
| 57 | "name": row[1], |
|
| 58 | "uuid": row[2]} |
|
| 59 | ||
| 60 | # query point dict |
|
| 61 | point_dict = dict() |
|
| 62 | query = (" SELECT id, name " |
|
| 63 | " FROM tbl_points ") |
|
| 64 | cursor.execute(query) |
|
| 65 | rows_points = cursor.fetchall() |
|
| 66 | if rows_points is not None and len(rows_points) > 0: |
|
| 67 | for row in rows_points: |
|
| 68 | point_dict[row[0]] = {"id": row[0], |
|
| 69 | "name": row[1]} |
|
| 70 | ||
| 71 | query = (" SELECT id, name, uuid, " |
|
| 72 | " address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
|
| 73 | " contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description, " |
|
| 74 | " latitude_point_id, longitude_point_id, svg2_id, svg3_id, svg4_id, svg5_id " |
|
| 75 | " FROM tbl_energy_storage_power_stations " |
|
| 76 | " ORDER BY id ") |
|
| 77 | cursor.execute(query) |
|
| 78 | rows_energy_storage_power_stations = cursor.fetchall() |
|
| 79 | ||
| 80 | result = list() |
|
| 81 | if rows_energy_storage_power_stations is not None and len(rows_energy_storage_power_stations) > 0: |
|
| 82 | for row in rows_energy_storage_power_stations: |
|
| 83 | meta_result = {"id": row[0], |
|
| 84 | "name": row[1], |
|
| 85 | "uuid": row[2], |
|
| 86 | "address": row[3], |
|
| 87 | "postal_code": row[4], |
|
| 88 | "latitude": row[5], |
|
| 89 | "longitude": row[6], |
|
| 90 | "rated_capacity": row[7], |
|
| 91 | "rated_power": row[8], |
|
| 92 | "contact": contact_dict.get(row[9], None), |
|
| 93 | "cost_center": cost_center_dict.get(row[10], None), |
|
| 94 | "svg": svg_dict.get(row[11], None), |
|
| 95 | "is_cost_data_displayed": bool(row[12]), |
|
| 96 | "phase_of_lifecycle": row[13], |
|
| 97 | "description": row[14], |
|
| 98 | "latitude_point": point_dict.get(row[15], None), |
|
| 99 | "longitude_point": point_dict.get(row[16], None), |
|
| 100 | "svg2": svg_dict.get(row[17], None), |
|
| 101 | "svg3": svg_dict.get(row[18], None), |
|
| 102 | "svg4": svg_dict.get(row[19], None), |
|
| 103 | "svg5": svg_dict.get(row[20], None), |
|
| 104 | "qrcode": 'energystoragepowerstation:' + row[2]} |
|
| 105 | result.append(meta_result) |
|
| 106 | ||
| 107 | cursor.close() |
|
| 108 | cnx.close() |
|
| 109 | resp.text = json.dumps(result) |
|
| 110 | ||
| 111 | @staticmethod |
|
| 112 | @user_logger |
|
| 113 | def on_post(req, resp): |
|
| 114 | """Handles POST requests""" |
|
| 115 | admin_control(req) |
|
| 116 | try: |
|
| 117 | raw_json = req.stream.read().decode('utf-8') |
|
| 118 | except Exception as ex: |
|
| 119 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 120 | title='API.BAD_REQUEST', |
|
| 121 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 122 | ||
| 123 | new_values = json.loads(raw_json) |
|
| 124 | ||
| 125 | if 'name' not in new_values['data'].keys() or \ |
|
| 126 | not isinstance(new_values['data']['name'], str) or \ |
|
| 127 | len(str.strip(new_values['data']['name'])) == 0: |
|
| 128 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 129 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_NAME') |
|
| 130 | name = str.strip(new_values['data']['name']) |
|
| 131 | ||
| 132 | if 'address' not in new_values['data'].keys() or \ |
|
| 133 | not isinstance(new_values['data']['address'], str) or \ |
|
| 134 | len(str.strip(new_values['data']['address'])) == 0: |
|
| 135 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 136 | description='API.INVALID_ADDRESS_VALUE') |
|
| 137 | address = str.strip(new_values['data']['address']) |
|
| 138 | ||
| 139 | if 'postal_code' not in new_values['data'].keys() or \ |
|
| 140 | not isinstance(new_values['data']['postal_code'], str) or \ |
|
| 141 | len(str.strip(new_values['data']['postal_code'])) == 0: |
|
| 142 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 143 | description='API.INVALID_POSTAL_CODE_VALUE') |
|
| 144 | postal_code = str.strip(new_values['data']['postal_code']) |
|
| 145 | ||
| 146 | if 'latitude' not in new_values['data'].keys() or \ |
|
| 147 | not (isinstance(new_values['data']['latitude'], float) or |
|
| 148 | isinstance(new_values['data']['latitude'], int)) or \ |
|
| 149 | new_values['data']['latitude'] < -90.0 or \ |
|
| 150 | new_values['data']['latitude'] > 90.0: |
|
| 151 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 152 | description='API.INVALID_LATITUDE_VALUE') |
|
| 153 | latitude = new_values['data']['latitude'] |
|
| 154 | ||
| 155 | if 'longitude' not in new_values['data'].keys() or \ |
|
| 156 | not (isinstance(new_values['data']['longitude'], float) or |
|
| 157 | isinstance(new_values['data']['longitude'], int)) or \ |
|
| 158 | new_values['data']['longitude'] < -180.0 or \ |
|
| 159 | new_values['data']['longitude'] > 180.0: |
|
| 160 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 161 | description='API.INVALID_LONGITUDE_VALUE') |
|
| 162 | longitude = new_values['data']['longitude'] |
|
| 163 | ||
| 164 | if 'rated_capacity' not in new_values['data'].keys() or \ |
|
| 165 | not (isinstance(new_values['data']['rated_capacity'], float) or |
|
| 166 | isinstance(new_values['data']['rated_capacity'], int)) or \ |
|
| 167 | new_values['data']['rated_capacity'] < 0.0: |
|
| 168 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 169 | description='API.INVALID_RATED_CAPACITY') |
|
| 170 | rated_capacity = new_values['data']['rated_capacity'] |
|
| 171 | ||
| 172 | if 'rated_power' not in new_values['data'].keys() or \ |
|
| 173 | not (isinstance(new_values['data']['rated_power'], float) or |
|
| 174 | isinstance(new_values['data']['rated_power'], int)) or \ |
|
| 175 | new_values['data']['rated_power'] < 0.0: |
|
| 176 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 177 | description='API.INVALID_RATED_POWER') |
|
| 178 | rated_power = new_values['data']['rated_power'] |
|
| 179 | ||
| 180 | if 'contact_id' not in new_values['data'].keys() or \ |
|
| 181 | not isinstance(new_values['data']['contact_id'], int) or \ |
|
| 182 | new_values['data']['contact_id'] <= 0: |
|
| 183 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 184 | description='API.INVALID_CONTACT_ID') |
|
| 185 | contact_id = new_values['data']['contact_id'] |
|
| 186 | ||
| 187 | if 'cost_center_id' not in new_values['data'].keys() or \ |
|
| 188 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
|
| 189 | new_values['data']['cost_center_id'] <= 0: |
|
| 190 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 191 | description='API.INVALID_COST_CENTER_ID') |
|
| 192 | cost_center_id = new_values['data']['cost_center_id'] |
|
| 193 | ||
| 194 | if 'svg_id' not in new_values['data'].keys() or \ |
|
| 195 | not isinstance(new_values['data']['svg_id'], int) or \ |
|
| 196 | new_values['data']['svg_id'] <= 0: |
|
| 197 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 198 | description='API.INVALID_SVG_ID') |
|
| 199 | svg_id = new_values['data']['svg_id'] |
|
| 200 | ||
| 201 | if 'is_cost_data_displayed' not in new_values['data'].keys() or \ |
|
| 202 | not isinstance(new_values['data']['is_cost_data_displayed'], bool): |
|
| 203 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 204 | description='API.INVALID_IS_COST_DATA_DISPLAYED') |
|
| 205 | is_cost_data_displayed = new_values['data']['is_cost_data_displayed'] |
|
| 206 | ||
| 207 | if 'phase_of_lifecycle' not in new_values['data'].keys() or \ |
|
| 208 | not isinstance(new_values['data']['phase_of_lifecycle'], str) or \ |
|
| 209 | len(str.strip(new_values['data']['phase_of_lifecycle'])) == 0: |
|
| 210 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 211 | description='API.INVALID_PHASE_OF_LIFECYCLE') |
|
| 212 | phase_of_lifecycle = str.strip(new_values['data']['phase_of_lifecycle']) |
|
| 213 | ||
| 214 | if 'description' in new_values['data'].keys() and \ |
|
| 215 | new_values['data']['description'] is not None and \ |
|
| 216 | len(str(new_values['data']['description'])) > 0: |
|
| 217 | description = str.strip(new_values['data']['description']) |
|
| 218 | else: |
|
| 219 | description = None |
|
| 220 | ||
| 221 | if 'latitude_point_id' in new_values['data'].keys() and \ |
|
| 222 | isinstance(new_values['data']['latitude_point_id'], int) and \ |
|
| 223 | new_values['data']['latitude_point_id'] > 0: |
|
| 224 | latitude_point_id = new_values['data']['latitude_point_id'] |
|
| 225 | else: |
|
| 226 | latitude_point_id = None |
|
| 227 | ||
| 228 | if 'longitude_point_id' in new_values['data'].keys() and \ |
|
| 229 | isinstance(new_values['data']['longitude_point_id'], int) and \ |
|
| 230 | new_values['data']['longitude_point_id'] > 0: |
|
| 231 | longitude_point_id = new_values['data']['longitude_point_id'] |
|
| 232 | else: |
|
| 233 | longitude_point_id = None |
|
| 234 | ||
| 235 | if 'svg2_id' in new_values['data'].keys() and \ |
|
| 236 | isinstance(new_values['data']['svg2_id'], int) and \ |
|
| 237 | new_values['data']['svg2_id'] > 0: |
|
| 238 | svg2_id = new_values['data']['svg2_id'] |
|
| 239 | else: |
|
| 240 | svg2_id = None |
|
| 241 | ||
| 242 | if 'svg3_id' in new_values['data'].keys() and \ |
|
| 243 | isinstance(new_values['data']['svg3_id'], int) and \ |
|
| 244 | new_values['data']['svg3_id'] > 0: |
|
| 245 | svg3_id = new_values['data']['svg3_id'] |
|
| 246 | else: |
|
| 247 | svg3_id = None |
|
| 248 | ||
| 249 | if 'svg4_id' in new_values['data'].keys() and \ |
|
| 250 | isinstance(new_values['data']['svg4_id'], int) and \ |
|
| 251 | new_values['data']['svg4_id'] > 0: |
|
| 252 | svg4_id = new_values['data']['svg4_id'] |
|
| 253 | else: |
|
| 254 | svg4_id = None |
|
| 255 | ||
| 256 | if 'svg5_id' in new_values['data'].keys() and \ |
|
| 257 | isinstance(new_values['data']['svg5_id'], int) and \ |
|
| 258 | new_values['data']['svg5_id'] > 0: |
|
| 259 | svg5_id = new_values['data']['svg5_id'] |
|
| 260 | else: |
|
| 261 | svg5_id = None |
|
| 262 | ||
| 263 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 264 | cursor = cnx.cursor() |
|
| 265 | ||
| 266 | cursor.execute(" SELECT name " |
|
| 267 | " FROM tbl_energy_storage_power_stations " |
|
| 268 | " WHERE name = %s ", (name,)) |
|
| 269 | if cursor.fetchone() is not None: |
|
| 270 | cursor.close() |
|
| 271 | cnx.close() |
|
| 272 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 273 | description='API.ENERGY_STORAGE_POWER_STATION_NAME_IS_ALREADY_IN_USE') |
|
| 274 | ||
| 275 | cursor.execute(" SELECT name " |
|
| 276 | " FROM tbl_contacts " |
|
| 277 | " WHERE id = %s ", |
|
| 278 | (contact_id,)) |
|
| 279 | row = cursor.fetchone() |
|
| 280 | if row is None: |
|
| 281 | cursor.close() |
|
| 282 | cnx.close() |
|
| 283 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 284 | description='API.CONTACT_NOT_FOUND') |
|
| 285 | ||
| 286 | cursor.execute(" SELECT name " |
|
| 287 | " FROM tbl_cost_centers " |
|
| 288 | " WHERE id = %s ", |
|
| 289 | (cost_center_id,)) |
|
| 290 | row = cursor.fetchone() |
|
| 291 | if row is None: |
|
| 292 | cursor.close() |
|
| 293 | cnx.close() |
|
| 294 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 295 | description='API.COST_CENTER_NOT_FOUND') |
|
| 296 | ||
| 297 | cursor.execute(" SELECT name " |
|
| 298 | " FROM tbl_svgs " |
|
| 299 | " WHERE id = %s ", |
|
| 300 | (svg_id,)) |
|
| 301 | row = cursor.fetchone() |
|
| 302 | if row is None: |
|
| 303 | cursor.close() |
|
| 304 | cnx.close() |
|
| 305 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 306 | description='API.SVG_NOT_FOUND') |
|
| 307 | ||
| 308 | add_values = (" INSERT INTO tbl_energy_storage_power_stations " |
|
| 309 | " (name, uuid, address, postal_code, latitude, longitude, rated_capacity, rated_power, " |
|
| 310 | " contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description, " |
|
| 311 | " latitude_point_id, longitude_point_id, svg2_id, svg3_id, svg4_id, svg5_id ) " |
|
| 312 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
|
| 313 | cursor.execute(add_values, (name, |
|
| 314 | str(uuid.uuid4()), |
|
| 315 | address, |
|
| 316 | postal_code, |
|
| 317 | latitude, |
|
| 318 | longitude, |
|
| 319 | rated_capacity, |
|
| 320 | rated_power, |
|
| 321 | contact_id, |
|
| 322 | cost_center_id, |
|
| 323 | svg_id, |
|
| 324 | is_cost_data_displayed, |
|
| 325 | phase_of_lifecycle, |
|
| 326 | description, |
|
| 327 | latitude_point_id, |
|
| 328 | longitude_point_id, |
|
| 329 | svg2_id, |
|
| 330 | svg3_id, |
|
| 331 | svg4_id, |
|
| 332 | svg5_id)) |
|
| 333 | new_id = cursor.lastrowid |
|
| 334 | cnx.commit() |
|
| 335 | cursor.close() |
|
| 336 | cnx.close() |
|
| 337 | ||
| 338 | resp.status = falcon.HTTP_201 |
|
| 339 | resp.location = '/energystoragepowerstations/' + str(new_id) |
|
| 340 | ||
| 341 | ||
| 342 | class EnergyStoragePowerStationItem: |
|