Total Complexity | 93 |
Total Lines | 946 |
Duplicated Lines | 93.55 % |
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.equipmentsaving 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 | from core.utilities import get_translation |
||
3 | import os |
||
4 | import re |
||
5 | import uuid |
||
6 | from decimal import Decimal |
||
7 | import openpyxl.utils.cell as format_cell |
||
8 | from openpyxl import Workbook |
||
9 | from openpyxl.chart import PieChart, LineChart, Reference |
||
10 | from openpyxl.chart.label import DataLabelList |
||
11 | from openpyxl.drawing.image import Image |
||
12 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
||
13 | from core.utilities import round2 |
||
14 | |||
15 | ######################################################################################################################## |
||
16 | # PROCEDURES |
||
17 | # Step 1: Validate the report data |
||
18 | # Step 2: Generate excel file |
||
19 | # Step 3: Encode the excel file to Base64 |
||
20 | ######################################################################################################################## |
||
21 | |||
22 | |||
23 | View Code Duplication | def export(report, |
|
|
|||
24 | name, |
||
25 | base_period_start_datetime_local, |
||
26 | base_period_end_datetime_local, |
||
27 | reporting_start_datetime_local, |
||
28 | reporting_end_datetime_local, |
||
29 | period_type, |
||
30 | language): |
||
31 | #################################################################################################################### |
||
32 | # Step 1: Validate the report data |
||
33 | #################################################################################################################### |
||
34 | if report is None: |
||
35 | return None |
||
36 | |||
37 | #################################################################################################################### |
||
38 | # Step 2: Generate excel file from the report data |
||
39 | #################################################################################################################### |
||
40 | filename = generate_excel(report, |
||
41 | name, |
||
42 | base_period_start_datetime_local, |
||
43 | base_period_end_datetime_local, |
||
44 | reporting_start_datetime_local, |
||
45 | reporting_end_datetime_local, |
||
46 | period_type, |
||
47 | language) |
||
48 | #################################################################################################################### |
||
49 | # Step 3: Encode the excel file to Base64 |
||
50 | #################################################################################################################### |
||
51 | binary_file_data = b'' |
||
52 | try: |
||
53 | with open(filename, 'rb') as binary_file: |
||
54 | binary_file_data = binary_file.read() |
||
55 | except IOError as ex: |
||
56 | print(str(ex)) |
||
57 | |||
58 | # Base64 encode the bytes |
||
59 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
60 | # get the Base64 encoded data using human-readable characters. |
||
61 | base64_message = base64_encoded_data.decode('utf-8') |
||
62 | # delete the file from server |
||
63 | try: |
||
64 | os.remove(filename) |
||
65 | except NotImplementedError as ex: |
||
66 | print(str(ex)) |
||
67 | return base64_message |
||
68 | |||
69 | |||
70 | View Code Duplication | def generate_excel(report, |
|
71 | name, |
||
72 | base_period_start_datetime_local, |
||
73 | base_period_end_datetime_local, |
||
74 | reporting_start_datetime_local, |
||
75 | reporting_end_datetime_local, |
||
76 | period_type, |
||
77 | language): |
||
78 | trans = get_translation(language) |
||
79 | trans.install() |
||
80 | _ = trans.gettext |
||
81 | wb = Workbook() |
||
82 | ws = wb.active |
||
83 | ws.title = "EquipmentSaving" |
||
84 | |||
85 | # Row height |
||
86 | ws.row_dimensions[1].height = 102 |
||
87 | for i in range(2, 2000 + 1): |
||
88 | ws.row_dimensions[i].height = 42 |
||
89 | |||
90 | # Col width |
||
91 | ws.column_dimensions['A'].width = 1.5 |
||
92 | |||
93 | ws.column_dimensions['B'].width = 25.0 |
||
94 | |||
95 | for i in range(ord('C'), ord('Z')): |
||
96 | ws.column_dimensions[chr(i)].width = 15.0 |
||
97 | |||
98 | # Font |
||
99 | name_font = Font(name='Arial', size=15, bold=True) |
||
100 | title_font = Font(name='Arial', size=15, bold=True) |
||
101 | |||
102 | table_fill = PatternFill(fill_type='solid', fgColor='90ee90') |
||
103 | f_border = Border(left=Side(border_style='medium'), |
||
104 | right=Side(border_style='medium'), |
||
105 | bottom=Side(border_style='medium'), |
||
106 | top=Side(border_style='medium') |
||
107 | ) |
||
108 | b_border = Border(bottom=Side(border_style='medium'), ) |
||
109 | |||
110 | b_c_alignment = Alignment(vertical='bottom', |
||
111 | horizontal='center', |
||
112 | text_rotation=0, |
||
113 | wrap_text=True, |
||
114 | shrink_to_fit=False, |
||
115 | indent=0) |
||
116 | c_c_alignment = Alignment(vertical='center', |
||
117 | horizontal='center', |
||
118 | text_rotation=0, |
||
119 | wrap_text=True, |
||
120 | shrink_to_fit=False, |
||
121 | indent=0) |
||
122 | b_r_alignment = Alignment(vertical='bottom', |
||
123 | horizontal='right', |
||
124 | text_rotation=0, |
||
125 | wrap_text=True, |
||
126 | shrink_to_fit=False, |
||
127 | indent=0) |
||
128 | |||
129 | # Img |
||
130 | img = Image("excelexporters/myems.png") |
||
131 | ws.add_image(img, 'A1') |
||
132 | |||
133 | # Title |
||
134 | ws['B3'].alignment = b_r_alignment |
||
135 | ws['B3'] = _('Name') + ':' |
||
136 | ws['C3'].border = b_border |
||
137 | ws['C3'].alignment = b_c_alignment |
||
138 | ws['C3'] = name |
||
139 | |||
140 | ws['D3'].alignment = b_r_alignment |
||
141 | ws['D3'] = _('Period Type') + ':' |
||
142 | ws['E3'].border = b_border |
||
143 | ws['E3'].alignment = b_c_alignment |
||
144 | ws['E3'] = period_type |
||
145 | |||
146 | ws['B4'].alignment = b_r_alignment |
||
147 | ws['B4'] = _('Reporting Start Datetime') + ':' |
||
148 | ws['C4'].border = b_border |
||
149 | ws['C4'].alignment = b_c_alignment |
||
150 | ws['C4'] = reporting_start_datetime_local |
||
151 | |||
152 | ws['D4'].alignment = b_r_alignment |
||
153 | ws['D4'] = _('Reporting End Datetime') + ':' |
||
154 | ws['E4'].border = b_border |
||
155 | ws['E4'].alignment = b_c_alignment |
||
156 | ws['E4'] = reporting_end_datetime_local |
||
157 | |||
158 | is_base_period_timestamp_exists_flag = is_base_period_timestamp_exists(report['base_period']) |
||
159 | |||
160 | if is_base_period_timestamp_exists_flag: |
||
161 | ws['B5'].alignment = b_r_alignment |
||
162 | ws['B5'] = _('Base Period Start Datetime') + ':' |
||
163 | ws['C5'].border = b_border |
||
164 | ws['C5'].alignment = b_c_alignment |
||
165 | ws['C5'] = base_period_start_datetime_local |
||
166 | |||
167 | ws['D5'].alignment = b_r_alignment |
||
168 | ws['D5'] = _('Base Period End Datetime') + ':' |
||
169 | ws['E5'].border = b_border |
||
170 | ws['E5'].alignment = b_c_alignment |
||
171 | ws['E5'] = base_period_end_datetime_local |
||
172 | |||
173 | if "reporting_period" not in report.keys() or \ |
||
174 | "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0: |
||
175 | filename = str(uuid.uuid4()) + '.xlsx' |
||
176 | wb.save(filename) |
||
177 | |||
178 | return filename |
||
179 | |||
180 | #################################################################################################################### |
||
181 | |||
182 | current_row_number = 7 |
||
183 | reporting_period_data = report['reporting_period'] |
||
184 | if "names" not in reporting_period_data.keys() or \ |
||
185 | reporting_period_data['names'] is None or \ |
||
186 | len(reporting_period_data['names']) == 0: |
||
187 | pass |
||
188 | else: |
||
189 | ws['B' + str(current_row_number)].font = title_font |
||
190 | ws['B' + str(current_row_number)] = name + ' ' + _('Reporting Period Saving') |
||
191 | |||
192 | current_row_number += 1 |
||
193 | category = reporting_period_data['names'] |
||
194 | ca_len = len(category) |
||
195 | |||
196 | ws.row_dimensions[current_row_number].height = 75 |
||
197 | ws['B' + str(current_row_number)].fill = table_fill |
||
198 | ws['B' + str(current_row_number)].border = f_border |
||
199 | |||
200 | col = 'C' |
||
201 | |||
202 | for i in range(0, ca_len): |
||
203 | ws[col + str(current_row_number)].fill = table_fill |
||
204 | ws[col + str(current_row_number)].font = name_font |
||
205 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
206 | ws[col + str(current_row_number)].border = f_border |
||
207 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + " (" + _('Baseline') + ' - ' \ |
||
208 | + _('Actual') + ") (" + reporting_period_data['units'][i] + ")" |
||
209 | |||
210 | col = chr(ord(col) + 1) |
||
211 | |||
212 | ws[col + str(current_row_number)].fill = table_fill |
||
213 | ws[col + str(current_row_number)].font = name_font |
||
214 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
215 | ws[col + str(current_row_number)].border = f_border |
||
216 | ws[col + str(current_row_number)] = _('Ton of Standard Coal') + ' (' + _('Baseline') + ' - ' \ |
||
217 | + _('Actual') + ') (TCE)' |
||
218 | |||
219 | col = chr(ord(col) + 1) |
||
220 | |||
221 | ws[col + str(current_row_number)].fill = table_fill |
||
222 | ws[col + str(current_row_number)].font = name_font |
||
223 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
224 | ws[col + str(current_row_number)].border = f_border |
||
225 | ws[col + str(current_row_number)] = _('Ton of Carbon Dioxide Emissions') + ' (' + _('Baseline') + ' - ' \ |
||
226 | + _('Actual') + ') (TCO2E)' |
||
227 | |||
228 | col = chr(ord(col) + 1) |
||
229 | |||
230 | current_row_number += 1 |
||
231 | |||
232 | ws['B' + str(current_row_number)].font = title_font |
||
233 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
234 | ws['B' + str(current_row_number)].border = f_border |
||
235 | ws['B' + str(current_row_number)] = _('Saving') |
||
236 | |||
237 | col = 'C' |
||
238 | |||
239 | for i in range(0, ca_len): |
||
240 | ws[col + str(current_row_number)].font = name_font |
||
241 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
242 | ws[col + str(current_row_number)].border = f_border |
||
243 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals_saving'][i], 2) |
||
244 | |||
245 | col = chr(ord(col) + 1) |
||
246 | |||
247 | ws[col + str(current_row_number)].font = name_font |
||
248 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
249 | ws[col + str(current_row_number)].border = f_border |
||
250 | ws[col + str(current_row_number)] = round2(reporting_period_data['total_in_kgce_saving'] / 1000, 2) |
||
251 | |||
252 | col = chr(ord(col) + 1) |
||
253 | |||
254 | ws[col + str(current_row_number)].font = name_font |
||
255 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
256 | ws[col + str(current_row_number)].border = f_border |
||
257 | ws[col + str(current_row_number)] = round2(reporting_period_data['total_in_kgco2e_saving'] / 1000, 2) |
||
258 | |||
259 | col = chr(ord(col) + 1) |
||
260 | |||
261 | current_row_number += 1 |
||
262 | |||
263 | ws['B' + str(current_row_number)].font = title_font |
||
264 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
265 | ws['B' + str(current_row_number)].border = f_border |
||
266 | ws['B' + str(current_row_number)] = _('Increment Rate') |
||
267 | |||
268 | col = 'C' |
||
269 | |||
270 | for i in range(0, ca_len): |
||
271 | ws[col + str(current_row_number)].font = name_font |
||
272 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
273 | ws[col + str(current_row_number)].border = f_border |
||
274 | ws[col + str(current_row_number)] = str( |
||
275 | round2(reporting_period_data['increment_rates_saving'][i] * 100, 2)) + '%' \ |
||
276 | if reporting_period_data['increment_rates_saving'][i] is not None else '-' |
||
277 | |||
278 | col = chr(ord(col) + 1) |
||
279 | |||
280 | ws[col + str(current_row_number)].font = name_font |
||
281 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
282 | ws[col + str(current_row_number)].border = f_border |
||
283 | ws[col + str(current_row_number)] = str( |
||
284 | round2(reporting_period_data['increment_rate_in_kgce_saving'] * 100, 2)) + '%' \ |
||
285 | if reporting_period_data['increment_rate_in_kgce_saving'] is not None else '-' |
||
286 | |||
287 | col = chr(ord(col) + 1) |
||
288 | |||
289 | ws[col + str(current_row_number)].font = name_font |
||
290 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
291 | ws[col + str(current_row_number)].border = f_border |
||
292 | ws[col + str(current_row_number)] = str( |
||
293 | round2(reporting_period_data['increment_rate_in_kgco2e_saving'] * 100, 2)) + '%' \ |
||
294 | if reporting_period_data['increment_rate_in_kgco2e_saving'] is not None else '-' |
||
295 | |||
296 | col = chr(ord(col) + 1) |
||
297 | |||
298 | current_row_number += 2 |
||
299 | |||
300 | ws['B' + str(current_row_number)].font = title_font |
||
301 | ws['B' + str(current_row_number)] = name + ' ' + _('Ton of Standard Coal(TCE) by Energy Category') |
||
302 | |||
303 | current_row_number += 1 |
||
304 | table_start_row_number = current_row_number |
||
305 | chart_start_row_number = current_row_number |
||
306 | |||
307 | ws.row_dimensions[current_row_number].height = 60 |
||
308 | ws['B' + str(current_row_number)].fill = table_fill |
||
309 | ws['B' + str(current_row_number)].border = f_border |
||
310 | |||
311 | ws['C' + str(current_row_number)].fill = table_fill |
||
312 | ws['C' + str(current_row_number)].font = name_font |
||
313 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
314 | ws['C' + str(current_row_number)].border = f_border |
||
315 | ws['C' + str(current_row_number)] = _('Saving') |
||
316 | |||
317 | ws['D' + str(current_row_number)].fill = table_fill |
||
318 | ws['D' + str(current_row_number)].font = name_font |
||
319 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
320 | ws['D' + str(current_row_number)].border = f_border |
||
321 | ws['D' + str(current_row_number)] = _('Ton of Standard Coal(TCE) by Energy Category') |
||
322 | |||
323 | current_row_number += 1 |
||
324 | |||
325 | subtotals_in_kgce_saving_sum = sum_list(reporting_period_data['subtotals_in_kgce_saving']) |
||
326 | |||
327 | for i in range(0, ca_len): |
||
328 | ws['B' + str(current_row_number)].font = title_font |
||
329 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
330 | ws['B' + str(current_row_number)].border = f_border |
||
331 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
332 | |||
333 | ws['C' + str(current_row_number)].font = name_font |
||
334 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
335 | ws['C' + str(current_row_number)].border = f_border |
||
336 | ws['C' + str(current_row_number)] = round2(reporting_period_data['subtotals_in_kgce_saving'][i] / 1000, 3) |
||
337 | |||
338 | ws['D' + str(current_row_number)].font = name_font |
||
339 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
340 | ws['D' + str(current_row_number)].border = f_border |
||
341 | ws['D' + str(current_row_number)] = str(round2(reporting_period_data['subtotals_in_kgce_saving'][i] / |
||
342 | subtotals_in_kgce_saving_sum * 100, 2)) + '%' \ |
||
343 | if abs(subtotals_in_kgce_saving_sum) > 0 else '-' |
||
344 | |||
345 | current_row_number += 1 |
||
346 | |||
347 | table_end_row_number = current_row_number - 1 |
||
348 | |||
349 | if ca_len < 4: |
||
350 | current_row_number = current_row_number - ca_len + 4 |
||
351 | |||
352 | current_row_number += 1 |
||
353 | |||
354 | pie = PieChart() |
||
355 | pie.title = name + ' ' + _('Ton of Standard Coal(TCE) by Energy Category') |
||
356 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
357 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
358 | pie.add_data(pie_data, titles_from_data=True) |
||
359 | pie.set_categories(labels) |
||
360 | pie.height = 7.25 |
||
361 | pie.width = 9 |
||
362 | s1 = pie.series[0] |
||
363 | s1.dLbls = DataLabelList() |
||
364 | s1.dLbls.showCatName = False |
||
365 | s1.dLbls.showVal = False |
||
366 | s1.dLbls.showPercent = True |
||
367 | ws.add_chart(pie, 'E' + str(chart_start_row_number)) |
||
368 | |||
369 | ws['B' + str(current_row_number)].font = title_font |
||
370 | ws['B' + str(current_row_number)] = name + ' ' + _('Ton of Carbon Dioxide Emissions(TCO2E) by Energy Category') |
||
371 | |||
372 | current_row_number += 1 |
||
373 | table_start_row_number = current_row_number |
||
374 | chart_start_row_number = current_row_number |
||
375 | |||
376 | ws.row_dimensions[current_row_number].height = 60 |
||
377 | ws['B' + str(current_row_number)].fill = table_fill |
||
378 | ws['B' + str(current_row_number)].border = f_border |
||
379 | |||
380 | ws['C' + str(current_row_number)].fill = table_fill |
||
381 | ws['C' + str(current_row_number)].font = name_font |
||
382 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
383 | ws['C' + str(current_row_number)].border = f_border |
||
384 | ws['C' + str(current_row_number)] = _('Saving') |
||
385 | |||
386 | ws['D' + str(current_row_number)].fill = table_fill |
||
387 | ws['D' + str(current_row_number)].font = name_font |
||
388 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
389 | ws['D' + str(current_row_number)].border = f_border |
||
390 | ws['D' + str(current_row_number)] = _('Ton of Carbon Dioxide Emissions(TCO2E) by Energy Category') |
||
391 | |||
392 | current_row_number += 1 |
||
393 | |||
394 | subtotals_in_kgco2e_saving_sum = sum_list(reporting_period_data['subtotals_in_kgco2e_saving']) |
||
395 | |||
396 | for i in range(0, ca_len): |
||
397 | ws['B' + str(current_row_number)].font = title_font |
||
398 | ws['B' + str(current_row_number)].alignment = c_c_alignment |
||
399 | ws['B' + str(current_row_number)].border = f_border |
||
400 | ws['B' + str(current_row_number)] = reporting_period_data['names'][i] |
||
401 | |||
402 | ws['C' + str(current_row_number)].font = name_font |
||
403 | ws['C' + str(current_row_number)].alignment = c_c_alignment |
||
404 | ws['C' + str(current_row_number)].border = f_border |
||
405 | ws['C' + str(current_row_number)] = round2(reporting_period_data['subtotals_in_kgco2e_saving'][i] / 1000, 3) |
||
406 | |||
407 | ws['D' + str(current_row_number)].font = name_font |
||
408 | ws['D' + str(current_row_number)].alignment = c_c_alignment |
||
409 | ws['D' + str(current_row_number)].border = f_border |
||
410 | ws['D' + str(current_row_number)] = str(round2(reporting_period_data['subtotals_in_kgco2e_saving'][i] / |
||
411 | subtotals_in_kgco2e_saving_sum * 100, 2)) + '%' \ |
||
412 | if abs(subtotals_in_kgco2e_saving_sum) > 0 else '-' |
||
413 | |||
414 | current_row_number += 1 |
||
415 | |||
416 | table_end_row_number = current_row_number - 1 |
||
417 | |||
418 | if ca_len < 4: |
||
419 | current_row_number = current_row_number - ca_len + 4 |
||
420 | |||
421 | current_row_number += 1 |
||
422 | |||
423 | pie = PieChart() |
||
424 | pie.title = name + ' ' + _('Ton of Carbon Dioxide Emissions(TCO2E) by Energy Category') |
||
425 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
426 | pie_data = Reference(ws, min_col=3, min_row=table_start_row_number, max_row=table_end_row_number) |
||
427 | pie.add_data(pie_data, titles_from_data=True) |
||
428 | pie.set_categories(labels) |
||
429 | pie.height = 7.25 |
||
430 | pie.width = 9 |
||
431 | s1 = pie.series[0] |
||
432 | s1.dLbls = DataLabelList() |
||
433 | s1.dLbls.showCatName = False |
||
434 | s1.dLbls.showVal = False |
||
435 | s1.dLbls.showPercent = True |
||
436 | ws.add_chart(pie, 'E' + str(chart_start_row_number)) |
||
437 | |||
438 | #################################################################################################################### |
||
439 | |||
440 | table_start_draw_flag = current_row_number + 1 |
||
441 | |||
442 | if 'values_saving' not in reporting_period_data.keys() or \ |
||
443 | reporting_period_data['values_saving'] is None or \ |
||
444 | len(reporting_period_data['values_saving']) == 0 or \ |
||
445 | 'timestamps' not in reporting_period_data.keys() or \ |
||
446 | reporting_period_data['timestamps'] is None or \ |
||
447 | len(reporting_period_data['timestamps']) == 0 or \ |
||
448 | len(reporting_period_data['timestamps'][0]) == 0: |
||
449 | pass |
||
450 | else: |
||
451 | if not is_base_period_timestamp_exists_flag: |
||
452 | reporting_period_data = report['reporting_period'] |
||
453 | times = reporting_period_data['timestamps'] |
||
454 | ca_len = len(report['reporting_period']['names']) |
||
455 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
456 | ws['B' + str(current_row_number)].font = title_font |
||
457 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
458 | |||
459 | current_row_number += 1 |
||
460 | # 1: Stand for blank line 2: Stand for title |
||
461 | current_row_number += ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
||
462 | table_start_row_number = current_row_number |
||
463 | |||
464 | time = times[0] |
||
465 | has_data = False |
||
466 | |||
467 | if len(time) > 0: |
||
468 | has_data = True |
||
469 | |||
470 | if has_data: |
||
471 | |||
472 | ws.row_dimensions[current_row_number].height = 60 |
||
473 | current_col_number = 2 |
||
474 | col = format_cell.get_column_letter(current_col_number) |
||
475 | ws[col + str(current_row_number)].fill = table_fill |
||
476 | ws[col + str(current_row_number)].font = title_font |
||
477 | ws[col + str(current_row_number)].border = f_border |
||
478 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
479 | ws[col + str(current_row_number)] = _('Datetime') |
||
480 | |||
481 | for i in range(0, ca_len): |
||
482 | current_col_number += 1 |
||
483 | col = format_cell.get_column_letter(current_col_number) |
||
484 | |||
485 | ws[col + str(current_row_number)].fill = table_fill |
||
486 | ws[col + str(current_row_number)].font = title_font |
||
487 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
488 | ws[col + str(current_row_number)] = reporting_period_data['names'][i] + \ |
||
489 | " (" + reporting_period_data['units'][i] + ")" |
||
490 | ws[col + str(current_row_number)].border = f_border |
||
491 | |||
492 | current_row_number += 1 |
||
493 | |||
494 | for i in range(0, len(time)): |
||
495 | current_col_number = 2 |
||
496 | col = format_cell.get_column_letter(current_col_number) |
||
497 | ws[col + str(current_row_number)].font = title_font |
||
498 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
499 | ws[col + str(current_row_number)] = time[i] |
||
500 | ws[col + str(current_row_number)].border = f_border |
||
501 | |||
502 | for j in range(0, ca_len): |
||
503 | current_col_number += 1 |
||
504 | col = format_cell.get_column_letter(current_col_number) |
||
505 | |||
506 | ws[col + str(current_row_number)].font = title_font |
||
507 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
508 | ws[col + str(current_row_number)] = round2(reporting_period_data['values_saving'][j][i], 2) |
||
509 | ws[col + str(current_row_number)].border = f_border |
||
510 | |||
511 | current_row_number += 1 |
||
512 | |||
513 | table_end_row_number = current_row_number - 1 |
||
514 | |||
515 | current_col_number = 2 |
||
516 | col = format_cell.get_column_letter(current_col_number) |
||
517 | ws[col + str(current_row_number)].font = title_font |
||
518 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
519 | ws[col + str(current_row_number)] = _('Subtotal') |
||
520 | ws[col + str(current_row_number)].border = f_border |
||
521 | |||
522 | for i in range(0, ca_len): |
||
523 | current_col_number += 1 |
||
524 | col = format_cell.get_column_letter(current_col_number) |
||
525 | |||
526 | ws[col + str(current_row_number)].font = title_font |
||
527 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
528 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals_saving'][i], 2) |
||
529 | ws[col + str(current_row_number)].border = f_border |
||
530 | |||
531 | # line |
||
532 | line = LineChart() |
||
533 | line.title = _('Reporting Period Saving') + ' - ' \ |
||
534 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
535 | labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number) |
||
536 | line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
||
537 | max_row=table_end_row_number) |
||
538 | line.add_data(line_data, titles_from_data=True) |
||
539 | line.set_categories(labels) |
||
540 | line_data = line.series[0] |
||
541 | line_data.marker.symbol = "auto" |
||
542 | line_data.smooth = True |
||
543 | line.x_axis.crosses = 'min' |
||
544 | line.height = 8.25 |
||
545 | line.width = 24 |
||
546 | chart_col = 'B' |
||
547 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
||
548 | ws.add_chart(line, chart_cell) |
||
549 | |||
550 | current_row_number += 2 |
||
551 | else: |
||
552 | base_period_data = report['base_period'] |
||
553 | reporting_period_data = report['reporting_period'] |
||
554 | base_period_timestamps = base_period_data['timestamps'] |
||
555 | reporting_period_timestamps = reporting_period_data['timestamps'] |
||
556 | # Tip: |
||
557 | # base_period_data['names'] == reporting_period_data['names'] |
||
558 | # base_period_data['units'] == reporting_period_data['units'] |
||
559 | base_period_data_ca_len = len(base_period_data['names']) |
||
560 | reporting_period_data_ca_len = len(reporting_period_data['names']) |
||
561 | real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps']) |
||
562 | ws['B' + str(current_row_number)].font = title_font |
||
563 | ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data') |
||
564 | |||
565 | current_row_number += 1 |
||
566 | # 1: Stand for blank line 2: Stand for title |
||
567 | current_row_number += reporting_period_data_ca_len * 6 + real_timestamps_len * 6 + 1 + 2 |
||
568 | table_start_row_number = current_row_number |
||
569 | |||
570 | has_data = False |
||
571 | |||
572 | if len(base_period_timestamps[0]) or len(reporting_period_timestamps[0]) > 0: |
||
573 | has_data = True |
||
574 | |||
575 | if has_data: |
||
576 | ws.row_dimensions[current_row_number].height = 60 |
||
577 | current_col_number = 2 |
||
578 | col = format_cell.get_column_letter(current_col_number) |
||
579 | ws[col + str(current_row_number)].fill = table_fill |
||
580 | ws[col + str(current_row_number)].font = title_font |
||
581 | ws[col + str(current_row_number)].border = f_border |
||
582 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
583 | ws[col + str(current_row_number)] = _('Base Period') + " - " + _('Datetime') |
||
584 | |||
585 | for i in range(0, base_period_data_ca_len): |
||
586 | current_col_number += 1 |
||
587 | col = format_cell.get_column_letter(current_col_number) |
||
588 | |||
589 | ws[col + str(current_row_number)].fill = table_fill |
||
590 | ws[col + str(current_row_number)].font = title_font |
||
591 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
592 | ws[col + str(current_row_number)] = _('Base Period') + " - " + base_period_data['names'][i] + \ |
||
593 | " (" + base_period_data['units'][i] + ")" |
||
594 | ws[col + str(current_row_number)].border = f_border |
||
595 | current_col_number += 1 |
||
596 | col = format_cell.get_column_letter(current_col_number) |
||
597 | |||
598 | ws[col + str(current_row_number)].fill = table_fill |
||
599 | ws[col + str(current_row_number)].font = title_font |
||
600 | ws[col + str(current_row_number)].border = f_border |
||
601 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
602 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " + _('Datetime') |
||
603 | |||
604 | for i in range(0, reporting_period_data_ca_len): |
||
605 | current_col_number += 1 |
||
606 | col = format_cell.get_column_letter(current_col_number) |
||
607 | ws[col + str(current_row_number)].fill = table_fill |
||
608 | ws[col + str(current_row_number)].font = title_font |
||
609 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
610 | ws[col + str(current_row_number)] = _('Reporting Period') + " - " \ |
||
611 | + reporting_period_data['names'][i] + " (" + \ |
||
612 | reporting_period_data['units'][i] + ")" |
||
613 | ws[col + str(current_row_number)].border = f_border |
||
614 | |||
615 | current_row_number += 1 |
||
616 | |||
617 | max_timestamps_len = len(base_period_timestamps[0]) \ |
||
618 | if len(base_period_timestamps[0]) >= len(reporting_period_timestamps[0]) \ |
||
619 | else len(reporting_period_timestamps[0]) |
||
620 | |||
621 | for i in range(0, max_timestamps_len): |
||
622 | current_col_number = 2 |
||
623 | col = format_cell.get_column_letter(current_col_number) |
||
624 | ws[col + str(current_row_number)].font = title_font |
||
625 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
626 | ws[col + str(current_row_number)] = base_period_timestamps[0][i] \ |
||
627 | if i < len(base_period_timestamps[0]) else None |
||
628 | ws[col + str(current_row_number)].border = f_border |
||
629 | |||
630 | for j in range(0, base_period_data_ca_len): |
||
631 | current_col_number += 1 |
||
632 | col = format_cell.get_column_letter(current_col_number) |
||
633 | |||
634 | ws[col + str(current_row_number)].font = title_font |
||
635 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
636 | ws[col + str(current_row_number)] = round2(base_period_data['values_saving'][j][i], 2) \ |
||
637 | if i < len(base_period_data['values_saving'][j]) else None |
||
638 | ws[col + str(current_row_number)].border = f_border |
||
639 | current_col_number += 1 |
||
640 | col = format_cell.get_column_letter(current_col_number) |
||
641 | |||
642 | ws[col + str(current_row_number)].font = title_font |
||
643 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
644 | ws[col + str(current_row_number)] = reporting_period_timestamps[0][i] \ |
||
645 | if i < len(reporting_period_timestamps[0]) else None |
||
646 | ws[col + str(current_row_number)].border = f_border |
||
647 | |||
648 | for j in range(0, reporting_period_data_ca_len): |
||
649 | current_col_number += 1 |
||
650 | col = format_cell.get_column_letter(current_col_number) |
||
651 | |||
652 | ws[col + str(current_row_number)].font = title_font |
||
653 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
654 | ws[col + str(current_row_number)] = round2(reporting_period_data['values_saving'][j][i], 2) \ |
||
655 | if i < len(reporting_period_data['values_saving'][j]) else None |
||
656 | ws[col + str(current_row_number)].border = f_border |
||
657 | |||
658 | current_row_number += 1 |
||
659 | |||
660 | current_col_number = 2 |
||
661 | col = format_cell.get_column_letter(current_col_number) |
||
662 | ws[col + str(current_row_number)].font = title_font |
||
663 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
664 | ws[col + str(current_row_number)] = _('Subtotal') |
||
665 | ws[col + str(current_row_number)].border = f_border |
||
666 | |||
667 | for i in range(0, base_period_data_ca_len): |
||
668 | current_col_number += 1 |
||
669 | col = format_cell.get_column_letter(current_col_number) |
||
670 | ws[col + str(current_row_number)].font = title_font |
||
671 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
672 | ws[col + str(current_row_number)] = round2(base_period_data['subtotals_saving'][i], 2) |
||
673 | ws[col + str(current_row_number)].border = f_border |
||
674 | |||
675 | current_col_number += 1 |
||
676 | col = format_cell.get_column_letter(current_col_number) |
||
677 | |||
678 | ws[col + str(current_row_number)].font = title_font |
||
679 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
680 | ws[col + str(current_row_number)] = _('Subtotal') |
||
681 | ws[col + str(current_row_number)].border = f_border |
||
682 | |||
683 | for i in range(0, reporting_period_data_ca_len): |
||
684 | current_col_number += 1 |
||
685 | col = format_cell.get_column_letter(current_col_number) |
||
686 | ws[col + str(current_row_number)].font = title_font |
||
687 | ws[col + str(current_row_number)].alignment = c_c_alignment |
||
688 | ws[col + str(current_row_number)] = round2(reporting_period_data['subtotals_saving'][i], 2) |
||
689 | ws[col + str(current_row_number)].border = f_border |
||
690 | |||
691 | for i in range(0, reporting_period_data_ca_len): |
||
692 | # line |
||
693 | line = LineChart() |
||
694 | line.title = _('Base Period Saving') + ' / ' \ |
||
695 | + _('Reporting Period Saving') + ' - ' \ |
||
696 | + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")" |
||
697 | labels = Reference(ws, min_col=2 + base_period_data_ca_len + 1, |
||
698 | min_row=table_start_row_number + 1, |
||
699 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
700 | base_line_data = Reference(ws, min_col=3 + i, min_row=table_start_row_number, |
||
701 | max_row=table_start_row_number + len(reporting_period_timestamps[0])) |
||
702 | reporting_line_data = Reference(ws, min_col=3 + base_period_data_ca_len + 1 + i, |
||
703 | min_row=table_start_row_number, |
||
704 | max_row=table_start_row_number |
||
705 | + len(reporting_period_timestamps[0])) |
||
706 | line.add_data(base_line_data, titles_from_data=True) |
||
707 | line.add_data(reporting_line_data, titles_from_data=True) |
||
708 | line.set_categories(labels) |
||
709 | for j in range(len(line.series)): |
||
710 | line.series[j].marker.symbol = "auto" |
||
711 | line.series[j].smooth = True |
||
712 | line.x_axis.crosses = 'min' |
||
713 | line.height = 8.25 |
||
714 | line.width = 24 |
||
715 | chart_col = 'B' |
||
716 | chart_cell = chart_col + str(table_start_draw_flag + 6 * i) |
||
717 | ws.add_chart(line, chart_cell) |
||
718 | |||
719 | current_row_number += 2 |
||
720 | |||
721 | #################################################################################################################### |
||
722 | current_sheet_parameters_row_number = table_start_draw_flag + len(reporting_period_data['names']) * 6 + 1 |
||
723 | if 'parameters' not in report.keys() or \ |
||
724 | report['parameters'] is None or \ |
||
725 | 'names' not in report['parameters'].keys() or \ |
||
726 | report['parameters']['names'] is None or \ |
||
727 | len(report['parameters']['names']) == 0 or \ |
||
728 | 'timestamps' not in report['parameters'].keys() or \ |
||
729 | report['parameters']['timestamps'] is None or \ |
||
730 | len(report['parameters']['timestamps']) == 0 or \ |
||
731 | 'values' not in report['parameters'].keys() or \ |
||
732 | report['parameters']['values'] is None or \ |
||
733 | len(report['parameters']['values']) == 0 or \ |
||
734 | timestamps_data_all_equal_0(report['parameters']['timestamps']): |
||
735 | pass |
||
736 | else: |
||
737 | ################################################################################################################ |
||
738 | # new worksheet |
||
739 | ################################################################################################################ |
||
740 | |||
741 | parameters_data = report['parameters'] |
||
742 | parameters_names_len = len(parameters_data['names']) |
||
743 | |||
744 | file_name = (re.sub(r'[^A-Z]', '', ws.title))+'aving_' |
||
745 | parameters_ws = wb.create_sheet(file_name + _('Parameters')) |
||
746 | |||
747 | parameters_timestamps_data_max_len = \ |
||
748 | get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps'])) |
||
749 | |||
750 | # Row height |
||
751 | parameters_ws.row_dimensions[1].height = 102 |
||
752 | for i in range(2, 7 + 1): |
||
753 | parameters_ws.row_dimensions[i].height = 42 |
||
754 | |||
755 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
756 | parameters_ws.row_dimensions[i].height = 60 |
||
757 | |||
758 | # Col width |
||
759 | parameters_ws.column_dimensions['A'].width = 1.5 |
||
760 | |||
761 | parameters_ws.column_dimensions['B'].width = 25.0 |
||
762 | |||
763 | for i in range(3, 12 + parameters_names_len * 3): |
||
764 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0 |
||
765 | |||
766 | # Img |
||
767 | img = Image("excelexporters/myems.png") |
||
768 | parameters_ws.add_image(img, 'A1') |
||
769 | |||
770 | # Title |
||
771 | |||
772 | parameters_ws['B3'].alignment = b_r_alignment |
||
773 | parameters_ws['B3'] = _('Name') + ':' |
||
774 | parameters_ws['C3'].border = b_border |
||
775 | parameters_ws['C3'].alignment = b_c_alignment |
||
776 | parameters_ws['C3'] = name |
||
777 | |||
778 | parameters_ws['D3'].alignment = b_r_alignment |
||
779 | parameters_ws['D3'] = _('Period Type') + ':' |
||
780 | parameters_ws['E3'].border = b_border |
||
781 | parameters_ws['E3'].alignment = b_c_alignment |
||
782 | parameters_ws['E3'] = period_type |
||
783 | |||
784 | parameters_ws['B4'].alignment = b_r_alignment |
||
785 | parameters_ws['B4'] = _('Reporting Start Datetime') + ':' |
||
786 | parameters_ws['C4'].border = b_border |
||
787 | parameters_ws['C4'].alignment = b_c_alignment |
||
788 | parameters_ws['C4'] = reporting_start_datetime_local |
||
789 | |||
790 | parameters_ws['D4'].alignment = b_r_alignment |
||
791 | parameters_ws['D4'] = _('Reporting End Datetime') + ':' |
||
792 | parameters_ws['E4'].border = b_border |
||
793 | parameters_ws['E4'].alignment = b_c_alignment |
||
794 | parameters_ws['E4'] = reporting_end_datetime_local |
||
795 | |||
796 | parameters_ws_current_row_number = 6 |
||
797 | |||
798 | parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font |
||
799 | parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + _('Parameters') |
||
800 | |||
801 | parameters_ws_current_row_number += 1 |
||
802 | |||
803 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
804 | |||
805 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
806 | |||
807 | parameters_ws_current_row_number += 1 |
||
808 | |||
809 | table_current_col_number = 2 |
||
810 | |||
811 | for i in range(0, parameters_names_len): |
||
812 | |||
813 | if len(parameters_data['timestamps'][i]) == 0: |
||
814 | continue |
||
815 | |||
816 | col = format_cell.get_column_letter(table_current_col_number) |
||
817 | |||
818 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
819 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
820 | |||
821 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
822 | |||
823 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = table_fill |
||
824 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = f_border |
||
825 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = name_font |
||
826 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = c_c_alignment |
||
827 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = parameters_data['names'][i] |
||
828 | |||
829 | table_current_row_number = parameters_ws_current_row_number |
||
830 | |||
831 | for j, value in enumerate(list(parameters_data['timestamps'][i])): |
||
832 | col = format_cell.get_column_letter(table_current_col_number) |
||
833 | |||
834 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
835 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
836 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
837 | parameters_ws[col + str(table_current_row_number)] = value |
||
838 | |||
839 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
840 | |||
841 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
842 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
843 | parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment |
||
844 | parameters_ws[col + str(table_current_row_number)] = round2(parameters_data['values'][i][j], 2) |
||
845 | |||
846 | table_current_row_number += 1 |
||
847 | |||
848 | table_current_col_number = table_current_col_number + 3 |
||
849 | |||
850 | ################################################################################################################ |
||
851 | # parameters chart and parameters table |
||
852 | ################################################################################################################ |
||
853 | |||
854 | ws['B' + str(current_sheet_parameters_row_number)].font = title_font |
||
855 | ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + _('Parameters') |
||
856 | |||
857 | current_sheet_parameters_row_number += 1 |
||
858 | |||
859 | chart_start_row_number = current_sheet_parameters_row_number |
||
860 | |||
861 | col_index = 0 |
||
862 | |||
863 | for i in range(0, parameters_names_len): |
||
864 | |||
865 | if len(parameters_data['timestamps'][i]) == 0: |
||
866 | continue |
||
867 | |||
868 | line = LineChart() |
||
869 | data_col = 3 + col_index * 3 |
||
870 | labels_col = 2 + col_index * 3 |
||
871 | col_index += 1 |
||
872 | line.title = _('Parameters') + ' - ' + \ |
||
873 | parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value |
||
874 | labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1, |
||
875 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
876 | line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number, |
||
877 | max_row=(len(parameters_data['timestamps'][i]) + parameters_table_start_row_number)) |
||
878 | line.add_data(line_data, titles_from_data=True) |
||
879 | line.set_categories(labels) |
||
880 | line_data = line.series[0] |
||
881 | line_data.marker.symbol = "auto" |
||
882 | line_data.smooth = True |
||
883 | line.x_axis.crosses = 'min' |
||
884 | line.height = 8.25 |
||
885 | line.width = 24 |
||
886 | chart_col = 'B' |
||
887 | chart_cell = chart_col + str(chart_start_row_number) |
||
888 | chart_start_row_number += 6 |
||
889 | ws.add_chart(line, chart_cell) |
||
890 | |||
891 | current_sheet_parameters_row_number = chart_start_row_number |
||
892 | |||
893 | current_sheet_parameters_row_number += 1 |
||
894 | #################################################################################################################### |
||
895 | filename = str(uuid.uuid4()) + '.xlsx' |
||
896 | wb.save(filename) |
||
897 | |||
898 | return filename |
||
899 | |||
900 | |||
901 | def sum_list(lists): |
||
902 | total = Decimal(0.0) |
||
903 | |||
904 | for i in range(0, len(lists)): |
||
905 | total += lists[i] |
||
906 | |||
907 | return total |
||
908 | |||
909 | |||
910 | def timestamps_data_all_equal_0(lists): |
||
911 | for i, value in enumerate(list(lists)): |
||
912 | if len(value) > 0: |
||
913 | return False |
||
914 | |||
915 | return True |
||
916 | |||
917 | |||
918 | def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
||
919 | max_len = 0 |
||
920 | for i, value in enumerate(list(parameters_timestamps_lists)): |
||
921 | if len(value) > max_len: |
||
922 | max_len = len(value) |
||
923 | |||
924 | return max_len |
||
925 | |||
926 | |||
927 | def timestamps_data_not_equal_0(lists): |
||
928 | number = 0 |
||
929 | for i, value in enumerate(list(lists)): |
||
930 | if len(value) > 0: |
||
931 | number += 1 |
||
932 | return number |
||
933 | |||
934 | |||
935 | View Code Duplication | def is_base_period_timestamp_exists(base_period_data): |
|
936 | timestamps = base_period_data['timestamps'] |
||
937 | |||
938 | if len(timestamps) == 0: |
||
939 | return False |
||
940 | |||
941 | for timestamp in timestamps: |
||
942 | if len(timestamp) > 0: |
||
943 | return True |
||
944 | |||
945 | return False |
||
946 |