Code Duplication    Length = 57-63 lines in 2 locations

gsmmodem.py 1 location

@@ 149-211 (lines=63) @@
146
147
        resp.status = falcon.HTTP_204
148
149
    @staticmethod
150
    def on_put(req, resp, id_):
151
        """Handles PUT requests"""
152
        try:
153
            raw_json = req.stream.read().decode('utf-8')
154
        except Exception as ex:
155
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
156
157
        if not id_.isdigit() or int(id_) <= 0:
158
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
159
                                   description='API.INVALID_GSM_MODEM_ID')
160
161
        new_values = json.loads(raw_json, encoding='utf-8')
162
163
        if 'serial_port' not in new_values['data'].keys() or \
164
                not isinstance(new_values['data']['serial_port'], str) or \
165
                len(str.strip(new_values['data']['serial_port'])) == 0:
166
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
167
                                   description='API.INVALID_SERIAL_PORT')
168
169
        serial_port = str.strip(new_values['data']['serial_port'])
170
171
        if 'baud_rate' not in new_values['data'].keys() or \
172
                not isinstance(new_values['data']['baud_rate'], int) or \
173
                new_values['data']['baud_rate'] <= 0:
174
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
175
                                   description='API.INVALID_BAUD_RATE')
176
        baud_rate = float(new_values['data']['baud_rate'])
177
178
        cnx = mysql.connector.connect(**config.myems_fdd_db)
179
        cursor = cnx.cursor()
180
181
        cursor.execute(" SELECT serial_port "
182
                       " FROM tbl_gsm_modems "
183
                       " WHERE id = %s ",
184
                       (id_,))
185
        if cursor.fetchone() is None:
186
            cursor.close()
187
            cnx.disconnect()
188
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
189
                                   description='API.GSM_MODEM_NOT_FOUND')
190
191
        cursor.execute(" SELECT serial_port "
192
                       " FROM tbl_gsm_modems "
193
                       " WHERE serial_port = %s AND id != %s ", (serial_port, id_))
194
        if cursor.fetchone() is not None:
195
            cursor.close()
196
            cnx.disconnect()
197
            raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST',
198
                                   description='API.GSM_MODEM_SERIAL_PORT_IS_ALREADY_IN_USE')
199
200
        update_row = (" UPDATE tbl_gsm_modems "
201
                      " SET serial_port = %s, baud_rate = %s "
202
                      " WHERE id = %s ")
203
        cursor.execute(update_row, (serial_port,
204
                                    baud_rate,
205
                                    id_,))
206
        cnx.commit()
207
208
        cursor.close()
209
        cnx.disconnect()
210
211
        resp.status = falcon.HTTP_200
212

privilege.py 1 location

@@ 136-192 (lines=57) @@
133
134
        resp.status = falcon.HTTP_204
135
136
    @staticmethod
137
    def on_put(req, resp, id_):
138
        """Handles PUT requests"""
139
        try:
140
            raw_json = req.stream.read().decode('utf-8')
141
            new_values = json.loads(raw_json, encoding='utf-8')
142
        except Exception as ex:
143
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
144
145
        if not id_.isdigit() or int(id_) <= 0:
146
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
147
                                   description='API.INVALID_PRIVILEGE_ID_')
148
        if 'name' not in new_values['data'] or \
149
                not isinstance(new_values['data']['name'], str) or \
150
                len(str.strip(new_values['data']['name'])) == 0:
151
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
152
                                   description='API.INVALID_PRIVILEGE_NAME')
153
        name = str.strip(new_values['data']['name'])
154
155
        if 'data' not in new_values['data'] or \
156
                not isinstance(new_values['data']['data'], str) or \
157
                len(str.strip(new_values['data']['data'])) == 0:
158
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
159
                                   description='API.INVALID_PRIVILEGE_DATA')
160
        data = str.strip(new_values['data']['data'])
161
162
        cnx = mysql.connector.connect(**config.myems_user_db)
163
        cursor = cnx.cursor()
164
165
        cursor.execute(" SELECT name "
166
                       " FROM tbl_privileges "
167
                       " WHERE id = %s ", (id_,))
168
        if cursor.fetchone() is None:
169
            cursor.close()
170
            cnx.disconnect()
171
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
172
                                   description='API.PRIVILEGE_NOT_FOUND')
173
174
        cursor.execute(" SELECT name "
175
                       " FROM tbl_privileges "
176
                       " WHERE name = %s AND id != %s ", (name, id_))
177
        if cursor.fetchone() is not None:
178
            cursor.close()
179
            cnx.disconnect()
180
            raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST',
181
                                   description='API.PRIVILEGE_NAME_IS_ALREADY_IN_USE')
182
183
        update_row = (" UPDATE tbl_privileges "
184
                      " SET name = %s, data = %s "
185
                      " WHERE id = %s ")
186
        cursor.execute(update_row, (name, data, id_,))
187
        cnx.commit()
188
189
        cursor.close()
190
        cnx.disconnect()
191
192
        resp.status = falcon.HTTP_200
193
194