myems /
myems-api
| 1 | import falcon |
||
| 2 | import simplejson as json |
||
| 3 | import mysql.connector |
||
| 4 | import config |
||
| 5 | import uuid |
||
| 6 | |||
| 7 | |||
| 8 | class EnergyItemCollection: |
||
| 9 | @staticmethod |
||
| 10 | def __init__(): |
||
| 11 | pass |
||
| 12 | |||
| 13 | @staticmethod |
||
| 14 | def on_options(req, resp): |
||
| 15 | resp.status = falcon.HTTP_200 |
||
| 16 | |||
| 17 | @staticmethod |
||
| 18 | def on_get(req, resp): |
||
| 19 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 20 | cursor = cnx.cursor(dictionary=True) |
||
| 21 | |||
| 22 | query = (" SELECT id, name, uuid " |
||
| 23 | " FROM tbl_energy_categories ") |
||
| 24 | cursor.execute(query) |
||
| 25 | rows_energy_categories = cursor.fetchall() |
||
| 26 | |||
| 27 | energy_category_dict = dict() |
||
| 28 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 29 | for row in rows_energy_categories: |
||
| 30 | energy_category_dict[row['id']] = {"id": row['id'], |
||
| 31 | "name": row['name'], |
||
| 32 | "uuid": row['uuid']} |
||
| 33 | |||
| 34 | query = (" SELECT id, name, uuid, energy_category_id " |
||
| 35 | " FROM tbl_energy_items " |
||
| 36 | " ORDER BY id ") |
||
| 37 | cursor.execute(query) |
||
| 38 | rows = cursor.fetchall() |
||
| 39 | cursor.close() |
||
| 40 | cnx.disconnect() |
||
| 41 | |||
| 42 | result = list() |
||
| 43 | if rows is not None and len(rows) > 0: |
||
| 44 | for row in rows: |
||
| 45 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
| 46 | meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], |
||
| 47 | "energy_category": energy_category} |
||
| 48 | result.append(meta_result) |
||
| 49 | |||
| 50 | resp.body = json.dumps(result) |
||
| 51 | |||
| 52 | @staticmethod |
||
| 53 | def on_post(req, resp): |
||
| 54 | """Handles POST requests""" |
||
| 55 | try: |
||
| 56 | raw_json = req.stream.read().decode('utf-8') |
||
| 57 | except Exception as ex: |
||
| 58 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 59 | |||
| 60 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 61 | |||
| 62 | if 'name' not in new_values['data'].keys() or \ |
||
| 63 | not isinstance(new_values['data']['name'], str) or \ |
||
| 64 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 65 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 66 | description='API.INVALID_ENERGY_ITEM_NAME') |
||
| 67 | |||
| 68 | name = str.strip(new_values['data']['name']) |
||
| 69 | |||
| 70 | if 'energy_category_id' not in new_values['data'].keys() or \ |
||
| 71 | new_values['data']['energy_category_id'] <= 0: |
||
| 72 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 73 | description='API.INVALID_ENERGY_CATEGORY_ID') |
||
| 74 | energy_category_id = new_values['data']['energy_category_id'] |
||
| 75 | |||
| 76 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 77 | cursor = cnx.cursor() |
||
| 78 | |||
| 79 | cursor.execute(" SELECT name " |
||
| 80 | " FROM tbl_energy_items " |
||
| 81 | " WHERE name = %s ", (name,)) |
||
| 82 | if cursor.fetchone() is not None: |
||
| 83 | cursor.close() |
||
| 84 | cnx.disconnect() |
||
| 85 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 86 | description='API.ENERGY_ITEM_NAME_IS_ALREADY_IN_USE') |
||
| 87 | |||
| 88 | cursor.execute(" SELECT name " |
||
| 89 | " FROM tbl_energy_categories " |
||
| 90 | " WHERE id = %s ", |
||
| 91 | (energy_category_id,)) |
||
| 92 | if cursor.fetchone() is None: |
||
| 93 | cursor.close() |
||
| 94 | cnx.disconnect() |
||
| 95 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 96 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
||
| 97 | |||
| 98 | add_value = (" INSERT INTO tbl_energy_items " |
||
| 99 | " (name, uuid, energy_category_id) " |
||
| 100 | " VALUES (%s, %s, %s) ") |
||
| 101 | cursor.execute(add_value, (name, |
||
| 102 | str(uuid.uuid4()), |
||
| 103 | energy_category_id)) |
||
| 104 | new_id = cursor.lastrowid |
||
| 105 | cnx.commit() |
||
| 106 | cursor.close() |
||
| 107 | cnx.disconnect() |
||
| 108 | |||
| 109 | resp.status = falcon.HTTP_201 |
||
| 110 | resp.location = '/energyitems/' + str(new_id) |
||
| 111 | |||
| 112 | |||
| 113 | class EnergyItemItem: |
||
| 114 | @staticmethod |
||
| 115 | def __init__(): |
||
| 116 | pass |
||
| 117 | |||
| 118 | @staticmethod |
||
| 119 | def on_options(req, resp, id_): |
||
| 120 | resp.status = falcon.HTTP_200 |
||
| 121 | |||
| 122 | @staticmethod |
||
| 123 | def on_get(req, resp, id_): |
||
| 124 | if not id_.isdigit() or int(id_) <= 0: |
||
| 125 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 126 | description='API.INVALID_ENERGY_ITEM_ID') |
||
| 127 | |||
| 128 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 129 | cursor = cnx.cursor(dictionary=True) |
||
| 130 | |||
| 131 | query = (" SELECT id, name, uuid " |
||
| 132 | " FROM tbl_energy_categories ") |
||
| 133 | cursor.execute(query) |
||
| 134 | rows_energy_categories = cursor.fetchall() |
||
| 135 | |||
| 136 | energy_category_dict = dict() |
||
| 137 | if rows_energy_categories is not None and len(rows_energy_categories) > 0: |
||
| 138 | for row in rows_energy_categories: |
||
| 139 | energy_category_dict[row['id']] = {"id": row['id'], |
||
| 140 | "name": row['name'], |
||
| 141 | "uuid": row['uuid']} |
||
| 142 | |||
| 143 | query = (" SELECT id, name, uuid, energy_category_id " |
||
| 144 | " FROM tbl_energy_items " |
||
| 145 | " WHERE id =%s ") |
||
| 146 | cursor.execute(query, (id_,)) |
||
| 147 | row = cursor.fetchone() |
||
| 148 | cursor.close() |
||
| 149 | cnx.disconnect() |
||
| 150 | if row is None: |
||
| 151 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 152 | description='API.ENERGY_ITEM_NOT_FOUND') |
||
| 153 | |||
| 154 | energy_category = energy_category_dict.get(row['energy_category_id'], None) |
||
| 155 | result = {"id": row['id'], |
||
| 156 | "name": row['name'], |
||
| 157 | "uuid": row['uuid'], |
||
| 158 | "energy_category": energy_category} |
||
| 159 | resp.body = json.dumps(result) |
||
| 160 | |||
| 161 | View Code Duplication | @staticmethod |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 162 | def on_delete(req, resp, id_): |
||
| 163 | if not id_.isdigit() or int(id_) <= 0: |
||
| 164 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 165 | description='API.INVALID_ENERGY_ITEM_ID') |
||
| 166 | |||
| 167 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 168 | cursor = cnx.cursor() |
||
| 169 | |||
| 170 | cursor.execute(" SELECT name " |
||
| 171 | " FROM tbl_energy_items " |
||
| 172 | " WHERE id = %s ", (id_,)) |
||
| 173 | if cursor.fetchone() is None: |
||
| 174 | cursor.close() |
||
| 175 | cnx.disconnect() |
||
| 176 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 177 | description='API.ENERGY_ITEM_NOT_FOUND') |
||
| 178 | |||
| 179 | cursor.execute(" SELECT id " |
||
| 180 | " FROM tbl_meters " |
||
| 181 | " WHERE energy_item_id = %s ", (id_,)) |
||
| 182 | rows_meters = cursor.fetchall() |
||
| 183 | if rows_meters is not None and len(rows_meters) > 0: |
||
| 184 | cursor.close() |
||
| 185 | cnx.disconnect() |
||
| 186 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 187 | title='API.BAD_REQUEST', |
||
| 188 | description='API.ENERGY_ITEM_USED_IN_METER') |
||
| 189 | |||
| 190 | cursor.execute(" SELECT id " |
||
| 191 | " FROM tbl_virtual_meters " |
||
| 192 | " WHERE energy_item_id = %s ", (id_,)) |
||
| 193 | rows_virtual_meters = cursor.fetchall() |
||
| 194 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
| 195 | cursor.close() |
||
| 196 | cnx.disconnect() |
||
| 197 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 198 | title='API.BAD_REQUEST', |
||
| 199 | description='API.ENERGY_ITEM_USED_IN_VIRTUAL_METER') |
||
| 200 | |||
| 201 | cursor.execute(" SELECT id " |
||
| 202 | " FROM tbl_offline_meters " |
||
| 203 | " WHERE energy_item_id = %s ", (id_,)) |
||
| 204 | rows_offline_meters = cursor.fetchall() |
||
| 205 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
| 206 | cursor.close() |
||
| 207 | cnx.disconnect() |
||
| 208 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 209 | title='API.BAD_REQUEST', |
||
| 210 | description='API.ENERGY_ITEM_USED_IN_OFFLINE_METER') |
||
| 211 | |||
| 212 | cursor.execute(" DELETE FROM tbl_energy_items WHERE id = %s ", (id_,)) |
||
| 213 | cnx.commit() |
||
| 214 | |||
| 215 | cursor.close() |
||
| 216 | cnx.disconnect() |
||
| 217 | resp.status = falcon.HTTP_204 |
||
| 218 | |||
| 219 | @staticmethod |
||
| 220 | def on_put(req, resp, id_): |
||
| 221 | """Handles PUT requests""" |
||
| 222 | try: |
||
| 223 | raw_json = req.stream.read().decode('utf-8') |
||
| 224 | except Exception as ex: |
||
| 225 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 226 | |||
| 227 | if not id_.isdigit() or int(id_) <= 0: |
||
| 228 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 229 | description='API.INVALID_ENERGY_ITEM_ID') |
||
| 230 | |||
| 231 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 232 | if 'name' not in new_values['data'].keys() or \ |
||
| 233 | not isinstance(new_values['data']['name'], str) or \ |
||
| 234 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 235 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 236 | description='API.INVALID_ENERGY_ITEM_NAME') |
||
| 237 | |||
| 238 | name = str.strip(new_values['data']['name']) |
||
| 239 | |||
| 240 | if 'energy_category_id' not in new_values['data'].keys() or new_values['data']['energy_category_id'] <= 0: |
||
| 241 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 242 | description='API.INVALID_ENERGY_CATEGORY_ID') |
||
| 243 | energy_category_id = new_values['data']['energy_category_id'] |
||
| 244 | |||
| 245 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 246 | cursor = cnx.cursor() |
||
| 247 | |||
| 248 | cursor.execute(" SELECT name " |
||
| 249 | " FROM tbl_energy_items " |
||
| 250 | " WHERE id = %s ", (id_,)) |
||
| 251 | if cursor.fetchone() is None: |
||
| 252 | cursor.close() |
||
| 253 | cnx.disconnect() |
||
| 254 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 255 | description='API.ENERGY_ITEM_NOT_FOUND') |
||
| 256 | |||
| 257 | cursor.execute(" SELECT name " |
||
| 258 | " FROM tbl_energy_items " |
||
| 259 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 260 | if cursor.fetchone() is not None: |
||
| 261 | cursor.close() |
||
| 262 | cnx.disconnect() |
||
| 263 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 264 | description='API.ENERGY_ITEM_NAME_IS_ALREADY_IN_USE') |
||
| 265 | |||
| 266 | cursor.execute(" SELECT name " |
||
| 267 | " FROM tbl_energy_categories " |
||
| 268 | " WHERE id = %s ", |
||
| 269 | (energy_category_id,)) |
||
| 270 | if cursor.fetchone() is None: |
||
| 271 | cursor.close() |
||
| 272 | cnx.disconnect() |
||
| 273 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 274 | description='API.ENERGY_CATEGORY_NOT_FOUND') |
||
| 275 | |||
| 276 | update_row = (" UPDATE tbl_energy_items " |
||
| 277 | " SET name = %s, energy_category_id = %s " |
||
| 278 | " WHERE id = %s ") |
||
| 279 | cursor.execute(update_row, (name, |
||
| 280 | energy_category_id, |
||
| 281 | id_,)) |
||
| 282 | cnx.commit() |
||
| 283 | cursor.close() |
||
| 284 | cnx.disconnect() |
||
| 285 | resp.status = falcon.HTTP_200 |
||
| 286 | |||
| 287 |