Conditions | 29 |
Total Lines | 107 |
Code Lines | 90 |
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 core.rule.RuleCollection.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 |
||
45 | @staticmethod |
||
46 | def on_post(req, resp): |
||
47 | """Handles POST requests""" |
||
48 | try: |
||
49 | raw_json = req.stream.read().decode('utf-8') |
||
50 | except Exception as ex: |
||
51 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
52 | |||
53 | new_values = json.loads(raw_json) |
||
54 | if 'name' not in new_values['data'].keys() or \ |
||
55 | not isinstance(new_values['data']['name'], str) or \ |
||
56 | len(str.strip(new_values['data']['name'])) == 0: |
||
57 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
58 | description='API.INVALID_RULE_NAME') |
||
59 | name = str.strip(new_values['data']['name']) |
||
60 | |||
61 | if 'fdd_code' not in new_values['data'].keys() or \ |
||
62 | not isinstance(new_values['data']['fdd_code'], str) or \ |
||
63 | len(str.strip(new_values['data']['fdd_code'])) == 0: |
||
64 | raise falcon.HTTPError(falcon.HTTP_400, |
||
65 | title='API.BAD_REQUEST', |
||
66 | description='API.INVALID_FDD_CODE') |
||
67 | fdd_code = str.strip(new_values['data']['fdd_code']) |
||
68 | |||
69 | if 'category' not in new_values['data'].keys() or \ |
||
70 | not isinstance(new_values['data']['category'], str) or \ |
||
71 | len(str.strip(new_values['data']['category'])) == 0 or \ |
||
72 | str.strip(new_values['data']['category']) not in \ |
||
73 | ('SYSTEM', 'SPACE', 'METER', 'TENANT', 'STORE', 'SHOPFLOOR', 'EQUIPMENT', 'COMBINEDEQUIPMENT'): |
||
74 | raise falcon.HTTPError(falcon.HTTP_400, |
||
75 | title='API.BAD_REQUEST', |
||
76 | description='API.INVALID_CATEGORY') |
||
77 | category = str.strip(new_values['data']['category']) |
||
78 | |||
79 | if 'priority' not in new_values['data'].keys() or \ |
||
80 | not isinstance(new_values['data']['priority'], str) or \ |
||
81 | len(str.strip(new_values['data']['priority'])) == 0 or \ |
||
82 | str.strip(new_values['data']['priority']) not in \ |
||
83 | ('CRITICAL', 'HIGH', 'MEDIUM', 'LOW'): |
||
84 | raise falcon.HTTPError(falcon.HTTP_400, |
||
85 | title='API.BAD_REQUEST', |
||
86 | description='API.INVALID_PRIORITY') |
||
87 | priority = str.strip(new_values['data']['priority']) |
||
88 | |||
89 | if 'channel' not in new_values['data'].keys() or \ |
||
90 | not isinstance(new_values['data']['channel'], str) or \ |
||
91 | len(str.strip(new_values['data']['channel'])) == 0 or \ |
||
92 | str.strip(new_values['data']['channel']) not in ('WEB', 'EMAIL', 'SMS', 'WECHAT', 'CALL'): |
||
93 | raise falcon.HTTPError(falcon.HTTP_400, |
||
94 | title='API.BAD_REQUEST', |
||
95 | description='API.INVALID_CHANNEL') |
||
96 | channel = str.strip(new_values['data']['channel']) |
||
97 | |||
98 | if 'expression' not in new_values['data'].keys() or \ |
||
99 | not isinstance(new_values['data']['expression'], str) or \ |
||
100 | len(str.strip(new_values['data']['expression'])) == 0: |
||
101 | raise falcon.HTTPError(falcon.HTTP_400, |
||
102 | title='API.BAD_REQUEST', |
||
103 | description='API.INVALID_EXPRESSION') |
||
104 | expression = str.strip(new_values['data']['expression']) |
||
105 | |||
106 | if 'message_template' not in new_values['data'].keys() or \ |
||
107 | not isinstance(new_values['data']['message_template'], str) or \ |
||
108 | len(str.strip(new_values['data']['message_template'])) == 0: |
||
109 | raise falcon.HTTPError(falcon.HTTP_400, |
||
110 | title='API.BAD_REQUEST', |
||
111 | description='API.INVALID_MESSAGE_TEMPLATE') |
||
112 | message_template = str.strip(new_values['data']['message_template']) |
||
113 | |||
114 | if 'is_enabled' not in new_values['data'].keys() or \ |
||
115 | not isinstance(new_values['data']['is_enabled'], bool): |
||
116 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
117 | description='API.INVALID_IS_ENABLED') |
||
118 | is_enabled = new_values['data']['is_enabled'] |
||
119 | |||
120 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
121 | cursor = cnx.cursor() |
||
122 | |||
123 | cursor.execute(" SELECT name " |
||
124 | " FROM tbl_rules " |
||
125 | " WHERE name = %s ", (name,)) |
||
126 | if cursor.fetchone() is not None: |
||
127 | cursor.close() |
||
128 | cnx.disconnect() |
||
129 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
130 | description='API.RULE_NAME_IS_ALREADY_IN_USE') |
||
131 | |||
132 | add_row = (" INSERT INTO tbl_rules " |
||
133 | " (name, uuid, fdd_code, category, priority, " |
||
134 | " channel, expression, message_template, is_enabled) " |
||
135 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
136 | cursor.execute(add_row, (name, |
||
137 | str(uuid.uuid4()), |
||
138 | fdd_code, |
||
139 | category, |
||
140 | priority, |
||
141 | channel, |
||
142 | expression, |
||
143 | message_template, |
||
144 | is_enabled)) |
||
145 | new_id = cursor.lastrowid |
||
146 | cnx.commit() |
||
147 | cursor.close() |
||
148 | cnx.disconnect() |
||
149 | |||
150 | resp.status = falcon.HTTP_201 |
||
151 | resp.location = '/rules/' + str(new_id) |
||
152 | |||
339 |