Total Complexity | 50 |
Total Lines | 468 |
Duplicated Lines | 94.66 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like excelexporters.tenantload 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 base64 |
||
2 | import uuid |
||
3 | import os |
||
4 | from openpyxl.chart import ( |
||
5 | PieChart, |
||
6 | BarChart, |
||
7 | Reference, |
||
8 | ) |
||
9 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
||
10 | from openpyxl.drawing.image import Image |
||
11 | from openpyxl import Workbook |
||
12 | from openpyxl.chart.label import DataLabelList |
||
13 | |||
14 | |||
15 | #################################################################################################################### |
||
16 | # PROCEDURES |
||
17 | # Step 1: Validate the report data |
||
18 | # Step 2: Generate excel file |
||
19 | # Step 3: Encode the excel file bytes to Base64 |
||
20 | #################################################################################################################### |
||
21 | |||
22 | |||
23 | View Code Duplication | def export(report, |
|
|
|||
24 | name, |
||
25 | reporting_start_datetime_local, |
||
26 | reporting_end_datetime_local, |
||
27 | period_type): |
||
28 | #################################################################################################################### |
||
29 | # Step 1: Validate the report data |
||
30 | #################################################################################################################### |
||
31 | if report is None: |
||
32 | return None |
||
33 | print(report) |
||
34 | |||
35 | #################################################################################################################### |
||
36 | # Step 2: Generate excel file from the report data |
||
37 | #################################################################################################################### |
||
38 | filename = generate_excel(report, |
||
39 | name, |
||
40 | reporting_start_datetime_local, |
||
41 | reporting_end_datetime_local, |
||
42 | period_type) |
||
43 | #################################################################################################################### |
||
44 | # Step 3: Encode the excel file to Base64 |
||
45 | #################################################################################################################### |
||
46 | try: |
||
47 | with open(filename, 'rb') as binary_file: |
||
48 | binary_file_data = binary_file.read() |
||
49 | except IOError as ex: |
||
50 | pass |
||
51 | |||
52 | # Base64 encode the bytes |
||
53 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
54 | # get the Base64 encoded data using human-readable characters. |
||
55 | base64_message = base64_encoded_data.decode('utf-8') |
||
56 | # delete the file from server |
||
57 | try: |
||
58 | os.remove(filename) |
||
59 | except NotImplementedError as ex: |
||
60 | pass |
||
61 | return base64_message |
||
62 | |||
63 | |||
64 | View Code Duplication | def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
65 | wb = Workbook() |
||
66 | |||
67 | # todo |
||
68 | ws = wb.active |
||
69 | |||
70 | # Row height |
||
71 | ws.row_dimensions[1].height = 118 |
||
72 | for i in range(2, 2000 + 1): |
||
73 | ws.row_dimensions[i].height = 30 |
||
74 | |||
75 | # Col width |
||
76 | ws.column_dimensions['A'].width = 1.5 |
||
77 | |||
78 | for i in range(ord('B'), ord('I')): |
||
79 | ws.column_dimensions[chr(i)].width = 18 |
||
80 | |||
81 | # Font |
||
82 | name_font = Font(name='Constantia', size=15, bold=True) |
||
83 | title_font = Font(name='宋体', size=15, bold=True) |
||
84 | table_title_font = Font(name='宋体', size=10, bold=True) |
||
85 | data_font = Font(name='Franklin Gothic Book', size=11) |
||
86 | |||
87 | table_fill = PatternFill(fill_type='solid', fgColor='1F497D') |
||
88 | f_border = Border(left=Side(border_style='medium', color='00000000'), |
||
89 | right=Side(border_style='medium', color='00000000'), |
||
90 | bottom=Side(border_style='medium', color='00000000'), |
||
91 | top=Side(border_style='medium', color='00000000') |
||
92 | ) |
||
93 | b_border = Border( |
||
94 | bottom=Side(border_style='medium', color='00000000'), |
||
95 | ) |
||
96 | |||
97 | b_c_alignment = Alignment(vertical='bottom', |
||
98 | horizontal='center', |
||
99 | text_rotation=0, |
||
100 | wrap_text=False, |
||
101 | shrink_to_fit=False, |
||
102 | indent=0) |
||
103 | c_c_alignment = Alignment(vertical='center', |
||
104 | horizontal='center', |
||
105 | text_rotation=0, |
||
106 | wrap_text=False, |
||
107 | shrink_to_fit=False, |
||
108 | indent=0) |
||
109 | b_r_alignment = Alignment(vertical='bottom', |
||
110 | horizontal='right', |
||
111 | text_rotation=0, |
||
112 | wrap_text=False, |
||
113 | shrink_to_fit=False, |
||
114 | indent=0) |
||
115 | c_r_alignment = Alignment(vertical='bottom', |
||
116 | horizontal='center', |
||
117 | text_rotation=0, |
||
118 | wrap_text=False, |
||
119 | shrink_to_fit=False, |
||
120 | indent=0) |
||
121 | |||
122 | # Img |
||
123 | img = Image("excelexporters/myems.png") |
||
124 | ws.add_image(img, 'B1') |
||
125 | |||
126 | # Title |
||
127 | ws['B3'].font = name_font |
||
128 | ws['B3'].alignment = b_r_alignment |
||
129 | ws['B3'] = 'Name:' |
||
130 | ws['C3'].border = b_border |
||
131 | ws['C3'].alignment = b_c_alignment |
||
132 | ws['C3'].font = name_font |
||
133 | ws['C3'] = name |
||
134 | |||
135 | ws['D3'].font = name_font |
||
136 | ws['D3'].alignment = b_r_alignment |
||
137 | ws['D3'] = 'Period:' |
||
138 | ws['E3'].border = b_border |
||
139 | ws['E3'].alignment = b_c_alignment |
||
140 | ws['E3'].font = name_font |
||
141 | ws['E3'] = period_type |
||
142 | |||
143 | ws['F3'].font = name_font |
||
144 | ws['F3'].alignment = b_r_alignment |
||
145 | ws['F3'] = 'Date:' |
||
146 | ws.merge_cells("G3:J3") |
||
147 | for i in range(ord('G'), ord('K')): |
||
148 | ws[chr(i) + '3'].border = b_border |
||
149 | ws['G3'].alignment = b_c_alignment |
||
150 | ws['G3'].font = name_font |
||
151 | ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local |
||
152 | |||
153 | if "reporting_period" not in report.keys() or \ |
||
154 | "timestamps" not in report['reporting_period'].keys() or len(report['reporting_period']['timestamps']) == 0: |
||
155 | filename = str(uuid.uuid4()) + '.xlsx' |
||
156 | wb.save(filename) |
||
157 | |||
158 | return filename |
||
159 | |||
160 | ############################### |
||
161 | |||
162 | has_names_data_flag = True |
||
163 | |||
164 | if "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
165 | has_names_data_flag = False |
||
166 | |||
167 | current_row_number = 6 |
||
168 | if has_names_data_flag: |
||
169 | reporting_period_data = report['reporting_period'] |
||
170 | category = reporting_period_data['names'] |
||
171 | ca_len = len(category) |
||
172 | |||
173 | ws['B' + str(current_row_number)].font = title_font |
||
174 | ws['B' + str(current_row_number)] = name + '报告期平均负荷' |
||
175 | |||
176 | current_row_number += 1 |
||
177 | |||
178 | ws['B' + str(current_row_number)].fill = table_fill |
||
179 | for i in range(0, ca_len): |
||
180 | col = chr(ord('C') + i) |
||
181 | ws[col + str(current_row_number)].fill = table_fill |
||
182 | ws[col + str(current_row_number)].font = name_font |
||
183 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
184 | ws[col + str(current_row_number)].border = f_border |
||
185 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
186 | " (" + reporting_period_data['units'][i] + "/H)" |
||
187 | |||
188 | current_row_number += 1 |
||
189 | |||
190 | ws['B' + str(current_row_number)].font = title_font |
||
191 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
192 | ws['B' + str(current_row_number)].border = f_border |
||
193 | ws['B' + str(current_row_number)] = '平均负荷' |
||
194 | |||
195 | for i in range(0, ca_len): |
||
196 | col = chr(ord('C') + i) |
||
197 | ws[col + str(current_row_number)].font = name_font |
||
198 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
199 | ws[col + str(current_row_number)].border = f_border |
||
200 | ws[col + str(current_row_number)] = round(reporting_period_data['averages'][i], 2) |
||
201 | |||
202 | current_row_number += 1 |
||
203 | |||
204 | ws['B' + str(current_row_number)].font = title_font |
||
205 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
206 | ws['B' + str(current_row_number)].border = f_border |
||
207 | ws['B' + str(current_row_number)] = '单位面积值' |
||
208 | |||
209 | for i in range(0, ca_len): |
||
210 | col = chr(ord('C') + i) |
||
211 | ws[col + str(current_row_number)].font = name_font |
||
212 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
213 | ws[col + str(current_row_number)].border = f_border |
||
214 | ws[col + str(current_row_number)] = round(reporting_period_data['averages_per_unit_area'][i], 2) |
||
215 | |||
216 | current_row_number += 1 |
||
217 | |||
218 | ws['B' + str(current_row_number)].font = title_font |
||
219 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
220 | ws['B' + str(current_row_number)].border = f_border |
||
221 | ws['B' + str(current_row_number)] = '环比' |
||
222 | |||
223 | for i in range(0, ca_len): |
||
224 | col = chr(ord('C') + i) |
||
225 | ws[col + str(current_row_number)].font = name_font |
||
226 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
227 | ws[col + str(current_row_number)].border = f_border |
||
228 | ws[col + str(current_row_number)] = str( |
||
229 | round(reporting_period_data['averages_increment_rate'][i] * 100, 2)) + "%" \ |
||
230 | if reporting_period_data['averages_increment_rate'][i] is not None else "-" |
||
231 | |||
232 | current_row_number += 2 |
||
233 | |||
234 | ws['B' + str(current_row_number)].font = title_font |
||
235 | ws['B' + str(current_row_number)] = name + '报告期最大负荷' |
||
236 | |||
237 | current_row_number += 1 |
||
238 | |||
239 | ws['B' + str(current_row_number)].fill = table_fill |
||
240 | for i in range(0, ca_len): |
||
241 | col = chr(ord('C') + i) |
||
242 | ws[col + str(current_row_number)].fill = table_fill |
||
243 | ws[col + str(current_row_number)].font = name_font |
||
244 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
245 | ws[col + str(current_row_number)].border = f_border |
||
246 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
247 | " (" + reporting_period_data['units'][i] + "/H)" |
||
248 | |||
249 | current_row_number += 1 |
||
250 | |||
251 | ws['B' + str(current_row_number)].font = title_font |
||
252 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
253 | ws['B' + str(current_row_number)].border = f_border |
||
254 | ws['B' + str(current_row_number)] = '最大负荷' |
||
255 | |||
256 | for i in range(0, ca_len): |
||
257 | col = chr(ord('C') + i) |
||
258 | ws[col + str(current_row_number)].font = name_font |
||
259 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
260 | ws[col + str(current_row_number)].border = f_border |
||
261 | ws[col + str(current_row_number)] = round(reporting_period_data['maximums'][i], 2) |
||
262 | |||
263 | current_row_number += 1 |
||
264 | |||
265 | ws['B' + str(current_row_number)].font = title_font |
||
266 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
267 | ws['B' + str(current_row_number)].border = f_border |
||
268 | ws['B' + str(current_row_number)] = '单位面积值' |
||
269 | |||
270 | for i in range(0, ca_len): |
||
271 | col = chr(ord('C') + i) |
||
272 | ws[col + str(current_row_number)].font = name_font |
||
273 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
274 | ws[col + str(current_row_number)].border = f_border |
||
275 | ws[col + str(current_row_number)] = round(reporting_period_data['maximums_per_unit_area'][i], 2) |
||
276 | |||
277 | current_row_number += 1 |
||
278 | |||
279 | ws['B' + str(current_row_number)].font = title_font |
||
280 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
281 | ws['B' + str(current_row_number)].border = f_border |
||
282 | ws['B' + str(current_row_number)] = '环比' |
||
283 | |||
284 | for i in range(0, ca_len): |
||
285 | col = chr(ord('C') + i) |
||
286 | ws[col + str(current_row_number)].font = name_font |
||
287 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
288 | ws[col + str(current_row_number)].border = f_border |
||
289 | ws[col + str(current_row_number)] = str( |
||
290 | round(reporting_period_data['maximums_increment_rate'][i] * 100, 2)) + "%" \ |
||
291 | if reporting_period_data['maximums_increment_rate'][i] is not None else "-" |
||
292 | |||
293 | current_row_number += 2 |
||
294 | |||
295 | ws['B' + str(current_row_number)].font = title_font |
||
296 | ws['B' + str(current_row_number)] = name + '报告期负荷系数' |
||
297 | |||
298 | current_row_number += 1 |
||
299 | |||
300 | ws['B' + str(current_row_number)].fill = table_fill |
||
301 | for i in range(0, ca_len): |
||
302 | col = chr(ord('C') + i) |
||
303 | ws[col + str(current_row_number)].fill = table_fill |
||
304 | ws[col + str(current_row_number)].font = name_font |
||
305 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
306 | ws[col + str(current_row_number)].border = f_border |
||
307 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] |
||
308 | |||
309 | current_row_number += 1 |
||
310 | |||
311 | ws['B' + str(current_row_number)].font = title_font |
||
312 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
313 | ws['B' + str(current_row_number)].border = f_border |
||
314 | ws['B' + str(current_row_number)] = '负荷系数' |
||
315 | |||
316 | for i in range(0, ca_len): |
||
317 | col = chr(ord('C') + i) |
||
318 | ws[col + str(current_row_number)].font = name_font |
||
319 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
320 | ws[col + str(current_row_number)].border = f_border |
||
321 | ws[col + str(current_row_number)] = round(reporting_period_data['factors'][i], 2) \ |
||
322 | if reporting_period_data['factors'][i] is not None else '-' |
||
323 | |||
324 | current_row_number += 1 |
||
325 | |||
326 | ws['B' + str(current_row_number)].font = title_font |
||
327 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
328 | ws['B' + str(current_row_number)].border = f_border |
||
329 | ws['B' + str(current_row_number)] = '环比' |
||
330 | |||
331 | for i in range(0, ca_len): |
||
332 | col = chr(ord('C') + i) |
||
333 | ws[col + str(current_row_number)].font = name_font |
||
334 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
335 | ws[col + str(current_row_number)].border = f_border |
||
336 | ws[col + str(current_row_number)] = str( |
||
337 | round(reporting_period_data['factors_increment_rate'][i] * 100, 2)) + "%" \ |
||
338 | if reporting_period_data['factors_increment_rate'][i] is not None else "-" |
||
339 | |||
340 | current_row_number += 2 |
||
341 | |||
342 | has_sub_averages_data_flag = True |
||
343 | has_sub_maximums_data_flag = True |
||
344 | |||
345 | if "sub_averages" not in report['reporting_period'].keys() or len(report['reporting_period']['sub_averages']) == 0: |
||
346 | has_sub_averages_data_flag = False |
||
347 | |||
348 | if "sub_averages" not in report['reporting_period'].keys() or len(report['reporting_period']['sub_averages']) == 0: |
||
349 | has_sub_maximums_data_flag = False |
||
350 | |||
351 | if has_sub_averages_data_flag or has_sub_maximums_data_flag: |
||
352 | reporting_period_data = report['reporting_period'] |
||
353 | category = reporting_period_data['names'] |
||
354 | ca_len = len(category) |
||
355 | times = reporting_period_data['timestamps'] |
||
356 | time = times[0] |
||
357 | |||
358 | ws['B' + str(current_row_number)].font = title_font |
||
359 | ws['B' + str(current_row_number)] = name + '详细数据' |
||
360 | |||
361 | current_row_number += 1 |
||
362 | chart_start_number = current_row_number |
||
363 | |||
364 | if has_sub_averages_data_flag: |
||
365 | current_row_number = (current_row_number + ca_len * 5) |
||
366 | |||
367 | if has_sub_maximums_data_flag: |
||
368 | current_row_number = (current_row_number + ca_len * 5) |
||
369 | |||
370 | table_start_number = current_row_number |
||
371 | |||
372 | ws['B' + str(current_row_number)].fill = table_fill |
||
373 | ws['B' + str(current_row_number)].font = title_font |
||
374 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
375 | ws['B' + str(current_row_number)].border = f_border |
||
376 | ws['B' + str(current_row_number)] = '日期时间' |
||
377 | |||
378 | col = 'C' |
||
379 | |||
380 | for i in range(0, ca_len): |
||
381 | if has_sub_averages_data_flag: |
||
382 | ws[col + str(current_row_number)].fill = table_fill |
||
383 | ws[col + str(current_row_number)].font = table_title_font |
||
384 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
385 | ws[col + str(current_row_number)].border = f_border |
||
386 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
387 | " 平均负荷(" + reporting_period_data['units'][i] + "/H)" |
||
388 | col = chr(ord(col) + 1) |
||
389 | |||
390 | if has_sub_maximums_data_flag: |
||
391 | ws[col + str(current_row_number)].fill = table_fill |
||
392 | ws[col + str(current_row_number)].font = table_title_font |
||
393 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
394 | ws[col + str(current_row_number)].border = f_border |
||
395 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
396 | " 最大负荷(" + reporting_period_data['units'][i] + "/H)" |
||
397 | col = chr(ord(col) + 1) |
||
398 | |||
399 | current_row_number += 1 |
||
400 | |||
401 | for i in range(0, len(time)): |
||
402 | ws['B' + str(current_row_number)].font = title_font |
||
403 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
404 | ws['B' + str(current_row_number)].border = f_border |
||
405 | ws['B' + str(current_row_number)] = time[i] |
||
406 | |||
407 | col = 'C' |
||
408 | for j in range(0, ca_len): |
||
409 | |||
410 | if has_sub_averages_data_flag: |
||
411 | ws[col + str(current_row_number)].font = title_font |
||
412 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
413 | ws[col + str(current_row_number)].border = f_border |
||
414 | ws[col + str(current_row_number)] = round(reporting_period_data['sub_averages'][j][i], 2) \ |
||
415 | if reporting_period_data['sub_averages'][j][i] is not None else 0.00 |
||
416 | col = chr(ord(col) + 1) |
||
417 | |||
418 | if has_sub_maximums_data_flag: |
||
419 | ws[col + str(current_row_number)].font = title_font |
||
420 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
421 | ws[col + str(current_row_number)].border = f_border |
||
422 | ws[col + str(current_row_number)] = round(reporting_period_data['sub_maximums'][j][i], 2) \ |
||
423 | if reporting_period_data['sub_maximums'][j][i] is not None else 0.00 |
||
424 | col = chr(ord(col) + 1) |
||
425 | |||
426 | current_row_number += 1 |
||
427 | |||
428 | table_end_number = current_row_number - 1 |
||
429 | |||
430 | current_chart_col_number = 3 |
||
431 | current_chart_row_number = chart_start_number |
||
432 | |||
433 | for i in range(0, ca_len): |
||
434 | labels = Reference(ws, min_col=2, min_row=table_start_number + 1, max_row=table_end_number) |
||
435 | |||
436 | if has_sub_averages_data_flag: |
||
437 | bar = BarChart() |
||
438 | datas = Reference(ws, min_col=current_chart_col_number, min_row=table_start_number, |
||
439 | max_row=table_end_number) |
||
440 | bar.add_data(datas, titles_from_data=True) |
||
441 | bar.set_categories(labels) |
||
442 | bar.height = 5.25 |
||
443 | bar.width = len(time) |
||
444 | bar.dLbls = DataLabelList() |
||
445 | bar.dLbls.showVal = True |
||
446 | ws.add_chart(bar, "B" + str(current_chart_row_number)) |
||
447 | current_chart_row_number += 5 |
||
448 | current_chart_col_number += 1 |
||
449 | |||
450 | if has_sub_maximums_data_flag: |
||
451 | bar = BarChart() |
||
452 | datas = Reference(ws, min_col=current_chart_col_number, min_row=table_start_number, |
||
453 | max_row=table_end_number) |
||
454 | bar.add_data(datas, titles_from_data=True) |
||
455 | bar.set_categories(labels) |
||
456 | bar.height = 5.25 |
||
457 | bar.width = len(time) |
||
458 | bar.dLbls = DataLabelList() |
||
459 | bar.dLbls.showVal = True |
||
460 | ws.add_chart(bar, "B" + str(current_chart_row_number)) |
||
461 | current_chart_row_number += 5 |
||
462 | current_chart_col_number += 1 |
||
463 | |||
464 | filename = str(uuid.uuid4()) + '.xlsx' |
||
465 | wb.save(filename) |
||
466 | |||
467 | return filename |
||
468 |