Passed
Push — master ( d7adf1...e85ae2 )
by Guangyu
01:58 queued 10s
created

excelexporters.metercost.export()   B

Complexity

Conditions 8

Size

Total Lines 37
Code Lines 23

Duplication

Lines 37
Ratio 100 %

Importance

Changes 0
Metric Value
cc 8
eloc 23
nop 5
dl 37
loc 37
rs 7.3333
c 0
b 0
f 0
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):
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 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)
0 ignored issues
show
introduced by
The variable binary_file_data does not seem to be defined for all execution paths.
Loading history...
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