| Conditions | 11 |
| Total Lines | 77 |
| Code Lines | 51 |
| Lines | 77 |
| 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.PrivilegeCollection.on_post() 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 |
||
| 69 | View Code Duplication | @staticmethod |
|
|
|
|||
| 70 | @user_logger |
||
| 71 | def on_post(req, resp): |
||
| 72 | """ |
||
| 73 | Handle POST requests to create a new privilege |
||
| 74 | |||
| 75 | Creates a new privilege with the specified name and data configuration. |
||
| 76 | Requires admin privileges. |
||
| 77 | |||
| 78 | Args: |
||
| 79 | req: Falcon request object containing privilege data: |
||
| 80 | - name: Privilege name (required) |
||
| 81 | - data: Privilege data configuration (required) |
||
| 82 | resp: Falcon response object |
||
| 83 | """ |
||
| 84 | admin_control(req) |
||
| 85 | try: |
||
| 86 | raw_json = req.stream.read().decode('utf-8') |
||
| 87 | new_values = json.loads(raw_json) |
||
| 88 | except UnicodeDecodeError as ex: |
||
| 89 | print("Failed to decode request") |
||
| 90 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 91 | title='API.BAD_REQUEST', |
||
| 92 | description='API.INVALID_ENCODING') |
||
| 93 | except json.JSONDecodeError as ex: |
||
| 94 | print("Failed to parse JSON") |
||
| 95 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 96 | title='API.BAD_REQUEST', |
||
| 97 | description='API.INVALID_JSON_FORMAT') |
||
| 98 | except Exception as ex: |
||
| 99 | print("Unexpected error reading request stream") |
||
| 100 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 101 | title='API.BAD_REQUEST', |
||
| 102 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 103 | |||
| 104 | # Validate privilege name |
||
| 105 | if 'name' not in new_values['data'] or \ |
||
| 106 | not isinstance(new_values['data']['name'], str) or \ |
||
| 107 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 108 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 109 | description='API.INVALID_PRIVILEGE_NAME') |
||
| 110 | name = str.strip(new_values['data']['name']) |
||
| 111 | |||
| 112 | # Validate privilege data |
||
| 113 | if 'data' not in new_values['data'] or \ |
||
| 114 | not isinstance(new_values['data']['data'], str) or \ |
||
| 115 | len(str.strip(new_values['data']['data'])) == 0: |
||
| 116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 117 | description='API.INVALID_PRIVILEGE_DATA') |
||
| 118 | data = str.strip(new_values['data']['data']) |
||
| 119 | |||
| 120 | cnx = mysql.connector.connect(**config.myems_user_db) |
||
| 121 | cursor = cnx.cursor() |
||
| 122 | |||
| 123 | # Check if privilege name already exists |
||
| 124 | cursor.execute(" SELECT name " |
||
| 125 | " FROM tbl_privileges " |
||
| 126 | " WHERE name = %s ", (name,)) |
||
| 127 | if cursor.fetchone() is not None: |
||
| 128 | cursor.close() |
||
| 129 | cnx.close() |
||
| 130 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 131 | description='API.PRIVILEGE_NAME_IS_ALREADY_IN_USE') |
||
| 132 | |||
| 133 | # Insert new privilege into database |
||
| 134 | add_row = (" INSERT INTO tbl_privileges " |
||
| 135 | " (name, data) " |
||
| 136 | " VALUES (%s, %s) ") |
||
| 137 | |||
| 138 | cursor.execute(add_row, (name, data, )) |
||
| 139 | new_id = cursor.lastrowid |
||
| 140 | cnx.commit() |
||
| 141 | cursor.close() |
||
| 142 | cnx.close() |
||
| 143 | |||
| 144 | resp.status = falcon.HTTP_201 |
||
| 145 | resp.location = '/privileges/' + str(new_id) |
||
| 146 | |||
| 319 |