Passed
Push — master ( 981d05...748a94 )
by Guangyu
18:29 queued 12s
created

excelexporters.metertracking.generate_excel()   C

Complexity

Conditions 5

Size

Total Lines 174
Code Lines 135

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 135
dl 0
loc 174
rs 6.5333
c 0
b 0
f 0
cc 5
nop 4

How to fix   Long Method   

Long Method

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:

1
import base64
2
import uuid
3
import os
4
from openpyxl.chart import (
5
    BarChart,
6
    Reference,
7
)
8
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
9
from openpyxl.drawing.image import Image
10
from openpyxl import Workbook
11
from openpyxl.chart.label import DataLabelList
12
13
14
####################################################################################################################
15
# PROCEDURES
16
# Step 1: Validate the report data
17
# Step 2: Generate excelexporters file
18
# Step 3: Encode the excelexporters file to Base64
19
####################################################################################################################
20
21 View Code Duplication
def export(result, space_name, reporting_start_datetime_local, reporting_end_datetime_local):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
    ####################################################################################################################
23
    # Step 1: Validate the report data
24
    ####################################################################################################################
25
    if result is None:
26
        return None
27
28
    ####################################################################################################################
29
    # Step 2: Generate excel file from the report data
30
    ####################################################################################################################
31
    filename = generate_excel(result,
32
                              space_name,
33
                              reporting_start_datetime_local,
34
                              reporting_end_datetime_local)
35
    ####################################################################################################################
36
    # Step 3: Encode the excel file to Base64
37
    ####################################################################################################################
38
    try:
39
        with open(filename, 'rb') as binary_file:
40
            binary_file_data = binary_file.read()
41
    except IOError as ex:
42
        pass
43
44
    # Base64 encode the bytes
45
    base64_encoded_data = base64.b64encode(binary_file_data)
0 ignored issues
show
introduced by
The variable binary_file_data does not seem to be defined for all execution paths.
Loading history...
46
    # get the Base64 encoded data using human-readable characters.
47
    base64_message = base64_encoded_data.decode('utf-8')
48
    # delete the file from server
49
    try:
50
        os.remove(filename)
51
    except NotImplementedError as ex:
52
        pass
53
    return base64_message
54
55
56
def generate_excel(report, space_name, reporting_start_datetime_local, reporting_end_datetime_local):
57
58
    wb = Workbook()
59
    ws = wb.active
60
61
    # Row height
62
    ws.row_dimensions[1].height = 102
63
    for i in range(2, 5 + 1):
64
        ws.row_dimensions[i].height = 42
65
66
    for i in range(6, len(report['meters']) + 15):
67
        ws.row_dimensions[i].height = 60
68
69
    # Col width
70
    ws.column_dimensions['A'].width = 1.5
71
72
    ws.column_dimensions['B'].width = 25.0
73
74
    for i in range(ord('C'), ord('L')):
75
        ws.column_dimensions[chr(i)].width = 15.0
76
77
    # Font
78
    name_font = Font(name='Constantia', size=15, bold=True)
79
    title_font = Font(name='宋体', size=15, bold=True)
80
    data_font = Font(name='Franklin Gothic Book', size=11)
81
82
    table_fill = PatternFill(fill_type='solid', fgColor='1F497D')
83
    f_border = Border(left=Side(border_style='medium', color='00000000'),
84
                      right=Side(border_style='medium', color='00000000'),
85
                      bottom=Side(border_style='medium', color='00000000'),
86
                      top=Side(border_style='medium', color='00000000')
87
                      )
88
    b_border = Border(
89
        bottom=Side(border_style='medium', color='00000000'),
90
    )
91
92
    b_c_alignment = Alignment(vertical='bottom',
93
                              horizontal='center',
94
                              text_rotation=0,
95
                              wrap_text=True,
96
                              shrink_to_fit=False,
97
                              indent=0)
98
    c_c_alignment = Alignment(vertical='center',
99
                              horizontal='center',
100
                              text_rotation=0,
101
                              wrap_text=True,
102
                              shrink_to_fit=False,
103
                              indent=0)
104
    b_r_alignment = Alignment(vertical='bottom',
105
                              horizontal='right',
106
                              text_rotation=0,
107
                              wrap_text=True,
108
                              shrink_to_fit=False,
109
                              indent=0)
110
    c_r_alignment = Alignment(vertical='bottom',
111
                              horizontal='center',
112
                              text_rotation=0,
113
                              wrap_text=True,
114
                              shrink_to_fit=False,
115
                              indent=0)
116
117
    # Img
118
    img = Image("excelexporters/myems.png")
119
    img.width = img.width * 0.85
120
    img.height = img.height * 0.85
121
    ws.add_image(img, 'B1')
122
123
    # Title
124
    ws.row_dimensions[3].height = 60
125
126
    ws['B3'].font = name_font
127
    ws['B3'].alignment = b_r_alignment
128
    ws['B3'] = 'Name:'
129
    ws['C3'].border = b_border
130
    ws['C3'].alignment = b_c_alignment
131
    ws['C3'].font = name_font
132
    ws['C3'] = space_name
133
134
    ws['F3'].font = name_font
135
    ws['F3'].alignment = b_r_alignment
136
    ws['F3'] = 'Date:'
137
    ws['G3'].border = b_border
138
    ws['G3'].alignment = b_c_alignment
139
    ws['G3'].font = name_font
140
    ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local
141
    ws.merge_cells("G3:H3")
142
143
    # Title
144
    ws['B6'].border = f_border
145
    ws['B6'].font = name_font
146
    ws['B6'].alignment = c_c_alignment
147
    ws['B6'].fill = table_fill
148
    ws['B6'] = '名称'
149
150
    ws['C6'].border = f_border
151
    ws['C6'].alignment = c_c_alignment
152
    ws['C6'].font = name_font
153
    ws['C6'].fill = table_fill
154
    ws['C6'] = '空间'
155
156
    ws['D6'].border = f_border
157
    ws['D6'].font = name_font
158
    ws['D6'].alignment = c_c_alignment
159
    ws['D6'].fill = table_fill
160
    ws['D6'] = '成本中心'
161
162
    ws['E6'].border = f_border
163
    ws['E6'].alignment = c_c_alignment
164
    ws['E6'].font = name_font
165
    ws['E6'].fill = table_fill
166
    ws['E6'] = '能耗分类'
167
168
    ws['F6'].border = f_border
169
    ws['F6'].font = name_font
170
    ws['F6'].alignment = c_c_alignment
171
    ws['F6'].fill = table_fill
172
    ws['F6'] = ' 描述'
173
174
    ws['G6'].border = f_border
175
    ws['G6'].font = name_font
176
    ws['G6'].alignment = c_c_alignment
177
    ws['G6'].fill = table_fill
178
    ws['G6'] = '开始值'
179
180
    ws['H6'].border = f_border
181
    ws['H6'].font = name_font
182
    ws['H6'].alignment = c_c_alignment
183
    ws['H6'].fill = table_fill
184
    ws['H6'] = ' 结束值'
185
186
    current_row_number = 7
187
    for i in range(0, len(report['meters'])):
188
189
        ws['B' + str(current_row_number)].font = title_font
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
190
        ws['B' + str(current_row_number)].border = f_border
191
        ws['B' + str(current_row_number)].alignment = c_c_alignment
192
        ws['B' + str(current_row_number)] = report['meters'][i]['meter_name']
193
194
        ws['C' + str(current_row_number)].font = title_font
195
        ws['C' + str(current_row_number)].border = f_border
196
        ws['C' + str(current_row_number)].alignment = c_c_alignment
197
        ws['C' + str(current_row_number)] = report['meters'][i]['space_name']
198
199
        ws['D' + str(current_row_number)].font = title_font
200
        ws['D' + str(current_row_number)].border = f_border
201
        ws['D' + str(current_row_number)].alignment = c_c_alignment
202
        ws['D' + str(current_row_number)] = report['meters'][i]['cost_center_name']
203
204
        ws['E' + str(current_row_number)].font = title_font
205
        ws['E' + str(current_row_number)].border = f_border
206
        ws['E' + str(current_row_number)].alignment = c_c_alignment
207
        ws['E' + str(current_row_number)] = report['meters'][i]['energy_category_name']
208
209
        ws['F' + str(current_row_number)].font = title_font
210
        ws['F' + str(current_row_number)].border = f_border
211
        ws['F' + str(current_row_number)].alignment = c_c_alignment
212
        ws['F' + str(current_row_number)] = report['meters'][i]['description']
213
214
        ws['G' + str(current_row_number)].font = title_font
215
        ws['G' + str(current_row_number)].border = f_border
216
        ws['G' + str(current_row_number)].alignment = c_c_alignment
217
        ws['G' + str(current_row_number)] = report['meters'][i]['start_value']
218
219
        ws['H' + str(current_row_number)].font = title_font
220
        ws['H' + str(current_row_number)].border = f_border
221
        ws['H' + str(current_row_number)].alignment = c_c_alignment
222
        ws['H' + str(current_row_number)] = report['meters'][i]['end_value']
223
224
        current_row_number += 1
225
226
    filename = str(uuid.uuid4()) + '.xlsx'
227
    wb.save(filename)
228
229
    return filename
230