@@ 4953-5060 (lines=108) @@ | ||
4950 | resp.status = falcon.HTTP_204 |
|
4951 | ||
4952 | ||
4953 | class MicrogridUserCollection: |
|
4954 | def __init__(self): |
|
4955 | """Initializes Class""" |
|
4956 | pass |
|
4957 | ||
4958 | @staticmethod |
|
4959 | def on_options(req, resp, id_): |
|
4960 | resp.status = falcon.HTTP_200 |
|
4961 | ||
4962 | @staticmethod |
|
4963 | def on_get(req, resp, id_): |
|
4964 | access_control(req) |
|
4965 | if not id_.isdigit() or int(id_) <= 0: |
|
4966 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4967 | description='API.INVALID_MICROGRID_ID') |
|
4968 | ||
4969 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
4970 | cursor = cnx.cursor() |
|
4971 | cursor.execute(" SELECT name " |
|
4972 | " FROM tbl_microgrids " |
|
4973 | " WHERE id = %s ", (id_,)) |
|
4974 | if cursor.fetchone() is None: |
|
4975 | cursor.close() |
|
4976 | cnx.close() |
|
4977 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4978 | description='API.MICROGRID_NOT_FOUND') |
|
4979 | ||
4980 | query = (" SELECT u.id, u.name, u.uuid " |
|
4981 | " FROM tbl_microgrids m, tbl_microgrids_users mu, " |
|
4982 | + config.myems_user_db['database'] + ".tbl_users u " |
|
4983 | " WHERE mu.microgrid_id = m.id AND u.id = mu.user_id AND m.id = %s " |
|
4984 | " ORDER BY u.id ") |
|
4985 | cursor.execute(query, (id_,)) |
|
4986 | rows = cursor.fetchall() |
|
4987 | result = list() |
|
4988 | if rows is not None and len(rows) > 0: |
|
4989 | for row in rows: |
|
4990 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
4991 | result.append(meta_result) |
|
4992 | ||
4993 | cursor.close() |
|
4994 | cnx.close() |
|
4995 | resp.text = json.dumps(result) |
|
4996 | ||
4997 | @staticmethod |
|
4998 | @user_logger |
|
4999 | def on_post(req, resp, id_): |
|
5000 | """Handles POST requests""" |
|
5001 | admin_control(req) |
|
5002 | try: |
|
5003 | raw_json = req.stream.read().decode('utf-8') |
|
5004 | except Exception as ex: |
|
5005 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
5006 | title='API.BAD_REQUEST', |
|
5007 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
5008 | ||
5009 | if not id_.isdigit() or int(id_) <= 0: |
|
5010 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5011 | description='API.INVALID_MICROGRID_ID') |
|
5012 | ||
5013 | new_values = json.loads(raw_json) |
|
5014 | if 'user_id' not in new_values['data'].keys() or \ |
|
5015 | not isinstance(new_values['data']['user_id'], int) or \ |
|
5016 | new_values['data']['user_id'] <= 0: |
|
5017 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
5018 | description='API.INVALID_USER_ID') |
|
5019 | user_id = new_values['data']['user_id'] |
|
5020 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
5021 | cursor = cnx.cursor() |
|
5022 | cursor.execute(" SELECT name " |
|
5023 | " from tbl_microgrids " |
|
5024 | " WHERE id = %s ", (id_,)) |
|
5025 | if cursor.fetchone() is None: |
|
5026 | cursor.close() |
|
5027 | cnx.close() |
|
5028 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5029 | description='API.MICROGRID_NOT_FOUND') |
|
5030 | ||
5031 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
|
5032 | cursor_user = cnx_user.cursor() |
|
5033 | cursor_user.execute(" SELECT name" |
|
5034 | " FROM tbl_users " |
|
5035 | " WHERE id = %s ", (user_id,)) |
|
5036 | if cursor_user.fetchone() is None: |
|
5037 | cursor_user.close() |
|
5038 | cnx_user.close() |
|
5039 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
5040 | description='API.USER_NOT_FOUND') |
|
5041 | query = (" SELECT id " |
|
5042 | " FROM tbl_microgrids_users " |
|
5043 | " WHERE microgrid_id = %s AND user_id = %s") |
|
5044 | cursor.execute(query, (id_, user_id,)) |
|
5045 | if cursor.fetchone() is not None: |
|
5046 | cursor.close() |
|
5047 | cnx.close() |
|
5048 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
5049 | description='API.MICROGRID_USER_RELATION_EXISTS') |
|
5050 | add_row = (" INSERT INTO tbl_microgrids_users (microgrid_id, user_id) " |
|
5051 | " VALUES (%s, %s) ") |
|
5052 | cursor.execute(add_row, (id_, user_id,)) |
|
5053 | cnx.commit() |
|
5054 | cursor.close() |
|
5055 | cnx.close() |
|
5056 | cursor_user.close() |
|
5057 | cnx_user.close() |
|
5058 | ||
5059 | resp.status = falcon.HTTP_201 |
|
5060 | resp.location = '/microgrids/' + str(id_) + '/users/' + str(user_id) |
|
5061 | ||
5062 | ||
5063 | class MicrogridUserItem: |
@@ 4769-4876 (lines=108) @@ | ||
4766 | resp.status = falcon.HTTP_200 |
|
4767 | ||
4768 | ||
4769 | class PhotovoltaicPowerStationUserCollection: |
|
4770 | def __init__(self): |
|
4771 | """Initializes Class""" |
|
4772 | pass |
|
4773 | ||
4774 | @staticmethod |
|
4775 | def on_options(req, resp, id_): |
|
4776 | resp.status = falcon.HTTP_200 |
|
4777 | ||
4778 | @staticmethod |
|
4779 | def on_get(req, resp, id_): |
|
4780 | access_control(req) |
|
4781 | if not id_.isdigit() or int(id_) <= 0: |
|
4782 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4783 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
4784 | ||
4785 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
4786 | cursor = cnx.cursor() |
|
4787 | cursor.execute(" SELECT name " |
|
4788 | " FROM tbl_photovoltaic_power_stations " |
|
4789 | " WHERE id = %s ", (id_,)) |
|
4790 | if cursor.fetchone() is None: |
|
4791 | cursor.close() |
|
4792 | cnx.close() |
|
4793 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4794 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
4795 | ||
4796 | query = (" SELECT u.id, u.name, u.uuid " |
|
4797 | " FROM tbl_photovoltaic_power_stations m, tbl_photovoltaic_power_stations_users mu, " |
|
4798 | + config.myems_user_db['database'] + ".tbl_users u " |
|
4799 | " WHERE mu.photovoltaic_power_station_id = m.id AND u.id = mu.user_id AND m.id = %s " |
|
4800 | " ORDER BY u.id ") |
|
4801 | cursor.execute(query, (id_,)) |
|
4802 | rows = cursor.fetchall() |
|
4803 | result = list() |
|
4804 | if rows is not None and len(rows) > 0: |
|
4805 | for row in rows: |
|
4806 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
4807 | result.append(meta_result) |
|
4808 | ||
4809 | cursor.close() |
|
4810 | cnx.close() |
|
4811 | resp.text = json.dumps(result) |
|
4812 | ||
4813 | @staticmethod |
|
4814 | @user_logger |
|
4815 | def on_post(req, resp, id_): |
|
4816 | """Handles POST requests""" |
|
4817 | admin_control(req) |
|
4818 | try: |
|
4819 | raw_json = req.stream.read().decode('utf-8') |
|
4820 | except Exception as ex: |
|
4821 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
4822 | title='API.BAD_REQUEST', |
|
4823 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
4824 | ||
4825 | if not id_.isdigit() or int(id_) <= 0: |
|
4826 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4827 | description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID') |
|
4828 | ||
4829 | new_values = json.loads(raw_json) |
|
4830 | if 'user_id' not in new_values['data'].keys() or \ |
|
4831 | not isinstance(new_values['data']['user_id'], int) or \ |
|
4832 | new_values['data']['user_id'] <= 0: |
|
4833 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
4834 | description='API.INVALID_USER_ID') |
|
4835 | user_id = new_values['data']['user_id'] |
|
4836 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
4837 | cursor = cnx.cursor() |
|
4838 | cursor.execute(" SELECT name " |
|
4839 | " FROM tbl_photovoltaic_power_stations " |
|
4840 | " WHERE id = %s ", (id_,)) |
|
4841 | if cursor.fetchone() is None: |
|
4842 | cursor.close() |
|
4843 | cnx.close() |
|
4844 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4845 | description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND') |
|
4846 | ||
4847 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
|
4848 | cursor_user = cnx_user.cursor() |
|
4849 | cursor_user.execute(" SELECT name" |
|
4850 | " FROM tbl_users " |
|
4851 | " WHERE id = %s ", (user_id,)) |
|
4852 | if cursor_user.fetchone() is None: |
|
4853 | cursor_user.close() |
|
4854 | cnx_user.close() |
|
4855 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
4856 | description='API.USER_NOT_FOUND') |
|
4857 | query = (" SELECT id " |
|
4858 | " FROM tbl_photovoltaic_power_stations_users " |
|
4859 | " WHERE photovoltaic_power_station_id = %s AND user_id = %s") |
|
4860 | cursor.execute(query, (id_, user_id,)) |
|
4861 | if cursor.fetchone() is not None: |
|
4862 | cursor.close() |
|
4863 | cnx.close() |
|
4864 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
4865 | description='API.PHOTOVOLTAIC_POWER_STATION_USER_RELATION_EXISTS') |
|
4866 | add_row = (" INSERT INTO tbl_photovoltaic_power_stations_users (photovoltaic_power_station_id, user_id) " |
|
4867 | " VALUES (%s, %s) ") |
|
4868 | cursor.execute(add_row, (id_, user_id,)) |
|
4869 | cnx.commit() |
|
4870 | cursor.close() |
|
4871 | cnx.close() |
|
4872 | cursor_user.close() |
|
4873 | cnx_user.close() |
|
4874 | ||
4875 | resp.status = falcon.HTTP_201 |
|
4876 | resp.location = '/photovoltaicpowerstations/' + str(id_) + '/users/' + str(user_id) |
|
4877 | ||
4878 | ||
4879 | class PhotovoltaicPowerStationUserItem: |
@@ 879-986 (lines=108) @@ | ||
876 | resp.status = falcon.HTTP_204 |
|
877 | ||
878 | ||
879 | class EnergyStoragePowerStationUserCollection: |
|
880 | def __init__(self): |
|
881 | """Initializes Class""" |
|
882 | pass |
|
883 | ||
884 | @staticmethod |
|
885 | def on_options(req, resp, id_): |
|
886 | resp.status = falcon.HTTP_200 |
|
887 | ||
888 | @staticmethod |
|
889 | def on_get(req, resp, id_): |
|
890 | access_control(req) |
|
891 | if not id_.isdigit() or int(id_) <= 0: |
|
892 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
893 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
|
894 | ||
895 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
896 | cursor = cnx.cursor() |
|
897 | cursor.execute(" SELECT name " |
|
898 | " FROM tbl_energy_storage_power_stations " |
|
899 | " WHERE id = %s ", (id_,)) |
|
900 | if cursor.fetchone() is None: |
|
901 | cursor.close() |
|
902 | cnx.close() |
|
903 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
904 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
|
905 | ||
906 | query = (" SELECT u.id, u.name, u.uuid " |
|
907 | " FROM tbl_energy_storage_power_stations m, tbl_energy_storage_power_stations_users mu, " |
|
908 | + config.myems_user_db['database'] + ".tbl_users u " |
|
909 | " WHERE mu.energy_storage_power_station_id = m.id AND u.id = mu.user_id AND m.id = %s " |
|
910 | " ORDER BY u.id ") |
|
911 | cursor.execute(query, (id_,)) |
|
912 | rows = cursor.fetchall() |
|
913 | result = list() |
|
914 | if rows is not None and len(rows) > 0: |
|
915 | for row in rows: |
|
916 | meta_result = {"id": row[0], "name": row[1], "uuid": row[2]} |
|
917 | result.append(meta_result) |
|
918 | ||
919 | cursor.close() |
|
920 | cnx.close() |
|
921 | resp.text = json.dumps(result) |
|
922 | ||
923 | @staticmethod |
|
924 | @user_logger |
|
925 | def on_post(req, resp, id_): |
|
926 | """Handles POST requests""" |
|
927 | admin_control(req) |
|
928 | try: |
|
929 | raw_json = req.stream.read().decode('utf-8') |
|
930 | except Exception as ex: |
|
931 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
932 | title='API.BAD_REQUEST', |
|
933 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
934 | ||
935 | if not id_.isdigit() or int(id_) <= 0: |
|
936 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
937 | description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID') |
|
938 | ||
939 | new_values = json.loads(raw_json) |
|
940 | if 'user_id' not in new_values['data'].keys() or \ |
|
941 | not isinstance(new_values['data']['user_id'], int) or \ |
|
942 | new_values['data']['user_id'] <= 0: |
|
943 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
944 | description='API.INVALID_USER_ID') |
|
945 | user_id = new_values['data']['user_id'] |
|
946 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
947 | cursor = cnx.cursor() |
|
948 | cursor.execute(" SELECT name " |
|
949 | " FROM tbl_energy_storage_power_stations " |
|
950 | " WHERE id = %s ", (id_,)) |
|
951 | if cursor.fetchone() is None: |
|
952 | cursor.close() |
|
953 | cnx.close() |
|
954 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
955 | description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND') |
|
956 | ||
957 | cnx_user = mysql.connector.connect(**config.myems_user_db) |
|
958 | cursor_user = cnx_user.cursor() |
|
959 | cursor_user.execute(" SELECT name" |
|
960 | " FROM tbl_users " |
|
961 | " WHERE id = %s ", (user_id,)) |
|
962 | if cursor_user.fetchone() is None: |
|
963 | cursor_user.close() |
|
964 | cnx_user.close() |
|
965 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
966 | description='API.USER_NOT_FOUND') |
|
967 | query = (" SELECT id " |
|
968 | " FROM tbl_energy_storage_power_stations_users " |
|
969 | " WHERE energy_storage_power_station_id = %s AND user_id = %s") |
|
970 | cursor.execute(query, (id_, user_id,)) |
|
971 | if cursor.fetchone() is not None: |
|
972 | cursor.close() |
|
973 | cnx.close() |
|
974 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
975 | description='API.ENERGY_STORAGE_POWER_STATION_USER_RELATION_EXISTS') |
|
976 | add_row = (" INSERT INTO tbl_energy_storage_power_stations_users (energy_storage_power_station_id, user_id) " |
|
977 | " VALUES (%s, %s) ") |
|
978 | cursor.execute(add_row, (id_, user_id,)) |
|
979 | cnx.commit() |
|
980 | cursor.close() |
|
981 | cnx.close() |
|
982 | cursor_user.close() |
|
983 | cnx_user.close() |
|
984 | ||
985 | resp.status = falcon.HTTP_201 |
|
986 | resp.location = '/energystoragepowerstations/' + str(id_) + '/users/' + str(user_id) |
|
987 | ||
988 | ||
989 | class EnergyStoragePowerStationUserItem: |