1
|
|
|
import base64 |
2
|
|
|
import uuid |
3
|
|
|
import os |
4
|
|
|
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
5
|
|
|
from openpyxl.drawing.image import Image |
6
|
|
|
from openpyxl import Workbook |
7
|
|
|
import gettext |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
######################################################################################################################## |
11
|
|
|
# PROCEDURES |
12
|
|
|
# Step 1: Validate the report data |
13
|
|
|
# Step 2: Generate excel file from the report data |
14
|
|
|
# Step 3: Encode the excel file to Base64 |
15
|
|
|
######################################################################################################################## |
16
|
|
|
|
17
|
|
View Code Duplication |
def export(result, space_name, reporting_start_datetime_local, reporting_end_datetime_local, language): |
|
|
|
|
18
|
|
|
#################################################################################################################### |
19
|
|
|
# Step 1: Validate the report data |
20
|
|
|
#################################################################################################################### |
21
|
|
|
if result is None: |
22
|
|
|
return None |
23
|
|
|
|
24
|
|
|
#################################################################################################################### |
25
|
|
|
# Step 2: Generate excel file from the report data |
26
|
|
|
#################################################################################################################### |
27
|
|
|
filename = generate_excel(result, |
28
|
|
|
space_name, |
29
|
|
|
reporting_start_datetime_local, |
30
|
|
|
reporting_end_datetime_local, |
31
|
|
|
language) |
32
|
|
|
#################################################################################################################### |
33
|
|
|
# Step 3: Encode the excel file to Base64 |
34
|
|
|
#################################################################################################################### |
35
|
|
|
binary_file_data = b'' |
36
|
|
|
try: |
37
|
|
|
with open(filename, 'rb') as binary_file: |
38
|
|
|
binary_file_data = binary_file.read() |
39
|
|
|
except IOError as ex: |
40
|
|
|
pass |
41
|
|
|
|
42
|
|
|
# Base64 encode the bytes |
43
|
|
|
base64_encoded_data = base64.b64encode(binary_file_data) |
44
|
|
|
# get the Base64 encoded data using human-readable characters. |
45
|
|
|
base64_message = base64_encoded_data.decode('utf-8') |
46
|
|
|
# delete the file from server |
47
|
|
|
try: |
48
|
|
|
os.remove(filename) |
49
|
|
|
except NotImplementedError as ex: |
50
|
|
|
pass |
51
|
|
|
return base64_message |
52
|
|
|
|
53
|
|
|
|
54
|
|
View Code Duplication |
def generate_excel(report, space_name, reporting_start_datetime_local, reporting_end_datetime_local, language): |
|
|
|
|
55
|
|
|
|
56
|
|
|
locale_path = './i18n/' |
57
|
|
|
if language == 'zh_CN': |
58
|
|
|
trans = gettext.translation('myems', locale_path, languages=['zh_CN']) |
59
|
|
|
elif language == 'de': |
60
|
|
|
trans = gettext.translation('myems', locale_path, languages=['de']) |
61
|
|
|
elif language == 'en': |
62
|
|
|
trans = gettext.translation('myems', locale_path, languages=['en']) |
63
|
|
|
else: |
64
|
|
|
trans = gettext.translation('myems', locale_path, languages=['en']) |
65
|
|
|
trans.install() |
66
|
|
|
_ = trans.gettext |
67
|
|
|
|
68
|
|
|
wb = Workbook() |
69
|
|
|
ws = wb.active |
70
|
|
|
ws.title = "OfflineMeterBatch" |
71
|
|
|
|
72
|
|
|
# Col width |
73
|
|
|
for i in range(ord('A'), ord('L')): |
74
|
|
|
ws.column_dimensions[chr(i)].width = 20.0 |
75
|
|
|
|
76
|
|
|
# Head image |
77
|
|
|
ws.row_dimensions[1].height = 105 |
78
|
|
|
img = Image("excelexporters/myems.png") |
79
|
|
|
ws.add_image(img, 'A1') |
80
|
|
|
|
81
|
|
|
# Query Parameters |
82
|
|
|
b_r_alignment = Alignment(vertical='bottom', |
83
|
|
|
horizontal='right', |
84
|
|
|
text_rotation=0, |
85
|
|
|
wrap_text=True, |
86
|
|
|
shrink_to_fit=False, |
87
|
|
|
indent=0) |
88
|
|
|
ws['A3'].alignment = b_r_alignment |
89
|
|
|
ws['A3'] = _('Space') + ':' |
90
|
|
|
ws['B3'] = space_name |
91
|
|
|
ws['A4'].alignment = b_r_alignment |
92
|
|
|
ws['A4'] = _('Start Datetime') + ':' |
93
|
|
|
ws['B4'] = reporting_start_datetime_local |
94
|
|
|
ws['A5'].alignment = b_r_alignment |
95
|
|
|
ws['A5'] = _('End Datetime') + ':' |
96
|
|
|
ws['B5'] = reporting_end_datetime_local |
97
|
|
|
|
98
|
|
|
# Title |
99
|
|
|
title_font = Font(size=12, bold=True) |
100
|
|
|
ws['A6'].font = title_font |
101
|
|
|
ws['A6'] = _('ID') |
102
|
|
|
ws['B6'].font = title_font |
103
|
|
|
ws['B6'] = _('Name') |
104
|
|
|
ws['C6'].font = title_font |
105
|
|
|
ws['C6'] = _('Space') |
106
|
|
|
|
107
|
|
|
ca_len = len(report['energycategories']) |
108
|
|
|
for i in range(0, ca_len): |
109
|
|
|
col = chr(ord('D') + i) |
110
|
|
|
ws[col + '6'].font = title_font |
111
|
|
|
ws[col + '6'] = report['energycategories'][i]['name'] + " (" + \ |
112
|
|
|
report['energycategories'][i]['unit_of_measure'] + ")" |
113
|
|
|
|
114
|
|
|
current_row_number = 7 |
115
|
|
|
for i in range(0, len(report['offline_meters'])): |
116
|
|
|
ws['A' + str(current_row_number)] = str(report['offline_meters'][i]['id']) |
|
|
|
|
117
|
|
|
ws['B' + str(current_row_number)] = report['offline_meters'][i]['offline_meter_name'] |
118
|
|
|
ws['C' + str(current_row_number)] = report['offline_meters'][i]['space_name'] |
119
|
|
|
ca_len = len(report['offline_meters'][i]['values']) |
120
|
|
|
for j in range(0, ca_len): |
121
|
|
|
col = chr(ord('D') + j) |
122
|
|
|
ws[col + str(current_row_number)] = report['offline_meters'][i]['values'][j] |
123
|
|
|
|
124
|
|
|
current_row_number += 1 |
125
|
|
|
|
126
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
127
|
|
|
wb.save(filename) |
128
|
|
|
|
129
|
|
|
return filename |
130
|
|
|
|