Issues (1577)

myems-api/excelexporters/offlinemeterbatch.py (2 issues)

1
import base64
2
from core.utilities import get_translation
3
import os
4
import uuid
5
from openpyxl import Workbook
6
from openpyxl.drawing.image import Image
7
from openpyxl.styles import Alignment, Font
8
9
10
########################################################################################################################
11
# PROCEDURES
12
# Step 1: Validate the report data
13
# Step 2: Generate excel file from the report data
14
# Step 3: Encode the excel file to Base64
15
########################################################################################################################
16
17
def export(result, space_name, reporting_start_datetime_local, reporting_end_datetime_local, language):
18
    ####################################################################################################################
19
    # Step 1: Validate the report data
20
    ####################################################################################################################
21
    if result is None:
22
        return None
23
24
    ####################################################################################################################
25
    # Step 2: Generate excel file from the report data
26
    ####################################################################################################################
27
    filename = generate_excel(result,
28
                              space_name,
29
                              reporting_start_datetime_local,
30
                              reporting_end_datetime_local,
31
                              language)
32
    ####################################################################################################################
33
    # Step 3: Encode the excel file to Base64
34
    ####################################################################################################################
35
    binary_file_data = b''
36
    try:
37
        with open(filename, 'rb') as binary_file:
38
            binary_file_data = binary_file.read()
39
    except IOError as ex:
40
        print(str(ex))
41
42
    # Base64 encode the bytes
43
    base64_encoded_data = base64.b64encode(binary_file_data)
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
        print(str(ex))
51
    return base64_message
52
53
54 View Code Duplication
def generate_excel(report, space_name, reporting_start_datetime_local, reporting_end_datetime_local, language):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
55
56
    trans = get_translation(language)
57
    trans.install()
58
    _ = trans.gettext
59
60
    wb = Workbook()
61
    ws = wb.active
62
    ws.title = "OfflineMeterBatch"
63
64
    # Col width
65
    for i in range(ord('A'), ord('L')):
66
        ws.column_dimensions[chr(i)].width = 20.0
67
68
    # Head image
69
    ws.row_dimensions[1].height = 105
70
    img = Image("excelexporters/myems.png")
71
    ws.add_image(img, 'A1')
72
73
    # Query Parameters
74
    b_r_alignment = Alignment(vertical='bottom',
75
                              horizontal='right',
76
                              text_rotation=0,
77
                              wrap_text=True,
78
                              shrink_to_fit=False,
79
                              indent=0)
80
    ws['A3'].alignment = b_r_alignment
81
    ws['A3'] = _('Space') + ':'
82
    ws['B3'] = space_name
83
    ws['A4'].alignment = b_r_alignment
84
    ws['A4'] = _('Start Datetime') + ':'
85
    ws['B4'] = reporting_start_datetime_local
86
    ws['A5'].alignment = b_r_alignment
87
    ws['A5'] = _('End Datetime') + ':'
88
    ws['B5'] = reporting_end_datetime_local
89
90
    # Title
91
    title_font = Font(size=12, bold=True)
92
    ws['A6'].font = title_font
93
    ws['A6'] = _('ID')
94
    ws['B6'].font = title_font
95
    ws['B6'] = _('Name')
96
    ws['C6'].font = title_font
97
    ws['C6'] = _('Space')
98
99
    ca_len = len(report['energycategories'])
100
    for i in range(0, ca_len):
101
        col = chr(ord('D') + i)
102
        ws[col + '6'].font = title_font
103
        ws[col + '6'] = report['energycategories'][i]['name'] + " (" + \
104
            report['energycategories'][i]['unit_of_measure'] + ")"
105
106
    current_row_number = 7
107
    for i in range(0, len(report['offline_meters'])):
108
        ws['A' + str(current_row_number)] = str(report['offline_meters'][i]['id'])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
109
        ws['B' + str(current_row_number)] = report['offline_meters'][i]['offline_meter_name']
110
        ws['C' + str(current_row_number)] = report['offline_meters'][i]['space_name']
111
        ca_len = len(report['offline_meters'][i]['values'])
112
        for j in range(0, ca_len):
113
            col = chr(ord('D') + j)
114
            ws[col + str(current_row_number)] = report['offline_meters'][i]['values'][j]
115
116
        current_row_number += 1
117
118
    filename = str(uuid.uuid4()) + '.xlsx'
119
    wb.save(filename)
120
121
    return filename
122