Conditions | 17 |
Total Lines | 89 |
Code Lines | 54 |
Lines | 89 |
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 reports.equipmenttracking.Reporting.on_get() 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 |
||
25 | @staticmethod |
||
26 | def on_get(req, resp): |
||
27 | print(req.params) |
||
28 | space_id = req.params.get('spaceid') |
||
29 | |||
30 | ################################################################################################################ |
||
31 | # Step 1: valid parameters |
||
32 | ################################################################################################################ |
||
33 | if space_id is None: |
||
34 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID') |
||
35 | else: |
||
36 | space_id = str.strip(space_id) |
||
37 | if not space_id.isdigit() or int(space_id) <= 0: |
||
38 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_SPACE_ID') |
||
39 | else: |
||
40 | space_id = int(space_id) |
||
41 | |||
42 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
43 | cursor = cnx.cursor(dictionary=True) |
||
44 | |||
45 | cursor.execute(" SELECT name " |
||
46 | " FROM tbl_spaces " |
||
47 | " WHERE id = %s ", (space_id,)) |
||
48 | row = cursor.fetchone() |
||
49 | |||
50 | if row is None: |
||
51 | if cursor: |
||
52 | cursor.close() |
||
53 | if cnx: |
||
54 | cnx.disconnect() |
||
55 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
56 | description='API.SPACE_NOT_FOUND') |
||
57 | else: |
||
58 | space_name = row['name'] |
||
59 | ################################################################################################################ |
||
60 | # Step 2: build a space tree |
||
61 | ################################################################################################################ |
||
62 | |||
63 | query = (" SELECT id, name, parent_space_id " |
||
64 | " FROM tbl_spaces " |
||
65 | " ORDER BY id ") |
||
66 | cursor.execute(query) |
||
67 | rows_spaces = cursor.fetchall() |
||
68 | node_dict = dict() |
||
69 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
70 | for row in rows_spaces: |
||
71 | parent_node = node_dict[row['parent_space_id']] if row['parent_space_id'] is not None else None |
||
72 | node_dict[row['id']] = AnyNode(id=row['id'], parent=parent_node, name=row['name']) |
||
73 | |||
74 | ################################################################################################################ |
||
75 | # Step 3: query all equipments in the space tree |
||
76 | ################################################################################################################ |
||
77 | equipment_list = list() |
||
78 | space_dict = dict() |
||
79 | |||
80 | for node in LevelOrderIter(node_dict[space_id]): |
||
81 | space_dict[node.id] = node.name |
||
82 | |||
83 | cursor.execute(" SELECT e.id, e.name AS equipment_name, s.name AS space_name, " |
||
84 | " cc.name AS cost_center_name, e.description " |
||
85 | " FROM tbl_spaces s, tbl_spaces_equipments se, tbl_equipments e, tbl_cost_centers cc " |
||
86 | " WHERE s.id IN ( " + ', '.join(map(str, space_dict.keys())) + ") " |
||
87 | " AND se.space_id = s.id " |
||
88 | " AND se.equipment_id = e.id " |
||
89 | " AND e.cost_center_id = cc.id ", ) |
||
90 | rows_equipments = cursor.fetchall() |
||
91 | if rows_equipments is not None and len(rows_equipments) > 0: |
||
92 | for row in rows_equipments: |
||
93 | equipment_list.append({"id": row['id'], |
||
94 | "equipment_name": row['equipment_name'], |
||
95 | "space_name": row['space_name'], |
||
96 | "cost_center_name": row['cost_center_name'], |
||
97 | "description": row['description']}) |
||
98 | |||
99 | if cursor: |
||
100 | cursor.close() |
||
101 | if cnx: |
||
102 | cnx.disconnect() |
||
103 | |||
104 | ################################################################################################################ |
||
105 | # Step 4: construct the report |
||
106 | ################################################################################################################ |
||
107 | result = {'equipments': equipment_list} |
||
108 | |||
109 | # export result to Excel file and then encode the file to base64 string |
||
110 | result['excel_bytes_base64'] = \ |
||
111 | excelexporters.equipmenttracking.export(result, |
||
112 | space_name) |
||
113 | resp.body = json.dumps(result) |
||
114 |