| Conditions | 14 |
| Total Lines | 90 |
| Code Lines | 59 |
| Lines | 90 |
| Ratio | 100 % |
| 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 core.privilege.PrivilegeItem.on_put() 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 |
||
| 228 | View Code Duplication | @staticmethod |
|
| 229 | @user_logger |
||
| 230 | def on_put(req, resp, id_): |
||
| 231 | """ |
||
| 232 | Handle PUT requests to update privilege information |
||
| 233 | |||
| 234 | Updates an existing privilege with new name and data configuration. |
||
| 235 | Requires admin privileges. |
||
| 236 | |||
| 237 | Args: |
||
| 238 | req: Falcon request object containing update data: |
||
| 239 | - name: New privilege name (required) |
||
| 240 | - data: New privilege data configuration (required) |
||
| 241 | resp: Falcon response object |
||
| 242 | id_: Privilege ID to update |
||
| 243 | """ |
||
| 244 | admin_control(req) |
||
| 245 | try: |
||
| 246 | raw_json = req.stream.read().decode('utf-8') |
||
| 247 | new_values = json.loads(raw_json) |
||
| 248 | except UnicodeDecodeError as ex: |
||
| 249 | print("Failed to decode request") |
||
| 250 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 251 | title='API.BAD_REQUEST', |
||
| 252 | description='API.INVALID_ENCODING') |
||
| 253 | except json.JSONDecodeError as ex: |
||
| 254 | print("Failed to parse JSON") |
||
| 255 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 256 | title='API.BAD_REQUEST', |
||
| 257 | description='API.INVALID_JSON_FORMAT') |
||
| 258 | except Exception as ex: |
||
| 259 | print("Unexpected error reading request stream") |
||
| 260 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 261 | title='API.BAD_REQUEST', |
||
| 262 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 263 | |||
| 264 | if not id_.isdigit() or int(id_) <= 0: |
||
| 265 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 266 | description='API.INVALID_PRIVILEGE_ID') |
||
| 267 | |||
| 268 | # Validate privilege name |
||
| 269 | if 'name' not in new_values['data'] or \ |
||
| 270 | not isinstance(new_values['data']['name'], str) or \ |
||
| 271 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 272 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 273 | description='API.INVALID_PRIVILEGE_NAME') |
||
| 274 | name = str.strip(new_values['data']['name']) |
||
| 275 | |||
| 276 | # Validate privilege data |
||
| 277 | if 'data' not in new_values['data'] or \ |
||
| 278 | not isinstance(new_values['data']['data'], str) or \ |
||
| 279 | len(str.strip(new_values['data']['data'])) == 0: |
||
| 280 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 281 | description='API.INVALID_PRIVILEGE_DATA') |
||
| 282 | data = str.strip(new_values['data']['data']) |
||
| 283 | |||
| 284 | cnx = mysql.connector.connect(**config.myems_user_db) |
||
| 285 | cursor = cnx.cursor() |
||
| 286 | |||
| 287 | # Check if privilege exists |
||
| 288 | cursor.execute(" SELECT name " |
||
| 289 | " FROM tbl_privileges " |
||
| 290 | " WHERE id = %s ", (id_,)) |
||
| 291 | if cursor.fetchone() is None: |
||
| 292 | cursor.close() |
||
| 293 | cnx.close() |
||
| 294 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 295 | description='API.PRIVILEGE_NOT_FOUND') |
||
| 296 | |||
| 297 | # Check if new name conflicts with existing privileges (excluding current) |
||
| 298 | cursor.execute(" SELECT name " |
||
| 299 | " FROM tbl_privileges " |
||
| 300 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 301 | if cursor.fetchone() is not None: |
||
| 302 | cursor.close() |
||
| 303 | cnx.close() |
||
| 304 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 305 | description='API.PRIVILEGE_NAME_IS_ALREADY_IN_USE') |
||
| 306 | |||
| 307 | # Update privilege information |
||
| 308 | update_row = (" UPDATE tbl_privileges " |
||
| 309 | " SET name = %s, data = %s " |
||
| 310 | " WHERE id = %s ") |
||
| 311 | cursor.execute(update_row, (name, data, id_,)) |
||
| 312 | cnx.commit() |
||
| 313 | |||
| 314 | cursor.close() |
||
| 315 | cnx.close() |
||
| 316 | |||
| 317 | resp.status = falcon.HTTP_200 |
||
| 318 | |||
| 319 |