Conditions | 24 |
Total Lines | 144 |
Code Lines | 109 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like costcenter.CostCenterItem.on_delete() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import falcon |
||
131 | @staticmethod |
||
132 | def on_delete(req, resp, id_): |
||
133 | if not id_.isdigit() or int(id_) <= 0: |
||
134 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
135 | description='API.INVALID_COST_CENTER_ID') |
||
136 | |||
137 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
138 | cursor = cnx.cursor() |
||
139 | |||
140 | cursor.execute(" SELECT name " |
||
141 | " FROM tbl_cost_centers " |
||
142 | " WHERE id = %s ", (id_,)) |
||
143 | if cursor.fetchone() is None: |
||
144 | cursor.close() |
||
145 | cnx.disconnect() |
||
146 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
147 | description='API.COST_CENTER_NOT_FOUND') |
||
148 | |||
149 | # check relation with equipments |
||
150 | cursor.execute(" SELECT id " |
||
151 | " FROM tbl_equipments " |
||
152 | " WHERE cost_center_id = %s ", (id_,)) |
||
153 | rows_equipments = cursor.fetchall() |
||
154 | if rows_equipments is not None and len(rows_equipments) > 0: |
||
155 | cursor.close() |
||
156 | cnx.disconnect() |
||
157 | raise falcon.HTTPError(falcon.HTTP_400, |
||
158 | title='API.BAD_REQUEST', |
||
159 | description='API.THERE_IS_RELATION_WITH_EQUIPMENTS') |
||
160 | |||
161 | # check relation with combined equipments |
||
162 | cursor.execute(" SELECT id " |
||
163 | " FROM tbl_combined_equipments " |
||
164 | " WHERE cost_center_id = %s ", (id_,)) |
||
165 | rows_combined_equipments = cursor.fetchall() |
||
166 | if rows_combined_equipments is not None and len(rows_combined_equipments) > 0: |
||
167 | cursor.close() |
||
168 | cnx.disconnect() |
||
169 | raise falcon.HTTPError(falcon.HTTP_400, |
||
170 | title='API.BAD_REQUEST', |
||
171 | description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS') |
||
172 | |||
173 | # check relation with tariffs |
||
174 | cursor.execute(" SELECT id " |
||
175 | " FROM tbl_cost_centers_tariffs " |
||
176 | " WHERE cost_center_id = %s ", (id_,)) |
||
177 | rows_tariffs = cursor.fetchall() |
||
178 | if rows_tariffs is not None and len(rows_tariffs) > 0: |
||
179 | cursor.close() |
||
180 | cnx.disconnect() |
||
181 | raise falcon.HTTPError(falcon.HTTP_400, |
||
182 | title='API.BAD_REQUEST', |
||
183 | description='API.THERE_IS_RELATION_WITH_TARIFFS') |
||
184 | |||
185 | # check relation with meters |
||
186 | cursor.execute(" SELECT id " |
||
187 | " FROM tbl_meters " |
||
188 | " WHERE cost_center_id = %s ", (id_,)) |
||
189 | rows_meters = cursor.fetchall() |
||
190 | if rows_meters is not None and len(rows_meters) > 0: |
||
191 | cursor.close() |
||
192 | cnx.disconnect() |
||
193 | raise falcon.HTTPError(falcon.HTTP_400, |
||
194 | title='API.BAD_REQUEST', |
||
195 | description='API.THERE_IS_RELATION_WITH_METERS') |
||
196 | |||
197 | # check relation with offline meters |
||
198 | cursor.execute(" SELECT id " |
||
199 | " FROM tbl_offline_meters " |
||
200 | " WHERE cost_center_id = %s ", (id_,)) |
||
201 | rows_offline_meters = cursor.fetchall() |
||
202 | if rows_offline_meters is not None and len(rows_offline_meters) > 0: |
||
203 | cursor.close() |
||
204 | cnx.disconnect() |
||
205 | raise falcon.HTTPError(falcon.HTTP_400, |
||
206 | title='API.BAD_REQUEST', |
||
207 | description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS') |
||
208 | |||
209 | # check relation with virtual meters |
||
210 | cursor.execute(" SELECT id " |
||
211 | " FROM tbl_virtual_meters " |
||
212 | " WHERE cost_center_id = %s ", (id_,)) |
||
213 | rows_virtual_meters = cursor.fetchall() |
||
214 | if rows_virtual_meters is not None and len(rows_virtual_meters) > 0: |
||
215 | cursor.close() |
||
216 | cnx.disconnect() |
||
217 | raise falcon.HTTPError(falcon.HTTP_400, |
||
218 | title='API.BAD_REQUEST', |
||
219 | description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS') |
||
220 | |||
221 | # check relation with tenants |
||
222 | cursor.execute(" SELECT id " |
||
223 | " FROM tbl_tenants " |
||
224 | " WHERE cost_center_id = %s ", (id_,)) |
||
225 | rows_tenants = cursor.fetchall() |
||
226 | if rows_tenants is not None and len(rows_tenants) > 0: |
||
227 | cursor.close() |
||
228 | cnx.disconnect() |
||
229 | raise falcon.HTTPError(falcon.HTTP_400, |
||
230 | title='API.BAD_REQUEST', |
||
231 | description='API.THERE_IS_RELATION_WITH_TENANTS') |
||
232 | |||
233 | # check relation with stores |
||
234 | cursor.execute(" SELECT id " |
||
235 | " FROM tbl_stores " |
||
236 | " WHERE cost_center_id = %s ", (id_,)) |
||
237 | rows_stores = cursor.fetchall() |
||
238 | if rows_stores is not None and len(rows_stores) > 0: |
||
239 | cursor.close() |
||
240 | cnx.disconnect() |
||
241 | raise falcon.HTTPError(falcon.HTTP_400, |
||
242 | title='API.BAD_REQUEST', |
||
243 | description='API.THERE_IS_RELATION_WITH_STORES') |
||
244 | |||
245 | # check relation with spaces |
||
246 | cursor.execute(" SELECT id " |
||
247 | " FROM tbl_spaces " |
||
248 | " WHERE cost_center_id = %s ", (id_,)) |
||
249 | rows_factories = cursor.fetchall() |
||
250 | if rows_factories is not None and len(rows_factories) > 0: |
||
251 | cursor.close() |
||
252 | cnx.disconnect() |
||
253 | raise falcon.HTTPError(falcon.HTTP_400, |
||
254 | title='API.BAD_REQUEST', |
||
255 | description='API.THERE_IS_RELATIONSHIP_WITH_SPACES') |
||
256 | |||
257 | # check relation with shopfloors |
||
258 | cursor.execute(" SELECT id " |
||
259 | " FROM tbl_shopfloors " |
||
260 | " WHERE cost_center_id = %s ", (id_,)) |
||
261 | rows_shopfloors = cursor.fetchall() |
||
262 | if rows_shopfloors is not None and len(rows_shopfloors) > 0: |
||
263 | cursor.close() |
||
264 | cnx.disconnect() |
||
265 | raise falcon.HTTPError(falcon.HTTP_400, |
||
266 | title='API.BAD_REQUEST', |
||
267 | description='API.THERE_IS_RELATION_WITH_SHOPFLOORS') |
||
268 | |||
269 | cursor.execute(" DELETE FROM tbl_cost_centers WHERE id = %s ", (id_,)) |
||
270 | cnx.commit() |
||
271 | |||
272 | cursor.close() |
||
273 | cnx.disconnect() |
||
274 | resp.status = falcon.HTTP_204 |
||
275 | |||
517 |