1
|
|
|
import base64 |
2
|
|
|
import uuid |
3
|
|
|
import os |
4
|
|
|
from decimal import Decimal |
5
|
|
|
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
6
|
|
|
from openpyxl.drawing.image import Image |
7
|
|
|
from openpyxl import Workbook |
8
|
|
|
import gettext |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
######################################################################################################################## |
12
|
|
|
# PROCEDURES |
13
|
|
|
# Step 1: Validate the report data |
14
|
|
|
# Step 2: Generate excelexporters file |
15
|
|
|
# Step 3: Encode the excelexporters file to Base64 |
16
|
|
|
######################################################################################################################## |
17
|
|
|
|
18
|
|
View Code Duplication |
def export(result, space_name, reporting_start_datetime_local, reporting_end_datetime_local, language): |
|
|
|
|
19
|
|
|
#################################################################################################################### |
20
|
|
|
# Step 1: Validate the report data |
21
|
|
|
#################################################################################################################### |
22
|
|
|
if result is None: |
23
|
|
|
return None |
24
|
|
|
|
25
|
|
|
#################################################################################################################### |
26
|
|
|
# Step 2: Generate excel file from the report data |
27
|
|
|
#################################################################################################################### |
28
|
|
|
filename = generate_excel(result, |
29
|
|
|
space_name, |
30
|
|
|
reporting_start_datetime_local, |
31
|
|
|
reporting_end_datetime_local, |
32
|
|
|
language) |
33
|
|
|
#################################################################################################################### |
34
|
|
|
# Step 3: Encode the excel file to Base64 |
35
|
|
|
#################################################################################################################### |
36
|
|
|
binary_file_data = b'' |
37
|
|
|
try: |
38
|
|
|
with open(filename, 'rb') as binary_file: |
39
|
|
|
binary_file_data = binary_file.read() |
40
|
|
|
except IOError as ex: |
41
|
|
|
pass |
42
|
|
|
|
43
|
|
|
# Base64 encode the bytes |
44
|
|
|
base64_encoded_data = base64.b64encode(binary_file_data) |
45
|
|
|
# get the Base64 encoded data using human-readable characters. |
46
|
|
|
base64_message = base64_encoded_data.decode('utf-8') |
47
|
|
|
# delete the file from server |
48
|
|
|
try: |
49
|
|
|
os.remove(filename) |
50
|
|
|
except NotImplementedError as ex: |
51
|
|
|
pass |
52
|
|
|
return base64_message |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def generate_excel(report, space_name, reporting_start_datetime_local, reporting_end_datetime_local, language): |
56
|
|
|
|
57
|
|
|
locale_path = './i18n/' |
58
|
|
|
if language == 'zh_CN': |
59
|
|
|
trans = gettext.translation('myems', locale_path, languages=['zh_CN']) |
60
|
|
|
elif language == 'de': |
61
|
|
|
trans = gettext.translation('myems', locale_path, languages=['de']) |
62
|
|
|
elif language == 'en': |
63
|
|
|
trans = gettext.translation('myems', locale_path, languages=['en']) |
64
|
|
|
else: |
65
|
|
|
trans = gettext.translation('myems', locale_path, languages=['en']) |
66
|
|
|
trans.install() |
67
|
|
|
_ = trans.gettext |
68
|
|
|
|
69
|
|
|
wb = Workbook() |
70
|
|
|
ws = wb.active |
71
|
|
|
ws.title = "MeterTracking" |
72
|
|
|
|
73
|
|
|
# Column width |
74
|
|
|
for i in range(ord('A'), ord('I')): |
75
|
|
|
ws.column_dimensions[chr(i)].width = 25.0 |
76
|
|
|
|
77
|
|
|
# Head image |
78
|
|
|
ws.row_dimensions[1].height = 105 |
79
|
|
|
img = Image("excelexporters/myems.png") |
80
|
|
|
ws.add_image(img, 'A1') |
81
|
|
|
|
82
|
|
|
# Query Parameters |
83
|
|
|
b_r_alignment = Alignment(vertical='bottom', |
84
|
|
|
horizontal='right', |
85
|
|
|
text_rotation=0, |
86
|
|
|
wrap_text=True, |
87
|
|
|
shrink_to_fit=False, |
88
|
|
|
indent=0) |
89
|
|
|
ws['A3'].alignment = b_r_alignment |
90
|
|
|
ws['A3'] = _('Space') + ':' |
91
|
|
|
ws['B3'] = space_name |
92
|
|
|
ws['A4'].alignment = b_r_alignment |
93
|
|
|
ws['A4'] = _('Start Datetime') + ':' |
94
|
|
|
ws['B4'] = reporting_start_datetime_local |
95
|
|
|
ws['A5'].alignment = b_r_alignment |
96
|
|
|
ws['A5'] = _('End Datetime') + ':' |
97
|
|
|
ws['B5'] = reporting_end_datetime_local |
98
|
|
|
ws['A6'].alignment = b_r_alignment |
99
|
|
|
ws['A6'] = _('Start Integrity Rate') + ':' |
100
|
|
|
ws['B6'] = (str(report['start_integrity_rate'] * Decimal(100.0)) + '%') \ |
|
|
|
|
101
|
|
|
if report['start_integrity_rate'] is not None else None |
102
|
|
|
ws['A7'].alignment = b_r_alignment |
103
|
|
|
ws['A7'] = _('End Integrity Rate') + ':' |
104
|
|
|
ws['B7'] = (str(report['end_integrity_rate'] * Decimal(100.0)) + '%') \ |
105
|
|
|
if report['end_integrity_rate'] is not None else None |
106
|
|
|
ws['A8'].alignment = b_r_alignment |
107
|
|
|
ws['A8'] = _('Full Integrity Rate') + ':' |
108
|
|
|
ws['B8'] = (str(report['full_integrity_rate'] * Decimal(100.0)) + '%') \ |
109
|
|
|
if report['full_integrity_rate'] is not None else None |
110
|
|
|
|
111
|
|
|
# Title |
112
|
|
|
title_font = Font(size=12, bold=True) |
113
|
|
|
ws['A9'].font = title_font |
114
|
|
|
ws['A9'] = 'ID' |
115
|
|
|
ws['B9'].font = title_font |
116
|
|
|
ws['B9'] = 'Name' |
117
|
|
|
ws['C9'].font = title_font |
118
|
|
|
ws['C9'] = 'Space' |
119
|
|
|
ws['D9'].font = title_font |
120
|
|
|
ws['D9'] = 'Cost Center' |
121
|
|
|
ws['E9'].font = title_font |
122
|
|
|
ws['E9'] = 'Energy Category' |
123
|
|
|
ws['F9'].font = title_font |
124
|
|
|
ws['F9'] = 'Description' |
125
|
|
|
ws['G9'].font = title_font |
126
|
|
|
ws['G9'] = 'Start Value' |
127
|
|
|
ws['H9'].font = title_font |
128
|
|
|
ws['H9'] = 'End Value' |
129
|
|
|
|
130
|
|
|
current_row_number = 10 |
131
|
|
|
for i in range(0, len(report['meters'])): |
132
|
|
|
ws['A' + str(current_row_number)] = report['meters'][i]['id'] |
133
|
|
|
ws['B' + str(current_row_number)] = report['meters'][i]['meter_name'] |
134
|
|
|
ws['C' + str(current_row_number)] = report['meters'][i]['space_name'] |
135
|
|
|
ws['D' + str(current_row_number)] = report['meters'][i]['cost_center_name'] |
136
|
|
|
ws['E' + str(current_row_number)] = report['meters'][i]['energy_category_name'] |
137
|
|
|
ws['F' + str(current_row_number)] = report['meters'][i]['description'] |
138
|
|
|
ws['G' + str(current_row_number)] = report['meters'][i]['start_value'] |
139
|
|
|
ws['H' + str(current_row_number)] = report['meters'][i]['end_value'] |
140
|
|
|
current_row_number += 1 |
141
|
|
|
|
142
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
143
|
|
|
wb.save(filename) |
144
|
|
|
|
145
|
|
|
return filename |
146
|
|
|
|