• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

akvo / rtmis / #1406

30 Apr 2024 01:04PM UTC coverage: 80.729% (-0.2%) from 80.903%
#1406

Pull #1431

coveralls-python

jkisioh
Cahnged order of sections
Pull Request #1431: Feature/1413 urban sanitation form feedback

2688 of 3492 branches covered (76.98%)

Branch coverage included in aggregate %.

6298 of 7639 relevant lines covered (82.45%)

0.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.85
backend/utils/upload_administration.py
1
import os
1✔
2
from typing import List
1✔
3
from django.db.models import QuerySet
1✔
4
from django.utils import timezone
1✔
5
from django.db.models import Q
1✔
6

7
import pandas as pd
1✔
8
from api.v1.v1_profile.models import (
1✔
9
    Levels,
10
    AdministrationAttribute,
11
    Administration,
12
)
13

14
from api.v1.v1_users.models import SystemUser
1✔
15
from utils.storage import upload
1✔
16

17

18
def generate_template(
1✔
19
    filepath,
20
    attributes: List[int] = [],
21
):
22
    level_headers = [
1✔
23
        f"{lvl.id}|{lvl.name}"
24
        for lvl in Levels.objects.order_by("level").all()
25
    ]
26
    attribute_headers = generate_attribute_headers(
1✔
27
        AdministrationAttribute.objects.filter(id__in=attributes).order_by(
28
            "id"
29
        )
30
    )
31
    columns = level_headers + attribute_headers
1✔
32
    data = pd.DataFrame(columns=columns, index=[0])
1✔
33
    writer = pd.ExcelWriter(filepath, engine="xlsxwriter")
1✔
34
    data.to_excel(
1✔
35
        writer, sheet_name="data", startrow=1, header=False, index=False
36
    )
37
    workbook = writer.book
1✔
38
    worksheet = writer.sheets["data"]
1✔
39
    header_format = workbook.add_format(
1✔
40
        {"bold": True, "text_wrap": True, "valign": "top", "border": 1}
41
    )
42
    for col_num, value in enumerate(data.columns.values):
1✔
43
        worksheet.write(0, col_num, value, header_format)
1✔
44
    writer.save()
1✔
45

46

47
def generate_administration_excel(
1✔
48
    user: SystemUser,
49
    attributes: List[int] = [],
50
):
51
    directory = "tmp/administrations-template"
1✔
52
    os.makedirs(directory, exist_ok=True)
1✔
53
    filename = (
1✔
54
        f"{timezone.now().strftime('%Y%m%d%H%M%S')}-{user.pk}-"
55
        "administrations-template.xlsx"
56
    )
57
    filepath = f"./{directory}/{filename}"
1✔
58
    if os.path.exists(filepath):
1!
59
        os.remove(filepath)
×
60
    generate_template(filepath=filepath, attributes=attributes)
1✔
61
    return filepath
1✔
62

63

64
def generate_administration_template(
1✔
65
    file_path: str,
66
    attributes: List[int] = [],
67
    level: int = None,
68
    adm_id: int = None,
69
):
70
    file_path = "./tmp/{0}".format(file_path.replace("/", "_"))
1✔
71
    if os.path.exists(file_path):
1!
72
        os.remove(file_path)
×
73
    level_headers = [
1✔
74
        f"{lvl.id}|{lvl.name}"
75
        for lvl in Levels.objects.order_by("level").all()
76
    ]
77
    attribute_headers = generate_attribute_headers(
1✔
78
        AdministrationAttribute.objects.filter(id__in=attributes).order_by(
79
            "id"
80
        )
81
    )
82
    columns = level_headers + attribute_headers
1✔
83
    data = pd.DataFrame(columns=columns, index=[0])
1✔
84
    writer = pd.ExcelWriter(file_path, engine="xlsxwriter")
1✔
85
    data.to_excel(
1✔
86
        writer, sheet_name="data", startrow=1, header=False, index=False
87
    )
88
    workbook = writer.book
1✔
89
    worksheet = writer.sheets["data"]
1✔
90
    header_format = workbook.add_format(
1✔
91
        {"bold": True, "text_wrap": True, "valign": "top", "border": 1}
92
    )
93
    for col_num, value in enumerate(data.columns.values):
1✔
94
        worksheet.write(0, col_num, value, header_format)
1✔
95
    # get administrations with path
96
    filter_administration = Administration.objects.get(pk=adm_id)
1✔
97
    filter_path = f"{filter_administration.id}."
1✔
98
    if filter_administration.path:
1!
99
        filter_path = "{0}{1}.".format(
×
100
            filter_administration.path, filter_administration.id
101
        )
102
    administrations = Administration.objects.filter(
1✔
103
        Q(path__startswith=filter_path) | Q(pk=adm_id)
104
    ).all()
105
    # EOL get administrations with path
106
    aggregate_type = AdministrationAttribute.Type.AGGREGATE
1✔
107
    multiple_type = AdministrationAttribute.Type.MULTIPLE_OPTION
1✔
108
    for adx, adm in enumerate(administrations):
1✔
109
        for cx, col in enumerate(columns):
1✔
110
            if cx < len(level_headers) and f"{adm.level_id}" in col:
1✔
111
                worksheet.write(adx + 1, cx, adm.name)
1✔
112
            if cx >= len(level_headers):
1!
113
                attr_props = col.split("|")
×
114
                attr_id = attr_props[0]
×
115
                find_attr = adm.attributes.filter(
×
116
                    attribute__id=int(attr_id)
117
                ).first()
118
                if find_attr:
×
119
                    attr_value = find_attr.value
×
120
                    if find_attr.attribute.type == aggregate_type:
×
121
                        worksheet.write(
×
122
                            adx + 1, cx, attr_value.get("value")[attr_props[2]]
123
                        )
124
                    if find_attr.attribute.type == multiple_type:
×
125
                        worksheet.write(
×
126
                            adx + 1, cx, "|".join(attr_value.get("value"))
127
                        )
128
                    if find_attr.attribute.type in [
×
129
                        AdministrationAttribute.Type.VALUE,
130
                        AdministrationAttribute.Type.OPTION,
131
                    ]:
132
                        worksheet.write(adx + 1, cx, attr_value.get("value"))
×
133
        if adm.path:
1✔
134
            parent_ids = list(filter(lambda path: path, adm.path.split(".")))
1✔
135
            parents = Administration.objects.filter(pk__in=parent_ids).all()
1✔
136
            for parent_col, p in enumerate(parent_ids):
1✔
137
                [find_adm] = list(
1✔
138
                    filter(lambda path: path.id == int(p), parents)
139
                )
140
                if find_adm:
1!
141
                    worksheet.write(adx + 1, parent_col, find_adm.name)
1✔
142
    writer.save()
1✔
143
    url = upload(file=file_path, folder="download_administration")
1✔
144
    return url
1✔
145

146

147
def generate_attribute_headers(
1✔
148
    attributes: QuerySet[AdministrationAttribute],
149
) -> List[str]:
150
    headers = []
1✔
151
    for attribute in attributes:
1✔
152
        if attribute.type == AdministrationAttribute.Type.AGGREGATE:
1✔
153
            headers = headers + generate_aggregate_attribute_headers(attribute)
1✔
154
        else:
155
            headers.append(f"{attribute.id}|{attribute.name}")
1✔
156
    return headers
1✔
157

158

159
def generate_aggregate_attribute_headers(
1✔
160
    attribute: AdministrationAttribute,
161
) -> List[str]:
162
    return [
1✔
163
        f"{attribute.id}|{attribute.name}|{opt}" for opt in attribute.options
164
    ]
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc