import falcon import simplejson as json import mysql.connector import config import uuid from datetime import datetime, timedelta, timezone from core.useractivity import user_logger, access_control class TenantCollection: @staticmethod def __init__(): """"Initializes TenantCollection""" pass @staticmethod def on_options(req, resp): resp.status = falcon.HTTP_200 @staticmethod def on_get(req, resp): cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) query = (" SELECT id, name, uuid " " FROM tbl_tenant_types ") cursor.execute(query) rows_tenant_types = cursor.fetchall() tenant_type_dict = dict() if rows_tenant_types is not None and len(rows_tenant_types) > 0: for row in rows_tenant_types: tenant_type_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid " " FROM tbl_contacts ") cursor.execute(query) rows_contacts = cursor.fetchall() contact_dict = dict() if rows_contacts is not None and len(rows_contacts) > 0: for row in rows_contacts: contact_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid " " FROM tbl_cost_centers ") cursor.execute(query) rows_cost_centers = cursor.fetchall() cost_center_dict = dict() if rows_cost_centers is not None and len(rows_cost_centers) > 0: for row in rows_cost_centers: cost_center_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid, " " buildings, floors, rooms, area, tenant_type_id, " " is_input_counted, is_key_tenant, " " lease_number, lease_start_datetime_utc, lease_end_datetime_utc, is_in_lease, " " contact_id, cost_center_id, description " " FROM tbl_tenants " " ORDER BY id ") cursor.execute(query) rows_spaces = cursor.fetchall() timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset result = list() if rows_spaces is not None and len(rows_spaces) > 0: for row in rows_spaces: tenant_type = tenant_type_dict.get(row['tenant_type_id'], None) contact = contact_dict.get(row['contact_id'], None) cost_center = cost_center_dict.get(row['cost_center_id'], None) lease_start_datetime_local = row['lease_start_datetime_utc'].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) lease_end_datetime_local = row['lease_end_datetime_utc'].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "buildings": row['buildings'], "floors": row['floors'], "rooms": row['rooms'], "area": row['area'], "tenant_type": tenant_type, "is_input_counted": bool(row['is_input_counted']), "is_key_tenant": bool(row['is_key_tenant']), "lease_number": row['lease_number'], "lease_start_datetime": lease_start_datetime_local.strftime('%Y-%m-%dT%H:%M:%S'), "lease_end_datetime": lease_end_datetime_local.strftime('%Y-%m-%dT%H:%M:%S'), "is_in_lease": bool(row['is_in_lease']), "contact": contact, "cost_center": cost_center, "description": row['description'], "qrcode": 'tenant:' + row['uuid']} result.append(meta_result) cursor.close() cnx.close() resp.text = json.dumps(result) @staticmethod @user_logger def on_post(req, resp): """Handles POST requests""" access_control(req) try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) new_values = json.loads(raw_json) if 'name' not in new_values['data'].keys() or \ not isinstance(new_values['data']['name'], str) or \ len(str.strip(new_values['data']['name'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_NAME') name = str.strip(new_values['data']['name']) if 'buildings' not in new_values['data'].keys() or \ not isinstance(new_values['data']['buildings'], str) or \ len(str.strip(new_values['data']['buildings'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_BUILDINGS_VALUE') buildings = str.strip(new_values['data']['buildings']) if 'floors' not in new_values['data'].keys() or \ not isinstance(new_values['data']['floors'], str) or \ len(str.strip(new_values['data']['floors'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_FLOORS_VALUE') floors = str.strip(new_values['data']['floors']) if 'rooms' not in new_values['data'].keys() or \ not isinstance(new_values['data']['rooms'], str) or \ len(str.strip(new_values['data']['rooms'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_ROOMS_VALUE') rooms = str.strip(new_values['data']['rooms']) if 'area' not in new_values['data'].keys() or \ not (isinstance(new_values['data']['area'], float) or isinstance(new_values['data']['area'], int)) or \ new_values['data']['area'] <= 0.0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_AREA_VALUE') area = new_values['data']['area'] if 'tenant_type_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['tenant_type_id'], int) or \ new_values['data']['tenant_type_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_TYPE_ID') tenant_type_id = new_values['data']['tenant_type_id'] if 'is_input_counted' not in new_values['data'].keys() or \ not isinstance(new_values['data']['is_input_counted'], bool): raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_IS_INPUT_COUNTED_VALUE') is_input_counted = new_values['data']['is_input_counted'] if 'is_key_tenant' not in new_values['data'].keys() or \ not isinstance(new_values['data']['is_key_tenant'], bool): raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_IS_KEY_TENANT_VALUE') is_key_tenant = new_values['data']['is_key_tenant'] if 'lease_number' not in new_values['data'].keys() or \ not isinstance(new_values['data']['lease_number'], str) or \ len(str.strip(new_values['data']['lease_number'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_LEASE_NUMBER_VALUE') lease_number = str.strip(new_values['data']['lease_number']) if 'is_in_lease' not in new_values['data'].keys() or \ not isinstance(new_values['data']['is_in_lease'], bool): raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_IS_IN_LEASE_VALUE') is_in_lease = new_values['data']['is_in_lease'] timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset # todo: validate datetime values lease_start_datetime_utc = datetime.strptime(new_values['data']['lease_start_datetime'], '%Y-%m-%dT%H:%M:%S') lease_start_datetime_utc = lease_start_datetime_utc.replace(tzinfo=timezone.utc) lease_start_datetime_utc -= timedelta(minutes=timezone_offset) lease_end_datetime_utc = datetime.strptime(new_values['data']['lease_end_datetime'], '%Y-%m-%dT%H:%M:%S') lease_end_datetime_utc = lease_end_datetime_utc.replace(tzinfo=timezone.utc) lease_end_datetime_utc -= timedelta(minutes=timezone_offset) if 'contact_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['contact_id'], int) or \ new_values['data']['contact_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_CONTACT_ID') contact_id = new_values['data']['contact_id'] if 'cost_center_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['cost_center_id'], int) or \ new_values['data']['cost_center_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_COST_CENTER_ID') cost_center_id = new_values['data']['cost_center_id'] if 'description' in new_values['data'].keys() and \ new_values['data']['description'] is not None and \ len(str(new_values['data']['description'])) > 0: description = str.strip(new_values['data']['description']) else: description = None cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE name = %s ", (name,)) if cursor.fetchone() is not None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', description='API.TENANT_NAME_IS_ALREADY_IN_USE') cursor.execute(" SELECT name " " FROM tbl_tenant_types " " WHERE id = %s ", (tenant_type_id,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_TYPE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_contacts " " WHERE id = %s ", (new_values['data']['contact_id'],)) row = cursor.fetchone() if row is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.CONTACT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_cost_centers " " WHERE id = %s ", (new_values['data']['cost_center_id'],)) row = cursor.fetchone() if row is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.COST_CENTER_NOT_FOUND') add_values = (" INSERT INTO tbl_tenants " " (name, uuid, buildings, floors, rooms, area, tenant_type_id, " " is_input_counted, is_key_tenant, " " lease_number, lease_start_datetime_utc, lease_end_datetime_utc, is_in_lease, " " contact_id, cost_center_id, description) " " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") cursor.execute(add_values, (name, str(uuid.uuid4()), buildings, floors, rooms, area, tenant_type_id, is_input_counted, is_key_tenant, lease_number, lease_start_datetime_utc, lease_end_datetime_utc, is_in_lease, contact_id, cost_center_id, description)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_201 resp.location = '/tenants/' + str(new_id) class TenantItem: @staticmethod def __init__(): """"Initializes TenantItem""" pass @staticmethod def on_options(req, resp, id_): resp.status = falcon.HTTP_200 @staticmethod def on_get(req, resp, id_): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) query = (" SELECT id, name, uuid " " FROM tbl_tenant_types ") cursor.execute(query) rows_tenant_types = cursor.fetchall() tenant_type_dict = dict() if rows_tenant_types is not None and len(rows_tenant_types) > 0: for row in rows_tenant_types: tenant_type_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid " " FROM tbl_contacts ") cursor.execute(query) rows_contacts = cursor.fetchall() contact_dict = dict() if rows_contacts is not None and len(rows_contacts) > 0: for row in rows_contacts: contact_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid " " FROM tbl_cost_centers ") cursor.execute(query) rows_cost_centers = cursor.fetchall() cost_center_dict = dict() if rows_cost_centers is not None and len(rows_cost_centers) > 0: for row in rows_cost_centers: cost_center_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT id, name, uuid, " " buildings, floors, rooms, area, tenant_type_id," " is_key_tenant, is_input_counted, " " lease_number, lease_start_datetime_utc, lease_end_datetime_utc, is_in_lease, " " contact_id, cost_center_id, description " " FROM tbl_tenants " " WHERE id = %s ") cursor.execute(query, (id_,)) row = cursor.fetchone() cursor.close() cnx.close() if row is None: raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') else: tenant_type = tenant_type_dict.get(row['tenant_type_id'], None) contact = contact_dict.get(row['contact_id'], None) cost_center = cost_center_dict.get(row['cost_center_id'], None) timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset lease_start_datetime_local = row['lease_start_datetime_utc'].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) lease_end_datetime_local = row['lease_end_datetime_utc'].replace(tzinfo=timezone.utc) + \ timedelta(minutes=timezone_offset) meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "buildings": row['buildings'], "floors": row['floors'], "rooms": row['rooms'], "area": row['area'], "tenant_type": tenant_type, "is_input_counted": bool(row['is_input_counted']), "is_key_tenant": bool(row['is_key_tenant']), "lease_number": row['lease_number'], "lease_start_datetime": lease_start_datetime_local.strftime('%Y-%m-%dT%H:%M:%S'), "lease_end_datetime": lease_end_datetime_local.strftime('%Y-%m-%dT%H:%M:%S'), "is_in_lease": bool(row['is_in_lease']), "contact": contact, "cost_center": cost_center, "description": row['description'], "qrcode": 'tenant:' + row['uuid']} resp.text = json.dumps(meta_result) @staticmethod @user_logger def on_delete(req, resp, id_): access_control(req) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') # check relation with space cursor.execute(" SELECT space_id " " FROM tbl_spaces_tenants " " WHERE tenant_id = %s ", (id_,)) rows_spaces = cursor.fetchall() if rows_spaces is not None and len(rows_spaces) > 0: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_SPACES') # check relation with meter cursor.execute(" SELECT meter_id " " FROM tbl_tenants_meters " " WHERE tenant_id = %s ", (id_,)) rows_meters = cursor.fetchall() if rows_meters is not None and len(rows_meters) > 0: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_METERS') # check relation with offline meter cursor.execute(" SELECT offline_meter_id " " FROM tbl_tenants_offline_meters " " WHERE tenant_id = %s ", (id_,)) rows_offline_meters = cursor.fetchall() if rows_offline_meters is not None and len(rows_offline_meters) > 0: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS') # check relation with points cursor.execute(" SELECT point_id " " FROM tbl_tenants_points " " WHERE tenant_id = %s ", (id_,)) rows_points = cursor.fetchall() if rows_points is not None and len(rows_points) > 0: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_POINTS') # check relation with sensor cursor.execute(" SELECT sensor_id " " FROM tbl_tenants_sensors " " WHERE tenant_id = %s ", (id_,)) rows_sensors = cursor.fetchall() if rows_sensors is not None and len(rows_sensors) > 0: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_SENSORS') # check relation with virtual meter cursor.execute(" SELECT virtual_meter_id " " FROM tbl_tenants_virtual_meters " " WHERE tenant_id = %s ", (id_,)) rows_virtual_meters = cursor.fetchall() if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.THERE_IS_RELATION_WITH_VIRTUAL_METER') cursor.execute(" DELETE FROM tbl_tenants WHERE id = %s ", (id_,)) cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_204 @staticmethod @user_logger def on_put(req, resp, id_): """Handles PUT requests""" access_control(req) try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') new_values = json.loads(raw_json) if 'name' not in new_values['data'].keys() or \ not isinstance(new_values['data']['name'], str) or \ len(str.strip(new_values['data']['name'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_NAME') name = str.strip(new_values['data']['name']) if 'buildings' not in new_values['data'].keys() or \ not isinstance(new_values['data']['buildings'], str) or \ len(str.strip(new_values['data']['buildings'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_BUILDINGS_VALUE') buildings = str.strip(new_values['data']['buildings']) if 'floors' not in new_values['data'].keys() or \ not isinstance(new_values['data']['floors'], str) or \ len(str.strip(new_values['data']['floors'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_FLOORS_VALUE') floors = str.strip(new_values['data']['floors']) if 'rooms' not in new_values['data'].keys() or \ not isinstance(new_values['data']['rooms'], str) or \ len(str.strip(new_values['data']['rooms'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_ROOMS_VALUE') rooms = str.strip(new_values['data']['rooms']) if 'area' not in new_values['data'].keys() or \ not (isinstance(new_values['data']['area'], float) or isinstance(new_values['data']['area'], int)) or \ new_values['data']['area'] <= 0.0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_AREA_VALUE') area = new_values['data']['area'] if 'tenant_type_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['tenant_type_id'], int) or \ new_values['data']['tenant_type_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_TYPE_ID') tenant_type_id = new_values['data']['tenant_type_id'] if 'is_input_counted' not in new_values['data'].keys() or \ not isinstance(new_values['data']['is_input_counted'], bool): raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_IS_INPUT_COUNTED_VALUE') is_input_counted = new_values['data']['is_input_counted'] if 'is_key_tenant' not in new_values['data'].keys() or \ not isinstance(new_values['data']['is_key_tenant'], bool): raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_IS_KEY_TENANT_VALUE') is_key_tenant = new_values['data']['is_key_tenant'] if 'lease_number' not in new_values['data'].keys() or \ not isinstance(new_values['data']['lease_number'], str) or \ len(str.strip(new_values['data']['lease_number'])) == 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_LEASE_NUMBER_VALUE') lease_number = str.strip(new_values['data']['lease_number']) if 'is_in_lease' not in new_values['data'].keys() or \ not isinstance(new_values['data']['is_in_lease'], bool): raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_IS_IN_LEASE_VALUE') is_in_lease = new_values['data']['is_in_lease'] timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) if config.utc_offset[0] == '-': timezone_offset = -timezone_offset # todo: validate datetime values lease_start_datetime_utc = datetime.strptime(new_values['data']['lease_start_datetime'], '%Y-%m-%dT%H:%M:%S') lease_start_datetime_utc = lease_start_datetime_utc.replace(tzinfo=timezone.utc) lease_start_datetime_utc -= timedelta(minutes=timezone_offset) lease_end_datetime_utc = datetime.strptime(new_values['data']['lease_end_datetime'], '%Y-%m-%dT%H:%M:%S') lease_end_datetime_utc = lease_end_datetime_utc.replace(tzinfo=timezone.utc) lease_end_datetime_utc -= timedelta(minutes=timezone_offset) if 'contact_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['contact_id'], int) or \ new_values['data']['contact_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_CONTACT_ID') contact_id = new_values['data']['contact_id'] if 'cost_center_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['cost_center_id'], int) or \ new_values['data']['cost_center_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_COST_CENTER_ID') cost_center_id = new_values['data']['cost_center_id'] if 'description' in new_values['data'].keys() and \ new_values['data']['description'] is not None and \ len(str(new_values['data']['description'])) > 0: description = str.strip(new_values['data']['description']) else: description = None cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE name = %s AND id != %s ", (name, id_)) if cursor.fetchone() is not None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', description='API.TENANT_NAME_IS_ALREADY_IN_USE') cursor.execute(" SELECT name " " FROM tbl_tenant_types " " WHERE id = %s ", (tenant_type_id,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_TYPE_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_contacts " " WHERE id = %s ", (new_values['data']['contact_id'],)) row = cursor.fetchone() if row is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.CONTACT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_cost_centers " " WHERE id = %s ", (new_values['data']['cost_center_id'],)) row = cursor.fetchone() if row is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.COST_CENTER_NOT_FOUND') update_row = (" UPDATE tbl_tenants " " SET name = %s, buildings = %s, floors = %s, rooms = %s, area = %s, " " tenant_type_id = %s, is_input_counted = %s, " " is_key_tenant = %s, lease_number = %s, lease_start_datetime_utc = %s, " " lease_end_datetime_utc = %s, is_in_lease = %s, contact_id = %s, cost_center_id = %s, " " description = %s " " WHERE id = %s ") cursor.execute(update_row, (name, buildings, floors, rooms, area, tenant_type_id, is_input_counted, is_key_tenant, lease_number, lease_start_datetime_utc, lease_end_datetime_utc, is_in_lease, contact_id, cost_center_id, description, id_)) cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_200 class TenantMeterCollection: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_): resp.status = falcon.HTTP_200 @staticmethod def on_get(req, resp, id_): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') query = (" SELECT id, name, uuid " " FROM tbl_energy_categories ") cursor.execute(query) rows_energy_categories = cursor.fetchall() energy_category_dict = dict() if rows_energy_categories is not None and len(rows_energy_categories) > 0: for row in rows_energy_categories: energy_category_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " " FROM tbl_tenants t, tbl_tenants_meters tm, tbl_meters m " " WHERE tm.tenant_id = t.id AND m.id = tm.meter_id AND t.id = %s " " ORDER BY m.id ") cursor.execute(query, (id_,)) rows = cursor.fetchall() result = list() if rows is not None and len(rows) > 0: for row in rows: energy_category = energy_category_dict.get(row['energy_category_id'], None) meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "energy_category": energy_category} result.append(meta_result) resp.text = json.dumps(result) @staticmethod @user_logger def on_post(req, resp, id_): """Handles POST requests""" access_control(req) try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') new_values = json.loads(raw_json) if 'meter_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['meter_id'], int) or \ new_values['data']['meter_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') meter_id = new_values['data']['meter_id'] cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_meters " " WHERE id = %s ", (meter_id,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND') query = (" SELECT id " " FROM tbl_tenants_meters " " WHERE tenant_id = %s AND meter_id = %s") cursor.execute(query, (id_, meter_id,)) if cursor.fetchone() is not None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.TENANT_METER_RELATION_EXISTS') add_row = (" INSERT INTO tbl_tenants_meters (tenant_id, meter_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, meter_id,)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_201 resp.location = '/tenants/' + str(id_) + '/meters/' + str(meter_id) class TenantMeterItem: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_, mid): resp.status = falcon.HTTP_200 @staticmethod @user_logger def on_delete(req, resp, id_, mid): access_control(req) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') if not mid.isdigit() or int(mid) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_METER_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_meters " " WHERE id = %s ", (mid,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.METER_NOT_FOUND') cursor.execute(" SELECT id " " FROM tbl_tenants_meters " " WHERE tenant_id = %s AND meter_id = %s ", (id_, mid)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_METER_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_tenants_meters WHERE tenant_id = %s AND meter_id = %s ", (id_, mid)) cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_204 class TenantOfflineMeterCollection: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_): resp.status = falcon.HTTP_200 @staticmethod def on_get(req, resp, id_): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') query = (" SELECT id, name, uuid " " FROM tbl_energy_categories ") cursor.execute(query) rows_energy_categories = cursor.fetchall() energy_category_dict = dict() if rows_energy_categories is not None and len(rows_energy_categories) > 0: for row in rows_energy_categories: energy_category_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " " FROM tbl_tenants s, tbl_tenants_offline_meters sm, tbl_offline_meters m " " WHERE sm.tenant_id = s.id AND m.id = sm.offline_meter_id AND s.id = %s " " ORDER BY m.id ") cursor.execute(query, (id_,)) rows = cursor.fetchall() result = list() if rows is not None and len(rows) > 0: for row in rows: energy_category = energy_category_dict.get(row['energy_category_id'], None) meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "energy_category": energy_category} result.append(meta_result) resp.text = json.dumps(result) @staticmethod @user_logger def on_post(req, resp, id_): """Handles POST requests""" access_control(req) try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') new_values = json.loads(raw_json) if 'offline_meter_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['offline_meter_id'], int) or \ new_values['data']['offline_meter_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_OFFLINE_METER_ID') offline_meter_id = new_values['data']['offline_meter_id'] cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_offline_meters " " WHERE id = %s ", (offline_meter_id,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.OFFLINE_METER_NOT_FOUND') query = (" SELECT id " " FROM tbl_tenants_offline_meters " " WHERE tenant_id = %s AND offline_meter_id = %s") cursor.execute(query, (id_, offline_meter_id,)) if cursor.fetchone() is not None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.TENANT_OFFLINE_METER_RELATION_EXISTS') add_row = (" INSERT INTO tbl_tenants_offline_meters (tenant_id, offline_meter_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, offline_meter_id,)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_201 resp.location = '/tenants/' + str(id_) + '/offlinemeters/' + str(offline_meter_id) class TenantOfflineMeterItem: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_, mid): resp.status = falcon.HTTP_200 @staticmethod @user_logger def on_delete(req, resp, id_, mid): access_control(req) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') if not mid.isdigit() or int(mid) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_OFFLINE_METER_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_offline_meters " " WHERE id = %s ", (mid,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.OFFLINE_METER_NOT_FOUND') cursor.execute(" SELECT id " " FROM tbl_tenants_offline_meters " " WHERE tenant_id = %s AND offline_meter_id = %s ", (id_, mid)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_OFFLINE_METER_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_tenants_offline_meters " " WHERE tenant_id = %s AND offline_meter_id = %s ", (id_, mid)) cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_204 class TenantPointCollection: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_): resp.status = falcon.HTTP_200 @staticmethod def on_get(req, resp, id_): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') query = (" SELECT id, name, uuid " " FROM tbl_data_sources ") cursor.execute(query) rows_data_sources = cursor.fetchall() data_source_dict = dict() if rows_data_sources is not None and len(rows_data_sources) > 0: for row in rows_data_sources: data_source_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT p.id, p.name, p.data_source_id " " FROM tbl_tenants t, tbl_tenants_points tp, tbl_points p " " WHERE tp.tenant_id = t.id AND p.id = tp.point_id AND t.id = %s " " ORDER BY p.id ") cursor.execute(query, (id_,)) rows = cursor.fetchall() result = list() if rows is not None and len(rows) > 0: for row in rows: data_source = data_source_dict.get(row['data_source_id'], None) meta_result = {"id": row['id'], "name": row['name'], "data_source": data_source} result.append(meta_result) resp.text = json.dumps(result) @staticmethod @user_logger def on_post(req, resp, id_): """Handles POST requests""" access_control(req) try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') new_values = json.loads(raw_json) if 'point_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['point_id'], int) or \ new_values['data']['point_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_POINT_ID') point_id = new_values['data']['point_id'] cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_points " " WHERE id = %s ", (point_id,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.POINT_NOT_FOUND') query = (" SELECT id " " FROM tbl_tenants_points " " WHERE tenant_id = %s AND point_id = %s") cursor.execute(query, (id_, point_id,)) if cursor.fetchone() is not None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.TENANT_POINT_RELATION_EXISTS') add_row = (" INSERT INTO tbl_tenants_points (tenant_id, point_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, point_id,)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_201 resp.location = '/tenants/' + str(id_) + '/points/' + str(point_id) class TenantPointItem: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_, pid): resp.status = falcon.HTTP_200 @staticmethod @user_logger def on_delete(req, resp, id_, pid): access_control(req) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') if not pid.isdigit() or int(pid) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_POINT_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_points " " WHERE id = %s ", (pid,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.POINT_NOT_FOUND') cursor.execute(" SELECT id " " FROM tbl_tenants_points " " WHERE tenant_id = %s AND point_id = %s ", (id_, pid)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_POINT_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_tenants_points " " WHERE tenant_id = %s AND point_id = %s ", (id_, pid)) cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_204 class TenantSensorCollection: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_): resp.status = falcon.HTTP_200 @staticmethod def on_get(req, resp, id_): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') query = (" SELECT s.id, s.name, s.uuid " " FROM tbl_tenants t, tbl_tenants_sensors ts, tbl_sensors s " " WHERE ts.tenant_id = t.id AND s.id = ts.sensor_id AND t.id = %s " " ORDER BY s.id ") cursor.execute(query, (id_,)) rows = cursor.fetchall() result = list() if rows is not None and len(rows) > 0: for row in rows: meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} result.append(meta_result) resp.text = json.dumps(result) @staticmethod @user_logger def on_post(req, resp, id_): """Handles POST requests""" access_control(req) try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') new_values = json.loads(raw_json) if 'sensor_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['sensor_id'], int) or \ new_values['data']['sensor_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_ID') sensor_id = new_values['data']['sensor_id'] cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_sensors " " WHERE id = %s ", (sensor_id,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SENSOR_NOT_FOUND') query = (" SELECT id " " FROM tbl_tenants_sensors " " WHERE tenant_id = %s AND sensor_id = %s") cursor.execute(query, (id_, sensor_id,)) if cursor.fetchone() is not None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.TENANT_SENSOR_RELATION_EXISTS') add_row = (" INSERT INTO tbl_tenants_sensors (tenant_id, sensor_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, sensor_id,)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_201 resp.location = '/tenants/' + str(id_) + '/sensors/' + str(sensor_id) class TenantSensorItem: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_, sid): resp.status = falcon.HTTP_200 @staticmethod @user_logger def on_delete(req, resp, id_, sid): access_control(req) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') if not sid.isdigit() or int(sid) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SENSOR_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_sensors " " WHERE id = %s ", (sid,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.SENSOR_NOT_FOUND') cursor.execute(" SELECT id " " FROM tbl_tenants_sensors " " WHERE tenant_id = %s AND sensor_id = %s ", (id_, sid)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_SENSOR_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_tenants_sensors WHERE tenant_id = %s AND sensor_id = %s ", (id_, sid)) cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_204 class TenantVirtualMeterCollection: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_): resp.status = falcon.HTTP_200 @staticmethod def on_get(req, resp, id_): if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor(dictionary=True) cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') query = (" SELECT id, name, uuid " " FROM tbl_energy_categories ") cursor.execute(query) rows_energy_categories = cursor.fetchall() energy_category_dict = dict() if rows_energy_categories is not None and len(rows_energy_categories) > 0: for row in rows_energy_categories: energy_category_dict[row['id']] = {"id": row['id'], "name": row['name'], "uuid": row['uuid']} query = (" SELECT m.id, m.name, m.uuid, m.energy_category_id " " FROM tbl_tenants t, tbl_tenants_virtual_meters tm, tbl_virtual_meters m " " WHERE tm.tenant_id = t.id AND m.id = tm.virtual_meter_id AND t.id = %s " " ORDER BY m.id ") cursor.execute(query, (id_,)) rows = cursor.fetchall() result = list() if rows is not None and len(rows) > 0: for row in rows: energy_category = energy_category_dict.get(row['energy_category_id'], None) meta_result = {"id": row['id'], "name": row['name'], "uuid": row['uuid'], "energy_category": energy_category} result.append(meta_result) resp.text = json.dumps(result) @staticmethod @user_logger def on_post(req, resp, id_): """Handles POST requests""" access_control(req) try: raw_json = req.stream.read().decode('utf-8') except Exception as ex: raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') new_values = json.loads(raw_json) if 'virtual_meter_id' not in new_values['data'].keys() or \ not isinstance(new_values['data']['virtual_meter_id'], int) or \ new_values['data']['virtual_meter_id'] <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_VIRTUAL_METER_ID') virtual_meter_id = new_values['data']['virtual_meter_id'] cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " from tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_virtual_meters " " WHERE id = %s ", (virtual_meter_id,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.VIRTUAL_METER_NOT_FOUND') query = (" SELECT id " " FROM tbl_tenants_virtual_meters " " WHERE tenant_id = %s AND virtual_meter_id = %s") cursor.execute(query, (id_, virtual_meter_id,)) if cursor.fetchone() is not None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description='API.TENANT_VIRTUAL_METER_RELATION_EXISTS') add_row = (" INSERT INTO tbl_tenants_virtual_meters (tenant_id, virtual_meter_id) " " VALUES (%s, %s) ") cursor.execute(add_row, (id_, virtual_meter_id,)) new_id = cursor.lastrowid cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_201 resp.location = '/tenants/' + str(id_) + '/virtualmeters/' + str(virtual_meter_id) class TenantVirtualMeterItem: @staticmethod def __init__(): """Initializes Class""" pass @staticmethod def on_options(req, resp, id_, mid): resp.status = falcon.HTTP_200 @staticmethod @user_logger def on_delete(req, resp, id_, mid): access_control(req) if not id_.isdigit() or int(id_) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_TENANT_ID') if not mid.isdigit() or int(mid) <= 0: raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_VIRTUAL_METER_ID') cnx = mysql.connector.connect(**config.myems_system_db) cursor = cnx.cursor() cursor.execute(" SELECT name " " FROM tbl_tenants " " WHERE id = %s ", (id_,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_NOT_FOUND') cursor.execute(" SELECT name " " FROM tbl_virtual_meters " " WHERE id = %s ", (mid,)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.VIRTUAL_METER_NOT_FOUND') cursor.execute(" SELECT id " " FROM tbl_tenants_virtual_meters " " WHERE tenant_id = %s AND virtual_meter_id = %s ", (id_, mid)) if cursor.fetchone() is None: cursor.close() cnx.close() raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', description='API.TENANT_VIRTUAL_METER_RELATION_NOT_FOUND') cursor.execute(" DELETE FROM tbl_tenants_virtual_meters " " WHERE tenant_id = %s AND virtual_meter_id = %s ", (id_, mid)) cnx.commit() cursor.close() cnx.close() resp.status = falcon.HTTP_204