Code Duplication    Length = 104-104 lines in 11 locations

space.py 6 locations

@@ 2147-2250 (lines=104) @@
2144
        resp.status = falcon.HTTP_204
2145
2146
2147
class SpaceTenantCollection:
2148
    @staticmethod
2149
    def __init__():
2150
        pass
2151
2152
    @staticmethod
2153
    def on_options(req, resp, id_):
2154
        resp.status = falcon.HTTP_200
2155
2156
    @staticmethod
2157
    def on_get(req, resp, id_):
2158
        if not id_.isdigit() or int(id_) <= 0:
2159
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2160
                                   description='API.INVALID_SPACE_ID')
2161
2162
        cnx = mysql.connector.connect(**config.myems_system_db)
2163
        cursor = cnx.cursor()
2164
2165
        cursor.execute(" SELECT name "
2166
                       " FROM tbl_spaces "
2167
                       " WHERE id = %s ", (id_,))
2168
        if cursor.fetchone() is None:
2169
            cursor.close()
2170
            cnx.disconnect()
2171
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2172
                                   description='API.SPACE_NOT_FOUND')
2173
2174
        query = (" SELECT t.id, t.name, t.uuid "
2175
                 " FROM tbl_spaces s, tbl_spaces_tenants st, tbl_tenants t "
2176
                 " WHERE st.space_id = s.id AND t.id = st.tenant_id AND s.id = %s "
2177
                 " ORDER BY t.id ")
2178
        cursor.execute(query, (id_,))
2179
        rows = cursor.fetchall()
2180
2181
        result = list()
2182
        if rows is not None and len(rows) > 0:
2183
            for row in rows:
2184
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
2185
                result.append(meta_result)
2186
2187
        resp.body = json.dumps(result)
2188
2189
    @staticmethod
2190
    def on_post(req, resp, id_):
2191
        """Handles POST requests"""
2192
        try:
2193
            raw_json = req.stream.read().decode('utf-8')
2194
        except Exception as ex:
2195
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
2196
2197
        if not id_.isdigit() or int(id_) <= 0:
2198
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2199
                                   description='API.INVALID_SPACE_ID')
2200
2201
        new_values = json.loads(raw_json, encoding='utf-8')
2202
2203
        if 'tenant_id' not in new_values['data'].keys() or \
2204
                not isinstance(new_values['data']['tenant_id'], int) or \
2205
                new_values['data']['tenant_id'] <= 0:
2206
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2207
                                   description='API.INVALID_TENANT_ID')
2208
        tenant_id = new_values['data']['tenant_id']
2209
2210
        cnx = mysql.connector.connect(**config.myems_system_db)
2211
        cursor = cnx.cursor()
2212
2213
        cursor.execute(" SELECT name "
2214
                       " from tbl_spaces "
2215
                       " WHERE id = %s ", (id_,))
2216
        if cursor.fetchone() is None:
2217
            cursor.close()
2218
            cnx.disconnect()
2219
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2220
                                   description='API.SPACE_NOT_FOUND')
2221
2222
        cursor.execute(" SELECT name "
2223
                       " FROM tbl_tenants "
2224
                       " WHERE id = %s ", (tenant_id,))
2225
        if cursor.fetchone() is None:
2226
            cursor.close()
2227
            cnx.disconnect()
2228
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2229
                                   description='API.TENANT_NOT_FOUND')
2230
2231
        query = (" SELECT id " 
2232
                 " FROM tbl_spaces_tenants "
2233
                 " WHERE space_id = %s AND tenant_id = %s")
2234
        cursor.execute(query, (id_, tenant_id,))
2235
        if cursor.fetchone() is not None:
2236
            cursor.close()
2237
            cnx.disconnect()
2238
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
2239
                                   description='API.SPACE_TENANT_RELATION_EXISTED')
2240
2241
        add_row = (" INSERT INTO tbl_spaces_tenants (space_id, tenant_id) "
2242
                   " VALUES (%s, %s) ")
2243
        cursor.execute(add_row, (id_, tenant_id,))
2244
        new_id = cursor.lastrowid
2245
        cnx.commit()
2246
        cursor.close()
2247
        cnx.disconnect()
2248
2249
        resp.status = falcon.HTTP_201
2250
        resp.location = '/spaces/' + str(id_) + '/tenants/' + str(tenant_id)
2251
2252
2253
class SpaceTenantItem:
@@ 1983-2086 (lines=104) @@
1980
        resp.status = falcon.HTTP_204
1981
1982
1983
class SpaceStoreCollection:
1984
    @staticmethod
1985
    def __init__():
1986
        pass
1987
1988
    @staticmethod
1989
    def on_options(req, resp, id_):
1990
        resp.status = falcon.HTTP_200
1991
1992
    @staticmethod
1993
    def on_get(req, resp, id_):
1994
        if not id_.isdigit() or int(id_) <= 0:
1995
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1996
                                   description='API.INVALID_SPACE_ID')
1997
1998
        cnx = mysql.connector.connect(**config.myems_system_db)
1999
        cursor = cnx.cursor()
2000
2001
        cursor.execute(" SELECT name "
2002
                       " FROM tbl_spaces "
2003
                       " WHERE id = %s ", (id_,))
2004
        if cursor.fetchone() is None:
2005
            cursor.close()
2006
            cnx.disconnect()
2007
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2008
                                   description='API.SPACE_NOT_FOUND')
2009
2010
        query = (" SELECT t.id, t.name, t.uuid "
2011
                 " FROM tbl_spaces s, tbl_spaces_stores st, tbl_stores t "
2012
                 " WHERE st.space_id = s.id AND t.id = st.store_id AND s.id = %s "
2013
                 " ORDER BY t.id ")
2014
        cursor.execute(query, (id_,))
2015
        rows = cursor.fetchall()
2016
2017
        result = list()
2018
        if rows is not None and len(rows) > 0:
2019
            for row in rows:
2020
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
2021
                result.append(meta_result)
2022
2023
        resp.body = json.dumps(result)
2024
2025
    @staticmethod
2026
    def on_post(req, resp, id_):
2027
        """Handles POST requests"""
2028
        try:
2029
            raw_json = req.stream.read().decode('utf-8')
2030
        except Exception as ex:
2031
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
2032
2033
        if not id_.isdigit() or int(id_) <= 0:
2034
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2035
                                   description='API.INVALID_SPACE_ID')
2036
2037
        new_values = json.loads(raw_json, encoding='utf-8')
2038
2039
        if 'store_id' not in new_values['data'].keys() or \
2040
                not isinstance(new_values['data']['store_id'], int) or \
2041
                new_values['data']['store_id'] <= 0:
2042
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
2043
                                   description='API.INVALID_STORE_ID')
2044
        store_id = new_values['data']['store_id']
2045
2046
        cnx = mysql.connector.connect(**config.myems_system_db)
2047
        cursor = cnx.cursor()
2048
2049
        cursor.execute(" SELECT name "
2050
                       " from tbl_spaces "
2051
                       " WHERE id = %s ", (id_,))
2052
        if cursor.fetchone() is None:
2053
            cursor.close()
2054
            cnx.disconnect()
2055
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2056
                                   description='API.SPACE_NOT_FOUND')
2057
2058
        cursor.execute(" SELECT name "
2059
                       " FROM tbl_stores "
2060
                       " WHERE id = %s ", (store_id,))
2061
        if cursor.fetchone() is None:
2062
            cursor.close()
2063
            cnx.disconnect()
2064
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
2065
                                   description='API.STORE_NOT_FOUND')
2066
2067
        query = (" SELECT id " 
2068
                 " FROM tbl_spaces_stores "
2069
                 " WHERE space_id = %s AND store_id = %s")
2070
        cursor.execute(query, (id_, store_id,))
2071
        if cursor.fetchone() is not None:
2072
            cursor.close()
2073
            cnx.disconnect()
2074
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
2075
                                   description='API.SPACE_STORE_RELATION_EXISTED')
2076
2077
        add_row = (" INSERT INTO tbl_spaces_stores (space_id, store_id) "
2078
                   " VALUES (%s, %s) ")
2079
        cursor.execute(add_row, (id_, store_id,))
2080
        new_id = cursor.lastrowid
2081
        cnx.commit()
2082
        cursor.close()
2083
        cnx.disconnect()
2084
2085
        resp.status = falcon.HTTP_201
2086
        resp.location = '/spaces/' + str(id_) + '/stores/' + str(store_id)
2087
2088
2089
class SpaceStoreItem:
@@ 1819-1922 (lines=104) @@
1816
        resp.status = falcon.HTTP_204
1817
1818
1819
class SpaceShopfloorCollection:
1820
    @staticmethod
1821
    def __init__():
1822
        pass
1823
1824
    @staticmethod
1825
    def on_options(req, resp, id_):
1826
        resp.status = falcon.HTTP_200
1827
1828
    @staticmethod
1829
    def on_get(req, resp, id_):
1830
        if not id_.isdigit() or int(id_) <= 0:
1831
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1832
                                   description='API.INVALID_SPACE_ID')
1833
1834
        cnx = mysql.connector.connect(**config.myems_system_db)
1835
        cursor = cnx.cursor()
1836
1837
        cursor.execute(" SELECT name "
1838
                       " FROM tbl_spaces "
1839
                       " WHERE id = %s ", (id_,))
1840
        if cursor.fetchone() is None:
1841
            cursor.close()
1842
            cnx.disconnect()
1843
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1844
                                   description='API.SPACE_NOT_FOUND')
1845
1846
        query = (" SELECT sf.id, sf.name, sf.uuid "
1847
                 " FROM tbl_spaces sp, tbl_spaces_shopfloors ss, tbl_shopfloors sf "
1848
                 " WHERE ss.space_id = sp.id AND sf.id = ss.shopfloor_id AND sp.id = %s "
1849
                 " ORDER BY sf.id ")
1850
        cursor.execute(query, (id_,))
1851
        rows = cursor.fetchall()
1852
1853
        result = list()
1854
        if rows is not None and len(rows) > 0:
1855
            for row in rows:
1856
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1857
                result.append(meta_result)
1858
1859
        resp.body = json.dumps(result)
1860
1861
    @staticmethod
1862
    def on_post(req, resp, id_):
1863
        """Handles POST requests"""
1864
        try:
1865
            raw_json = req.stream.read().decode('utf-8')
1866
        except Exception as ex:
1867
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1868
1869
        if not id_.isdigit() or int(id_) <= 0:
1870
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1871
                                   description='API.INVALID_SPACE_ID')
1872
1873
        new_values = json.loads(raw_json, encoding='utf-8')
1874
1875
        if 'shopfloor_id' not in new_values['data'].keys() or \
1876
                not isinstance(new_values['data']['shopfloor_id'], int) or \
1877
                new_values['data']['shopfloor_id'] <= 0:
1878
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1879
                                   description='API.INVALID_SHOPFLOOR_ID')
1880
        shopfloor_id = new_values['data']['shopfloor_id']
1881
1882
        cnx = mysql.connector.connect(**config.myems_system_db)
1883
        cursor = cnx.cursor()
1884
1885
        cursor.execute(" SELECT name "
1886
                       " from tbl_spaces "
1887
                       " WHERE id = %s ", (id_,))
1888
        if cursor.fetchone() is None:
1889
            cursor.close()
1890
            cnx.disconnect()
1891
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1892
                                   description='API.SPACE_NOT_FOUND')
1893
1894
        cursor.execute(" SELECT name "
1895
                       " FROM tbl_shopfloors "
1896
                       " WHERE id = %s ", (shopfloor_id,))
1897
        if cursor.fetchone() is None:
1898
            cursor.close()
1899
            cnx.disconnect()
1900
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1901
                                   description='API.SHOPFLOOR_NOT_FOUND')
1902
1903
        query = (" SELECT id " 
1904
                 " FROM tbl_spaces_shopfloors "
1905
                 " WHERE space_id = %s AND shopfloor_id = %s")
1906
        cursor.execute(query, (id_, shopfloor_id,))
1907
        if cursor.fetchone() is not None:
1908
            cursor.close()
1909
            cnx.disconnect()
1910
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1911
                                   description='API.SPACE_SHOPFLOOR_RELATION_EXISTED')
1912
1913
        add_row = (" INSERT INTO tbl_spaces_shopfloors (space_id, shopfloor_id) "
1914
                   " VALUES (%s, %s) ")
1915
        cursor.execute(add_row, (id_, shopfloor_id,))
1916
        new_id = cursor.lastrowid
1917
        cnx.commit()
1918
        cursor.close()
1919
        cnx.disconnect()
1920
1921
        resp.status = falcon.HTTP_201
1922
        resp.location = '/spaces/' + str(id_) + '/shopfloors/' + str(shopfloor_id)
1923
1924
1925
class SpaceShopfloorItem:
@@ 1655-1758 (lines=104) @@
1652
        resp.status = falcon.HTTP_204
1653
1654
1655
class SpaceSensorCollection:
1656
    @staticmethod
1657
    def __init__():
1658
        pass
1659
1660
    @staticmethod
1661
    def on_options(req, resp, id_):
1662
        resp.status = falcon.HTTP_200
1663
1664
    @staticmethod
1665
    def on_get(req, resp, id_):
1666
        if not id_.isdigit() or int(id_) <= 0:
1667
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1668
                                   description='API.INVALID_SPACE_ID')
1669
1670
        cnx = mysql.connector.connect(**config.myems_system_db)
1671
        cursor = cnx.cursor()
1672
1673
        cursor.execute(" SELECT name "
1674
                       " FROM tbl_spaces "
1675
                       " WHERE id = %s ", (id_,))
1676
        if cursor.fetchone() is None:
1677
            cursor.close()
1678
            cnx.disconnect()
1679
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1680
                                   description='API.SPACE_NOT_FOUND')
1681
1682
        query = (" SELECT se.id, se.name, se.uuid "
1683
                 " FROM tbl_spaces sp, tbl_spaces_sensors ss, tbl_sensors se "
1684
                 " WHERE ss.space_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s "
1685
                 " ORDER BY se.id ")
1686
        cursor.execute(query, (id_,))
1687
        rows = cursor.fetchall()
1688
1689
        result = list()
1690
        if rows is not None and len(rows) > 0:
1691
            for row in rows:
1692
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1693
                result.append(meta_result)
1694
1695
        resp.body = json.dumps(result)
1696
1697
    @staticmethod
1698
    def on_post(req, resp, id_):
1699
        """Handles POST requests"""
1700
        try:
1701
            raw_json = req.stream.read().decode('utf-8')
1702
        except Exception as ex:
1703
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1704
1705
        if not id_.isdigit() or int(id_) <= 0:
1706
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1707
                                   description='API.INVALID_SPACE_ID')
1708
1709
        new_values = json.loads(raw_json, encoding='utf-8')
1710
1711
        if 'sensor_id' not in new_values['data'].keys() or \
1712
                not isinstance(new_values['data']['sensor_id'], int) or \
1713
                new_values['data']['sensor_id'] <= 0:
1714
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1715
                                   description='API.INVALID_SENSOR_ID')
1716
        sensor_id = new_values['data']['sensor_id']
1717
1718
        cnx = mysql.connector.connect(**config.myems_system_db)
1719
        cursor = cnx.cursor()
1720
1721
        cursor.execute(" SELECT name "
1722
                       " from tbl_spaces "
1723
                       " WHERE id = %s ", (id_,))
1724
        if cursor.fetchone() is None:
1725
            cursor.close()
1726
            cnx.disconnect()
1727
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1728
                                   description='API.SPACE_NOT_FOUND')
1729
1730
        cursor.execute(" SELECT name "
1731
                       " FROM tbl_sensors "
1732
                       " WHERE id = %s ", (sensor_id,))
1733
        if cursor.fetchone() is None:
1734
            cursor.close()
1735
            cnx.disconnect()
1736
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1737
                                   description='API.SENSOR_NOT_FOUND')
1738
1739
        query = (" SELECT id " 
1740
                 " FROM tbl_spaces_sensors "
1741
                 " WHERE space_id = %s AND sensor_id = %s")
1742
        cursor.execute(query, (id_, sensor_id,))
1743
        if cursor.fetchone() is not None:
1744
            cursor.close()
1745
            cnx.disconnect()
1746
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1747
                                   description='API.SPACE_SENSOR_RELATION_EXISTED')
1748
1749
        add_row = (" INSERT INTO tbl_spaces_sensors (space_id, sensor_id) "
1750
                   " VALUES (%s, %s) ")
1751
        cursor.execute(add_row, (id_, sensor_id,))
1752
        new_id = cursor.lastrowid
1753
        cnx.commit()
1754
        cursor.close()
1755
        cnx.disconnect()
1756
1757
        resp.status = falcon.HTTP_201
1758
        resp.location = '/spaces/' + str(id_) + '/sensors/' + str(sensor_id)
1759
1760
1761
class SpaceSensorItem:
@@ 956-1059 (lines=104) @@
953
        resp.status = falcon.HTTP_204
954
955
956
class SpaceEquipmentCollection:
957
    @staticmethod
958
    def __init__():
959
        pass
960
961
    @staticmethod
962
    def on_options(req, resp, id_):
963
        resp.status = falcon.HTTP_200
964
965
    @staticmethod
966
    def on_get(req, resp, id_):
967
        if not id_.isdigit() or int(id_) <= 0:
968
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
969
                                   description='API.INVALID_SPACE_ID')
970
971
        cnx = mysql.connector.connect(**config.myems_system_db)
972
        cursor = cnx.cursor()
973
974
        cursor.execute(" SELECT name "
975
                       " FROM tbl_spaces "
976
                       " WHERE id = %s ", (id_,))
977
        if cursor.fetchone() is None:
978
            cursor.close()
979
            cnx.disconnect()
980
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
981
                                   description='API.SPACE_NOT_FOUND')
982
983
        query = (" SELECT e.id, e.name, e.uuid "
984
                 " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e "
985
                 " WHERE se.space_id = s.id AND e.id = se.equipment_id AND s.id = %s "
986
                 " ORDER BY e.id ")
987
        cursor.execute(query, (id_,))
988
        rows = cursor.fetchall()
989
990
        result = list()
991
        if rows is not None and len(rows) > 0:
992
            for row in rows:
993
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
994
                result.append(meta_result)
995
996
        resp.body = json.dumps(result)
997
998
    @staticmethod
999
    def on_post(req, resp, id_):
1000
        """Handles POST requests"""
1001
        try:
1002
            raw_json = req.stream.read().decode('utf-8')
1003
        except Exception as ex:
1004
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1005
1006
        if not id_.isdigit() or int(id_) <= 0:
1007
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1008
                                   description='API.INVALID_SPACE_ID')
1009
1010
        new_values = json.loads(raw_json, encoding='utf-8')
1011
1012
        if 'equipment_id' not in new_values['data'].keys() or \
1013
                not isinstance(new_values['data']['equipment_id'], int) or \
1014
                new_values['data']['equipment_id'] <= 0:
1015
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1016
                                   description='API.INVALID_EQUIPMENT_ID')
1017
        equipment_id = new_values['data']['equipment_id']
1018
1019
        cnx = mysql.connector.connect(**config.myems_system_db)
1020
        cursor = cnx.cursor()
1021
1022
        cursor.execute(" SELECT name "
1023
                       " from tbl_spaces "
1024
                       " WHERE id = %s ", (id_,))
1025
        if cursor.fetchone() is None:
1026
            cursor.close()
1027
            cnx.disconnect()
1028
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1029
                                   description='API.SPACE_NOT_FOUND')
1030
1031
        cursor.execute(" SELECT name "
1032
                       " FROM tbl_equipments "
1033
                       " WHERE id = %s ", (equipment_id,))
1034
        if cursor.fetchone() is None:
1035
            cursor.close()
1036
            cnx.disconnect()
1037
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1038
                                   description='API.EQUIPMENT_NOT_FOUND')
1039
1040
        query = (" SELECT id " 
1041
                 " FROM tbl_spaces_equipments "
1042
                 " WHERE space_id = %s AND equipment_id = %s")
1043
        cursor.execute(query, (id_, equipment_id,))
1044
        if cursor.fetchone() is not None:
1045
            cursor.close()
1046
            cnx.disconnect()
1047
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1048
                                   description='API.SPACE_EQUIPMENT_RELATION_EXISTED')
1049
1050
        add_row = (" INSERT INTO tbl_spaces_equipments (space_id, equipment_id) "
1051
                   " VALUES (%s, %s) ")
1052
        cursor.execute(add_row, (id_, equipment_id,))
1053
        new_id = cursor.lastrowid
1054
        cnx.commit()
1055
        cursor.close()
1056
        cnx.disconnect()
1057
1058
        resp.status = falcon.HTTP_201
1059
        resp.location = '/spaces/' + str(id_) + '/equipments/' + str(equipment_id)
1060
1061
1062
class SpaceEquipmentItem:
@@ 791-894 (lines=104) @@
788
        resp.body = json.dumps(result)
789
790
791
class SpaceCombinedEquipmentCollection:
792
    @staticmethod
793
    def __init__():
794
        pass
795
796
    @staticmethod
797
    def on_options(req, resp, id_):
798
        resp.status = falcon.HTTP_200
799
800
    @staticmethod
801
    def on_get(req, resp, id_):
802
        if not id_.isdigit() or int(id_) <= 0:
803
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
804
                                   description='API.INVALID_SPACE_ID')
805
806
        cnx = mysql.connector.connect(**config.myems_system_db)
807
        cursor = cnx.cursor()
808
809
        cursor.execute(" SELECT name "
810
                       " FROM tbl_spaces "
811
                       " WHERE id = %s ", (id_,))
812
        if cursor.fetchone() is None:
813
            cursor.close()
814
            cnx.disconnect()
815
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
816
                                   description='API.SPACE_NOT_FOUND')
817
818
        query = (" SELECT e.id, e.name, e.uuid "
819
                 " FROM tbl_spaces s, tbl_spaces_combined_equipments se, tbl_combined_equipments e "
820
                 " WHERE se.space_id = s.id AND e.id = se.combined_equipment_id AND s.id = %s "
821
                 " ORDER BY e.id ")
822
        cursor.execute(query, (id_,))
823
        rows = cursor.fetchall()
824
825
        result = list()
826
        if rows is not None and len(rows) > 0:
827
            for row in rows:
828
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
829
                result.append(meta_result)
830
831
        resp.body = json.dumps(result)
832
833
    @staticmethod
834
    def on_post(req, resp, id_):
835
        """Handles POST requests"""
836
        try:
837
            raw_json = req.stream.read().decode('utf-8')
838
        except Exception as ex:
839
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
840
841
        if not id_.isdigit() or int(id_) <= 0:
842
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
843
                                   description='API.INVALID_SPACE_ID')
844
845
        new_values = json.loads(raw_json, encoding='utf-8')
846
847
        if 'combined_equipment_id' not in new_values['data'].keys() or \
848
                not isinstance(new_values['data']['combined_equipment_id'], int) or \
849
                new_values['data']['combined_equipment_id'] <= 0:
850
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
851
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
852
        combined_equipment_id = new_values['data']['combined_equipment_id']
853
854
        cnx = mysql.connector.connect(**config.myems_system_db)
855
        cursor = cnx.cursor()
856
857
        cursor.execute(" SELECT name "
858
                       " from tbl_spaces "
859
                       " WHERE id = %s ", (id_,))
860
        if cursor.fetchone() is None:
861
            cursor.close()
862
            cnx.disconnect()
863
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
864
                                   description='API.SPACE_NOT_FOUND')
865
866
        cursor.execute(" SELECT name "
867
                       " FROM tbl_combined_equipments "
868
                       " WHERE id = %s ", (combined_equipment_id,))
869
        if cursor.fetchone() is None:
870
            cursor.close()
871
            cnx.disconnect()
872
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
873
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
874
875
        query = (" SELECT id " 
876
                 " FROM tbl_spaces_combined_equipments "
877
                 " WHERE space_id = %s AND combined_equipment_id = %s")
878
        cursor.execute(query, (id_, combined_equipment_id,))
879
        if cursor.fetchone() is not None:
880
            cursor.close()
881
            cnx.disconnect()
882
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
883
                                   description='API.SPACE_COMBINED_EQUIPMENT_RELATION_EXISTED')
884
885
        add_row = (" INSERT INTO tbl_spaces_combined_equipments (space_id, combined_equipment_id) "
886
                   " VALUES (%s, %s) ")
887
        cursor.execute(add_row, (id_, combined_equipment_id,))
888
        new_id = cursor.lastrowid
889
        cnx.commit()
890
        cursor.close()
891
        cnx.disconnect()
892
893
        resp.status = falcon.HTTP_201
894
        resp.location = '/spaces/' + str(id_) + '/combinedequipments/' + str(combined_equipment_id)
895
896
897
class SpaceCombinedEquipmentItem:

tenant.py 1 location

@@ 1222-1325 (lines=104) @@
1219
        resp.status = falcon.HTTP_204
1220
1221
1222
class TenantSensorCollection:
1223
    @staticmethod
1224
    def __init__():
1225
        pass
1226
1227
    @staticmethod
1228
    def on_options(req, resp, id_):
1229
        resp.status = falcon.HTTP_200
1230
1231
    @staticmethod
1232
    def on_get(req, resp, id_):
1233
        if not id_.isdigit() or int(id_) <= 0:
1234
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1235
                                   description='API.INVALID_TENANT_ID')
1236
1237
        cnx = mysql.connector.connect(**config.myems_system_db)
1238
        cursor = cnx.cursor()
1239
1240
        cursor.execute(" SELECT name "
1241
                       " FROM tbl_tenants "
1242
                       " WHERE id = %s ", (id_,))
1243
        if cursor.fetchone() is None:
1244
            cursor.close()
1245
            cnx.disconnect()
1246
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1247
                                   description='API.TENANT_NOT_FOUND')
1248
1249
        query = (" SELECT s.id, s.name, s.uuid "
1250
                 " FROM tbl_tenants t, tbl_tenants_sensors ts, tbl_sensors s "
1251
                 " WHERE ts.tenant_id = t.id AND s.id = ts.sensor_id AND t.id = %s "
1252
                 " ORDER BY s.id ")
1253
        cursor.execute(query, (id_,))
1254
        rows = cursor.fetchall()
1255
1256
        result = list()
1257
        if rows is not None and len(rows) > 0:
1258
            for row in rows:
1259
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1260
                result.append(meta_result)
1261
1262
        resp.body = json.dumps(result)
1263
1264
    @staticmethod
1265
    def on_post(req, resp, id_):
1266
        """Handles POST requests"""
1267
        try:
1268
            raw_json = req.stream.read().decode('utf-8')
1269
        except Exception as ex:
1270
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1271
1272
        if not id_.isdigit() or int(id_) <= 0:
1273
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1274
                                   description='API.INVALID_TENANT_ID')
1275
1276
        new_values = json.loads(raw_json, encoding='utf-8')
1277
1278
        if 'sensor_id' not in new_values['data'].keys() or \
1279
                not isinstance(new_values['data']['sensor_id'], int) or \
1280
                new_values['data']['sensor_id'] <= 0:
1281
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1282
                                   description='API.INVALID_SENSOR_ID')
1283
        sensor_id = new_values['data']['sensor_id']
1284
1285
        cnx = mysql.connector.connect(**config.myems_system_db)
1286
        cursor = cnx.cursor()
1287
1288
        cursor.execute(" SELECT name "
1289
                       " from tbl_tenants "
1290
                       " WHERE id = %s ", (id_,))
1291
        if cursor.fetchone() is None:
1292
            cursor.close()
1293
            cnx.disconnect()
1294
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1295
                                   description='API.TENANT_NOT_FOUND')
1296
1297
        cursor.execute(" SELECT name "
1298
                       " FROM tbl_sensors "
1299
                       " WHERE id = %s ", (sensor_id,))
1300
        if cursor.fetchone() is None:
1301
            cursor.close()
1302
            cnx.disconnect()
1303
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1304
                                   description='API.SENSOR_NOT_FOUND')
1305
1306
        query = (" SELECT id " 
1307
                 " FROM tbl_tenants_sensors "
1308
                 " WHERE tenant_id = %s AND sensor_id = %s")
1309
        cursor.execute(query, (id_, sensor_id,))
1310
        if cursor.fetchone() is not None:
1311
            cursor.close()
1312
            cnx.disconnect()
1313
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1314
                                   description='API.TENANT_SENSOR_RELATION_EXISTED')
1315
1316
        add_row = (" INSERT INTO tbl_tenants_sensors (tenant_id, sensor_id) "
1317
                   " VALUES (%s, %s) ")
1318
        cursor.execute(add_row, (id_, sensor_id,))
1319
        new_id = cursor.lastrowid
1320
        cnx.commit()
1321
        cursor.close()
1322
        cnx.disconnect()
1323
1324
        resp.status = falcon.HTTP_201
1325
        resp.location = '/tenants/' + str(id_) + '/sensors/' + str(sensor_id)
1326
1327
1328
class TenantSensorItem:

shopfloor.py 2 locations

@@ 1214-1317 (lines=104) @@
1211
        resp.status = falcon.HTTP_204
1212
1213
1214
class ShopfloorSensorCollection:
1215
    @staticmethod
1216
    def __init__():
1217
        pass
1218
1219
    @staticmethod
1220
    def on_options(req, resp, id_):
1221
        resp.status = falcon.HTTP_200
1222
1223
    @staticmethod
1224
    def on_get(req, resp, id_):
1225
        if not id_.isdigit() or int(id_) <= 0:
1226
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1227
                                   description='API.INVALID_SHOPFLOOR_ID')
1228
1229
        cnx = mysql.connector.connect(**config.myems_system_db)
1230
        cursor = cnx.cursor()
1231
1232
        cursor.execute(" SELECT name "
1233
                       " FROM tbl_shopfloors "
1234
                       " WHERE id = %s ", (id_,))
1235
        if cursor.fetchone() is None:
1236
            cursor.close()
1237
            cnx.disconnect()
1238
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1239
                                   description='API.SHOPFLOOR_NOT_FOUND')
1240
1241
        query = (" SELECT se.id, se.name, se.uuid "
1242
                 " FROM tbl_shopfloors sp, tbl_shopfloors_sensors ss, tbl_sensors se "
1243
                 " WHERE ss.shopfloor_id = sp.id AND se.id = ss.sensor_id AND sp.id = %s "
1244
                 " ORDER BY se.id ")
1245
        cursor.execute(query, (id_,))
1246
        rows = cursor.fetchall()
1247
1248
        result = list()
1249
        if rows is not None and len(rows) > 0:
1250
            for row in rows:
1251
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1252
                result.append(meta_result)
1253
1254
        resp.body = json.dumps(result)
1255
1256
    @staticmethod
1257
    def on_post(req, resp, id_):
1258
        """Handles POST requests"""
1259
        try:
1260
            raw_json = req.stream.read().decode('utf-8')
1261
        except Exception as ex:
1262
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1263
1264
        if not id_.isdigit() or int(id_) <= 0:
1265
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1266
                                   description='API.INVALID_SHOPFLOOR_ID')
1267
1268
        new_values = json.loads(raw_json, encoding='utf-8')
1269
1270
        if 'sensor_id' not in new_values['data'].keys() or \
1271
                not isinstance(new_values['data']['sensor_id'], int) or \
1272
                new_values['data']['sensor_id'] <= 0:
1273
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1274
                                   description='API.INVALID_SENSOR_ID')
1275
        sensor_id = new_values['data']['sensor_id']
1276
1277
        cnx = mysql.connector.connect(**config.myems_system_db)
1278
        cursor = cnx.cursor()
1279
1280
        cursor.execute(" SELECT name "
1281
                       " from tbl_shopfloors "
1282
                       " WHERE id = %s ", (id_,))
1283
        if cursor.fetchone() is None:
1284
            cursor.close()
1285
            cnx.disconnect()
1286
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1287
                                   description='API.SHOPFLOOR_NOT_FOUND')
1288
1289
        cursor.execute(" SELECT name "
1290
                       " FROM tbl_sensors "
1291
                       " WHERE id = %s ", (sensor_id,))
1292
        if cursor.fetchone() is None:
1293
            cursor.close()
1294
            cnx.disconnect()
1295
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1296
                                   description='API.SENSOR_NOT_FOUND')
1297
1298
        query = (" SELECT id " 
1299
                 " FROM tbl_shopfloors_sensors "
1300
                 " WHERE shopfloor_id = %s AND sensor_id = %s")
1301
        cursor.execute(query, (id_, sensor_id,))
1302
        if cursor.fetchone() is not None:
1303
            cursor.close()
1304
            cnx.disconnect()
1305
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1306
                                   description='API.SHOPFLOOR_SENSOR_RELATION_EXISTED')
1307
1308
        add_row = (" INSERT INTO tbl_shopfloors_sensors (shopfloor_id, sensor_id) "
1309
                   " VALUES (%s, %s) ")
1310
        cursor.execute(add_row, (id_, sensor_id,))
1311
        new_id = cursor.lastrowid
1312
        cnx.commit()
1313
        cursor.close()
1314
        cnx.disconnect()
1315
1316
        resp.status = falcon.HTTP_201
1317
        resp.location = '/shopfloors/' + str(id_) + '/sensors/' + str(sensor_id)
1318
1319
1320
class ShopfloorSensorItem:
@@ 514-617 (lines=104) @@
511
        resp.status = falcon.HTTP_200
512
513
514
class ShopfloorEquipmentCollection:
515
    @staticmethod
516
    def __init__():
517
        pass
518
519
    @staticmethod
520
    def on_options(req, resp, id_):
521
        resp.status = falcon.HTTP_200
522
523
    @staticmethod
524
    def on_get(req, resp, id_):
525
        if not id_.isdigit() or int(id_) <= 0:
526
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
527
                                   description='API.INVALID_SHOPFLOOR_ID')
528
529
        cnx = mysql.connector.connect(**config.myems_system_db)
530
        cursor = cnx.cursor()
531
532
        cursor.execute(" SELECT name "
533
                       " FROM tbl_shopfloors "
534
                       " WHERE id = %s ", (id_,))
535
        if cursor.fetchone() is None:
536
            cursor.close()
537
            cnx.disconnect()
538
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
539
                                   description='API.SHOPFLOOR_NOT_FOUND')
540
541
        query = (" SELECT e.id, e.name, e.uuid "
542
                 " FROM tbl_shopfloors s, tbl_shopfloors_equipments se, tbl_equipments e "
543
                 " WHERE se.shopfloor_id = s.id AND e.id = se.equipment_id AND s.id = %s "
544
                 " ORDER BY e.id ")
545
        cursor.execute(query, (id_,))
546
        rows = cursor.fetchall()
547
548
        result = list()
549
        if rows is not None and len(rows) > 0:
550
            for row in rows:
551
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
552
                result.append(meta_result)
553
554
        resp.body = json.dumps(result)
555
556
    @staticmethod
557
    def on_post(req, resp, id_):
558
        """Handles POST requests"""
559
        try:
560
            raw_json = req.stream.read().decode('utf-8')
561
        except Exception as ex:
562
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
563
564
        if not id_.isdigit() or int(id_) <= 0:
565
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
566
                                   description='API.INVALID_SHOPFLOOR_ID')
567
568
        new_values = json.loads(raw_json, encoding='utf-8')
569
570
        if 'equipment_id' not in new_values['data'].keys() or \
571
                not isinstance(new_values['data']['equipment_id'], int) or \
572
                new_values['data']['equipment_id'] <= 0:
573
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
574
                                   description='API.INVALID_EQUIPMENT_ID')
575
        equipment_id = new_values['data']['equipment_id']
576
577
        cnx = mysql.connector.connect(**config.myems_system_db)
578
        cursor = cnx.cursor()
579
580
        cursor.execute(" SELECT name "
581
                       " from tbl_shopfloors "
582
                       " WHERE id = %s ", (id_,))
583
        if cursor.fetchone() is None:
584
            cursor.close()
585
            cnx.disconnect()
586
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
587
                                   description='API.SHOPFLOOR_NOT_FOUND')
588
589
        cursor.execute(" SELECT name "
590
                       " FROM tbl_equipments "
591
                       " WHERE id = %s ", (equipment_id,))
592
        if cursor.fetchone() is None:
593
            cursor.close()
594
            cnx.disconnect()
595
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
596
                                   description='API.EQUIPMENT_NOT_FOUND')
597
598
        query = (" SELECT id " 
599
                 " FROM tbl_shopfloors_equipments "
600
                 " WHERE shopfloor_id = %s AND equipment_id = %s")
601
        cursor.execute(query, (id_, equipment_id,))
602
        if cursor.fetchone() is not None:
603
            cursor.close()
604
            cnx.disconnect()
605
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
606
                                   description='API.SHOPFLOOR_EQUIPMENT_RELATION_EXISTED')
607
608
        add_row = (" INSERT INTO tbl_shopfloors_equipments (shopfloor_id, equipment_id) "
609
                   " VALUES (%s, %s) ")
610
        cursor.execute(add_row, (id_, equipment_id,))
611
        new_id = cursor.lastrowid
612
        cnx.commit()
613
        cursor.close()
614
        cnx.disconnect()
615
616
        resp.status = falcon.HTTP_201
617
        resp.location = '/shopfloors/' + str(id_) + '/equipments/' + str(equipment_id)
618
619
620
class ShopfloorEquipmentItem:

store.py 1 location

@@ 1125-1228 (lines=104) @@
1122
        resp.status = falcon.HTTP_204
1123
1124
1125
class StoreSensorCollection:
1126
    @staticmethod
1127
    def __init__():
1128
        pass
1129
1130
    @staticmethod
1131
    def on_options(req, resp, id_):
1132
        resp.status = falcon.HTTP_200
1133
1134
    @staticmethod
1135
    def on_get(req, resp, id_):
1136
        if not id_.isdigit() or int(id_) <= 0:
1137
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1138
                                   description='API.INVALID_STORE_ID')
1139
1140
        cnx = mysql.connector.connect(**config.myems_system_db)
1141
        cursor = cnx.cursor()
1142
1143
        cursor.execute(" SELECT name "
1144
                       " FROM tbl_stores "
1145
                       " WHERE id = %s ", (id_,))
1146
        if cursor.fetchone() is None:
1147
            cursor.close()
1148
            cnx.disconnect()
1149
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1150
                                   description='API.STORE_NOT_FOUND')
1151
1152
        query = (" SELECT s.id, s.name, s.uuid "
1153
                 " FROM tbl_stores t, tbl_stores_sensors ts, tbl_sensors s "
1154
                 " WHERE ts.store_id = t.id AND s.id = ts.sensor_id AND t.id = %s "
1155
                 " ORDER BY s.id ")
1156
        cursor.execute(query, (id_,))
1157
        rows = cursor.fetchall()
1158
1159
        result = list()
1160
        if rows is not None and len(rows) > 0:
1161
            for row in rows:
1162
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
1163
                result.append(meta_result)
1164
1165
        resp.body = json.dumps(result)
1166
1167
    @staticmethod
1168
    def on_post(req, resp, id_):
1169
        """Handles POST requests"""
1170
        try:
1171
            raw_json = req.stream.read().decode('utf-8')
1172
        except Exception as ex:
1173
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
1174
1175
        if not id_.isdigit() or int(id_) <= 0:
1176
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1177
                                   description='API.INVALID_STORE_ID')
1178
1179
        new_values = json.loads(raw_json, encoding='utf-8')
1180
1181
        if 'sensor_id' not in new_values['data'].keys() or \
1182
                not isinstance(new_values['data']['sensor_id'], int) or \
1183
                new_values['data']['sensor_id'] <= 0:
1184
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
1185
                                   description='API.INVALID_SENSOR_ID')
1186
        sensor_id = new_values['data']['sensor_id']
1187
1188
        cnx = mysql.connector.connect(**config.myems_system_db)
1189
        cursor = cnx.cursor()
1190
1191
        cursor.execute(" SELECT name "
1192
                       " from tbl_stores "
1193
                       " WHERE id = %s ", (id_,))
1194
        if cursor.fetchone() is None:
1195
            cursor.close()
1196
            cnx.disconnect()
1197
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1198
                                   description='API.STORE_NOT_FOUND')
1199
1200
        cursor.execute(" SELECT name "
1201
                       " FROM tbl_sensors "
1202
                       " WHERE id = %s ", (sensor_id,))
1203
        if cursor.fetchone() is None:
1204
            cursor.close()
1205
            cnx.disconnect()
1206
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
1207
                                   description='API.SENSOR_NOT_FOUND')
1208
1209
        query = (" SELECT id " 
1210
                 " FROM tbl_stores_sensors "
1211
                 " WHERE store_id = %s AND sensor_id = %s")
1212
        cursor.execute(query, (id_, sensor_id,))
1213
        if cursor.fetchone() is not None:
1214
            cursor.close()
1215
            cnx.disconnect()
1216
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
1217
                                   description='API.STORE_SENSOR_RELATION_EXISTED')
1218
1219
        add_row = (" INSERT INTO tbl_stores_sensors (store_id, sensor_id) "
1220
                   " VALUES (%s, %s) ")
1221
        cursor.execute(add_row, (id_, sensor_id,))
1222
        new_id = cursor.lastrowid
1223
        cnx.commit()
1224
        cursor.close()
1225
        cnx.disconnect()
1226
1227
        resp.status = falcon.HTTP_201
1228
        resp.location = '/stores/' + str(id_) + '/sensors/' + str(sensor_id)
1229
1230
1231
class StoreSensorItem:

combinedequipment.py 1 location

@@ 515-618 (lines=104) @@
512
        resp.location = '/combinedequipments/' + str(new_id)
513
514
515
class CombinedEquipmentEquipmentCollection:
516
    @staticmethod
517
    def __init__():
518
        pass
519
520
    @staticmethod
521
    def on_options(req, resp, id_):
522
        resp.status = falcon.HTTP_200
523
524
    @staticmethod
525
    def on_get(req, resp, id_):
526
        if not id_.isdigit() or int(id_) <= 0:
527
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
528
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
529
530
        cnx = mysql.connector.connect(**config.myems_system_db)
531
        cursor = cnx.cursor()
532
533
        cursor.execute(" SELECT name "
534
                       " FROM tbl_combined_equipments "
535
                       " WHERE id = %s ", (id_,))
536
        if cursor.fetchone() is None:
537
            cursor.close()
538
            cnx.disconnect()
539
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
540
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
541
542
        query = (" SELECT e.id, e.name, e.uuid "
543
                 " FROM tbl_combined_equipments c, tbl_combined_equipments_equipments ce, tbl_equipments e "
544
                 " WHERE ce.combined_equipment_id = c.id AND e.id = ce.equipment_id AND c.id = %s "
545
                 " ORDER BY e.id ")
546
        cursor.execute(query, (id_,))
547
        rows = cursor.fetchall()
548
549
        result = list()
550
        if rows is not None and len(rows) > 0:
551
            for row in rows:
552
                meta_result = {"id": row[0], "name": row[1], "uuid": row[2]}
553
                result.append(meta_result)
554
555
        resp.body = json.dumps(result)
556
557
    @staticmethod
558
    def on_post(req, resp, id_):
559
        """Handles POST requests"""
560
        try:
561
            raw_json = req.stream.read().decode('utf-8')
562
        except Exception as ex:
563
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
564
565
        if not id_.isdigit() or int(id_) <= 0:
566
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
567
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
568
569
        new_values = json.loads(raw_json, encoding='utf-8')
570
571
        if 'equipment_id' not in new_values['data'].keys() or \
572
                not isinstance(new_values['data']['equipment_id'], int) or \
573
                new_values['data']['equipment_id'] <= 0:
574
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
575
                                   description='API.INVALID_EQUIPMENT_ID')
576
        equipment_id = new_values['data']['equipment_id']
577
578
        cnx = mysql.connector.connect(**config.myems_system_db)
579
        cursor = cnx.cursor()
580
581
        cursor.execute(" SELECT name "
582
                       " from tbl_combined_equipments "
583
                       " WHERE id = %s ", (id_,))
584
        if cursor.fetchone() is None:
585
            cursor.close()
586
            cnx.disconnect()
587
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
588
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
589
590
        cursor.execute(" SELECT name "
591
                       " FROM tbl_equipments "
592
                       " WHERE id = %s ", (equipment_id,))
593
        if cursor.fetchone() is None:
594
            cursor.close()
595
            cnx.disconnect()
596
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
597
                                   description='API.EQUIPMENT_NOT_FOUND')
598
599
        query = (" SELECT id " 
600
                 " FROM tbl_combined_equipments_equipments "
601
                 " WHERE combined_equipment_id = %s AND equipment_id = %s")
602
        cursor.execute(query, (id_, equipment_id,))
603
        if cursor.fetchone() is not None:
604
            cursor.close()
605
            cnx.disconnect()
606
            raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',
607
                                   description='API.COMBINED_EQUIPMENT_EQUIPMENT_RELATION_EXISTED')
608
609
        add_row = (" INSERT INTO tbl_combined_equipments_equipments (combined_equipment_id, equipment_id) "
610
                   " VALUES (%s, %s) ")
611
        cursor.execute(add_row, (id_, equipment_id,))
612
        new_id = cursor.lastrowid
613
        cnx.commit()
614
        cursor.close()
615
        cnx.disconnect()
616
617
        resp.status = falcon.HTTP_201
618
        resp.location = '/combinedequipments/' + str(id_) + '/equipments/' + str(equipment_id)
619
620
621
class CombinedEquipmentEquipmentItem: