Passed
Push — master ( 59b5cb...074b57 )
by Guangyu
16:24 queued 42s
created

excelexporters.metertracking   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 6

2 Functions

Rating   Name   Duplication   Size   Complexity  
A generate_excel() 0 9 1
A export() 0 31 5
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
def export(result, space_name):
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
    ####################################################################################################################
34
    # Step 3: Encode the excel file to Base64
35
    ####################################################################################################################
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)
0 ignored issues
show
introduced by
The variable binary_file_data does not seem to be defined for all execution paths.
Loading history...
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
def generate_excel(report, space_name):
55
56
    wb = Workbook()
57
    ws = wb.active
58
59
    filename = str(uuid.uuid4()) + '.xlsx'
60
    wb.save(filename)
61
62
    return filename
63