Code Duplication    Length = 119-119 lines in 3 locations

myems-api/core/energystoragecontainer.py 3 locations

@@ 2737-2855 (lines=119) @@
2734
        resp.status = falcon.HTTP_200
2735
2736
2737
class EnergyStorageContainerHVACCollection:
2738
    def __init__(self):
2739
        """Initializes Class"""
2740
        pass
2741
2742
    @staticmethod
2743
    def on_options(req, resp, id_):
2744
        resp.status = falcon.HTTP_200
2745
2746
    @staticmethod
2747
    def on_get(req, resp, id_):
2748
        access_control(req)
2749
        if not id_.isdigit() or int(id_) <= 0:
2750
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2751
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2752
2753
        cnx = mysql.connector.connect(**config.myems_system_db)
2754
        cursor = cnx.cursor()
2755
2756
        cursor.execute(" SELECT name "
2757
                       " FROM tbl_energy_storage_containers "
2758
                       " WHERE id = %s ", (id_,))
2759
        if cursor.fetchone() is None:
2760
            cursor.close()
2761
            cnx.close()
2762
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2763
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2764
2765
        query = (" SELECT id, name, uuid "
2766
                 " FROM tbl_energy_storage_containers_hvacs "
2767
                 " WHERE energy_storage_container_id = %s "
2768
                 " ORDER BY name ")
2769
        cursor.execute(query, (id_,))
2770
        rows = cursor.fetchall()
2771
2772
        result = list()
2773
        if rows is not None and len(rows) > 0:
2774
            for row in rows:
2775
                meta_result = {"id": row[0],
2776
                               "name": row[1],
2777
                               "uuid": row[2]
2778
                               }
2779
                result.append(meta_result)
2780
2781
        resp.text = json.dumps(result)
2782
2783
    @staticmethod
2784
    @user_logger
2785
    def on_post(req, resp, id_):
2786
        """Handles POST requests"""
2787
        admin_control(req)
2788
        try:
2789
            raw_json = req.stream.read().decode('utf-8')
2790
        except Exception as ex:
2791
            raise falcon.HTTPError(status=falcon.HTTP_400,
2792
                                   title='API.BAD_REQUEST',
2793
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2794
        if not id_.isdigit() or int(id_) <= 0:
2795
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2796
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2797
2798
        cnx = mysql.connector.connect(**config.myems_system_db)
2799
        cursor = cnx.cursor()
2800
2801
        cursor.execute(" SELECT name "
2802
                       " FROM tbl_energy_storage_containers "
2803
                       " WHERE id = %s ", (id_,))
2804
        if cursor.fetchone() is None:
2805
            cursor.close()
2806
            cnx.close()
2807
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2808
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2809
2810
        new_values = json.loads(raw_json)
2811
2812
        if 'name' not in new_values['data'].keys() or \
2813
                not isinstance(new_values['data']['name'], str) or \
2814
                len(str.strip(new_values['data']['name'])) == 0:
2815
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2816
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME')
2817
        name = str.strip(new_values['data']['name'])
2818
2819
        cnx = mysql.connector.connect(**config.myems_system_db)
2820
        cursor = cnx.cursor()
2821
2822
        cursor.execute(" SELECT name "
2823
                       " FROM tbl_energy_storage_containers "
2824
                       " WHERE id = %s ",
2825
                       (id_,))
2826
        if cursor.fetchone() is None:
2827
            cursor.close()
2828
            cnx.close()
2829
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2830
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2831
2832
        cursor.execute(" SELECT name "
2833
                       " FROM tbl_energy_storage_containers_hvacs "
2834
                       " WHERE energy_storage_container_id = %s AND name = %s ",
2835
                       (id_, name,))
2836
        if cursor.fetchone() is not None:
2837
            cursor.close()
2838
            cnx.close()
2839
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2840
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE')
2841
2842
        add_values = (" INSERT INTO tbl_energy_storage_containers_hvacs "
2843
                      "    (name, uuid, energy_storage_container_id) "
2844
                      " VALUES (%s, %s, %s) ")
2845
        cursor.execute(add_values, (name,
2846
                                    str(uuid.uuid4()),
2847
                                    id_
2848
                                    ))
2849
        new_id = cursor.lastrowid
2850
        cnx.commit()
2851
        cursor.close()
2852
        cnx.close()
2853
2854
        resp.status = falcon.HTTP_201
2855
        resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(new_id)
2856
2857
2858
class EnergyStorageContainerHVACItem:
@@ 1960-2078 (lines=119) @@
1957
        resp.status = falcon.HTTP_200
1958
1959
1960
class EnergyStorageContainerFirecontrolCollection:
1961
    def __init__(self):
1962
        """Initializes Class"""
1963
        pass
1964
1965
    @staticmethod
1966
    def on_options(req, resp, id_):
1967
        resp.status = falcon.HTTP_200
1968
1969
    @staticmethod
1970
    def on_get(req, resp, id_):
1971
        access_control(req)
1972
        if not id_.isdigit() or int(id_) <= 0:
1973
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1974
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1975
1976
        cnx = mysql.connector.connect(**config.myems_system_db)
1977
        cursor = cnx.cursor()
1978
1979
        cursor.execute(" SELECT name "
1980
                       " FROM tbl_energy_storage_containers "
1981
                       " WHERE id = %s ", (id_,))
1982
        if cursor.fetchone() is None:
1983
            cursor.close()
1984
            cnx.close()
1985
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1986
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1987
1988
        query = (" SELECT id, name, uuid "
1989
                 " FROM tbl_energy_storage_containers_firecontrols "
1990
                 " WHERE energy_storage_container_id = %s "
1991
                 " ORDER BY name ")
1992
        cursor.execute(query, (id_,))
1993
        rows = cursor.fetchall()
1994
1995
        result = list()
1996
        if rows is not None and len(rows) > 0:
1997
            for row in rows:
1998
                meta_result = {"id": row[0],
1999
                               "name": row[1],
2000
                               "uuid": row[2]
2001
                               }
2002
                result.append(meta_result)
2003
2004
        resp.text = json.dumps(result)
2005
2006
    @staticmethod
2007
    @user_logger
2008
    def on_post(req, resp, id_):
2009
        """Handles POST requests"""
2010
        admin_control(req)
2011
        try:
2012
            raw_json = req.stream.read().decode('utf-8')
2013
        except Exception as ex:
2014
            raise falcon.HTTPError(status=falcon.HTTP_400,
2015
                                   title='API.BAD_REQUEST',
2016
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2017
        if not id_.isdigit() or int(id_) <= 0:
2018
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2019
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2020
2021
        cnx = mysql.connector.connect(**config.myems_system_db)
2022
        cursor = cnx.cursor()
2023
2024
        cursor.execute(" SELECT name "
2025
                       " FROM tbl_energy_storage_containers "
2026
                       " WHERE id = %s ", (id_,))
2027
        if cursor.fetchone() is None:
2028
            cursor.close()
2029
            cnx.close()
2030
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2031
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2032
2033
        new_values = json.loads(raw_json)
2034
2035
        if 'name' not in new_values['data'].keys() or \
2036
                not isinstance(new_values['data']['name'], str) or \
2037
                len(str.strip(new_values['data']['name'])) == 0:
2038
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2039
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME')
2040
        name = str.strip(new_values['data']['name'])
2041
2042
        cnx = mysql.connector.connect(**config.myems_system_db)
2043
        cursor = cnx.cursor()
2044
2045
        cursor.execute(" SELECT name "
2046
                       " FROM tbl_energy_storage_containers "
2047
                       " WHERE id = %s ",
2048
                       (id_,))
2049
        if cursor.fetchone() is None:
2050
            cursor.close()
2051
            cnx.close()
2052
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2053
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2054
2055
        cursor.execute(" SELECT name "
2056
                       " FROM tbl_energy_storage_containers_firecontrols "
2057
                       " WHERE energy_storage_container_id = %s AND name = %s ",
2058
                       (id_, name,))
2059
        if cursor.fetchone() is not None:
2060
            cursor.close()
2061
            cnx.close()
2062
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2063
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE')
2064
2065
        add_values = (" INSERT INTO tbl_energy_storage_containers_firecontrols "
2066
                      "    (name, uuid, energy_storage_container_id) "
2067
                      " VALUES (%s, %s, %s) ")
2068
        cursor.execute(add_values, (name,
2069
                                    str(uuid.uuid4()),
2070
                                    id_
2071
                                    ))
2072
        new_id = cursor.lastrowid
2073
        cnx.commit()
2074
        cursor.close()
2075
        cnx.close()
2076
2077
        resp.status = falcon.HTTP_201
2078
        resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(new_id)
2079
2080
2081
class EnergyStorageContainerFirecontrolItem:
@@ 1664-1782 (lines=119) @@
1661
        resp.status = falcon.HTTP_204
1662
1663
1664
class EnergyStorageContainerDCDCCollection:
1665
    def __init__(self):
1666
        """Initializes Class"""
1667
        pass
1668
1669
    @staticmethod
1670
    def on_options(req, resp, id_):
1671
        resp.status = falcon.HTTP_200
1672
1673
    @staticmethod
1674
    def on_get(req, resp, id_):
1675
        access_control(req)
1676
        if not id_.isdigit() or int(id_) <= 0:
1677
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1678
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1679
1680
        cnx = mysql.connector.connect(**config.myems_system_db)
1681
        cursor = cnx.cursor()
1682
1683
        cursor.execute(" SELECT name "
1684
                       " FROM tbl_energy_storage_containers "
1685
                       " WHERE id = %s ", (id_,))
1686
        if cursor.fetchone() is None:
1687
            cursor.close()
1688
            cnx.close()
1689
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1690
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1691
1692
        query = (" SELECT id, name, uuid "
1693
                 " FROM tbl_energy_storage_containers_dcdcs "
1694
                 " WHERE energy_storage_container_id = %s "
1695
                 " ORDER BY name ")
1696
        cursor.execute(query, (id_,))
1697
        rows = cursor.fetchall()
1698
1699
        result = list()
1700
        if rows is not None and len(rows) > 0:
1701
            for row in rows:
1702
                meta_result = {"id": row[0],
1703
                               "name": row[1],
1704
                               "uuid": row[2]
1705
                               }
1706
                result.append(meta_result)
1707
1708
        resp.text = json.dumps(result)
1709
1710
    @staticmethod
1711
    @user_logger
1712
    def on_post(req, resp, id_):
1713
        """Handles POST requests"""
1714
        admin_control(req)
1715
        try:
1716
            raw_json = req.stream.read().decode('utf-8')
1717
        except Exception as ex:
1718
            raise falcon.HTTPError(status=falcon.HTTP_400,
1719
                                   title='API.BAD_REQUEST',
1720
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1721
        if not id_.isdigit() or int(id_) <= 0:
1722
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1723
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1724
1725
        cnx = mysql.connector.connect(**config.myems_system_db)
1726
        cursor = cnx.cursor()
1727
1728
        cursor.execute(" SELECT name "
1729
                       " FROM tbl_energy_storage_containers "
1730
                       " WHERE id = %s ", (id_,))
1731
        if cursor.fetchone() is None:
1732
            cursor.close()
1733
            cnx.close()
1734
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1735
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1736
1737
        new_values = json.loads(raw_json)
1738
1739
        if 'name' not in new_values['data'].keys() or \
1740
                not isinstance(new_values['data']['name'], str) or \
1741
                len(str.strip(new_values['data']['name'])) == 0:
1742
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1743
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_NAME')
1744
        name = str.strip(new_values['data']['name'])
1745
1746
        cnx = mysql.connector.connect(**config.myems_system_db)
1747
        cursor = cnx.cursor()
1748
1749
        cursor.execute(" SELECT name "
1750
                       " FROM tbl_energy_storage_containers "
1751
                       " WHERE id = %s ",
1752
                       (id_,))
1753
        if cursor.fetchone() is None:
1754
            cursor.close()
1755
            cnx.close()
1756
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1757
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1758
1759
        cursor.execute(" SELECT name "
1760
                       " FROM tbl_energy_storage_containers_dcdcs "
1761
                       " WHERE energy_storage_container_id = %s AND name = %s ",
1762
                       (id_, name,))
1763
        if cursor.fetchone() is not None:
1764
            cursor.close()
1765
            cnx.close()
1766
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1767
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NAME_IS_ALREADY_IN_USE')
1768
1769
        add_values = (" INSERT INTO tbl_energy_storage_containers_dcdcs "
1770
                      "    (name, uuid, energy_storage_container_id) "
1771
                      " VALUES (%s, %s, %s) ")
1772
        cursor.execute(add_values, (name,
1773
                                    str(uuid.uuid4()),
1774
                                    id_
1775
                                    ))
1776
        new_id = cursor.lastrowid
1777
        cnx.commit()
1778
        cursor.close()
1779
        cnx.close()
1780
1781
        resp.status = falcon.HTTP_201
1782
        resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(new_id)
1783
1784
1785
class EnergyStorageContainerDCDCItem: