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, 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(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
|
|
|
|
22
|
|
|
#################################################################################################################### |
23
|
|
|
# Step 1: Validate the report data |
24
|
|
|
#################################################################################################################### |
25
|
|
|
if report is None: |
26
|
|
|
return None |
27
|
|
|
|
28
|
|
|
if "reporting_period" not in report.keys() or \ |
29
|
|
|
"values" not in report['reporting_period'].keys() or len(report['reporting_period']['values']) == 0: |
30
|
|
|
return None |
31
|
|
|
#################################################################################################################### |
32
|
|
|
# Step 2: Generate excel file from the report data |
33
|
|
|
#################################################################################################################### |
34
|
|
|
filename = generate_excel(report, |
35
|
|
|
name, |
36
|
|
|
reporting_start_datetime_local, |
37
|
|
|
reporting_end_datetime_local, |
38
|
|
|
period_type) |
39
|
|
|
#################################################################################################################### |
40
|
|
|
# Step 3: Encode the excel file to Base64 |
41
|
|
|
#################################################################################################################### |
42
|
|
|
try: |
43
|
|
|
with open(filename, 'rb') as binary_file: |
44
|
|
|
binary_file_data = binary_file.read() |
45
|
|
|
except IOError as ex: |
46
|
|
|
pass |
47
|
|
|
|
48
|
|
|
# Base64 encode the bytes |
49
|
|
|
base64_encoded_data = base64.b64encode(binary_file_data) |
|
|
|
|
50
|
|
|
# get the Base64 encoded data using human-readable characters. |
51
|
|
|
base64_message = base64_encoded_data.decode('utf-8') |
52
|
|
|
# delete the file from server |
53
|
|
|
try: |
54
|
|
|
os.remove(filename) |
55
|
|
|
except NotImplementedError as ex: |
56
|
|
|
pass |
57
|
|
|
return base64_message |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def generate_excel(report, name, reporting_start_datetime_local, reporting_end_datetime_local, period_type): |
61
|
|
|
|
62
|
|
|
wb = Workbook() |
63
|
|
|
|
64
|
|
|
# todo |
65
|
|
|
|
66
|
|
|
filename = str(uuid.uuid4()) + '.xlsx' |
67
|
|
|
wb.save(filename) |
68
|
|
|
|
69
|
|
|
return filename |
70
|
|
|
|