territories-dashboard-lib 0.1.0__py3-none-any.whl → 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of territories-dashboard-lib might be problematic. Click here for more details.
- territories_dashboard_lib/commons/__init__.py +0 -0
- territories_dashboard_lib/commons/decorators.py +36 -0
- territories_dashboard_lib/commons/models.py +9 -0
- territories_dashboard_lib/geo_lib/__init__.py +0 -0
- territories_dashboard_lib/geo_lib/admin.py +64 -0
- territories_dashboard_lib/geo_lib/enums.py +7 -0
- territories_dashboard_lib/geo_lib/migrations/0001_initial.py +51 -0
- territories_dashboard_lib/geo_lib/migrations/__init__.py +0 -0
- territories_dashboard_lib/geo_lib/models.py +58 -0
- territories_dashboard_lib/geo_lib/urls.py +27 -0
- territories_dashboard_lib/geo_lib/views.py +239 -0
- territories_dashboard_lib/indicators_lib/__init__.py +0 -0
- territories_dashboard_lib/indicators_lib/admin.py +140 -0
- territories_dashboard_lib/indicators_lib/enums.py +59 -0
- territories_dashboard_lib/indicators_lib/export.py +29 -0
- territories_dashboard_lib/indicators_lib/format.py +34 -0
- territories_dashboard_lib/indicators_lib/methodo_pdf.py +99 -0
- territories_dashboard_lib/indicators_lib/migrations/0001_initial.py +138 -0
- territories_dashboard_lib/indicators_lib/migrations/__init__.py +0 -0
- territories_dashboard_lib/indicators_lib/models.py +230 -0
- territories_dashboard_lib/indicators_lib/payloads.py +54 -0
- territories_dashboard_lib/indicators_lib/query/commons.py +223 -0
- territories_dashboard_lib/indicators_lib/query/comparison.py +70 -0
- territories_dashboard_lib/indicators_lib/query/details.py +64 -0
- territories_dashboard_lib/indicators_lib/query/histogram.py +82 -0
- territories_dashboard_lib/indicators_lib/query/indicator_card.py +102 -0
- territories_dashboard_lib/indicators_lib/query/top_10.py +100 -0
- territories_dashboard_lib/indicators_lib/query/utils.py +20 -0
- territories_dashboard_lib/indicators_lib/refresh_filters.py +17 -0
- territories_dashboard_lib/indicators_lib/table.py +154 -0
- territories_dashboard_lib/indicators_lib/urls.py +97 -0
- territories_dashboard_lib/indicators_lib/views.py +490 -0
- territories_dashboard_lib/superset_lib/__init__.py +0 -0
- territories_dashboard_lib/superset_lib/admin.py +22 -0
- territories_dashboard_lib/superset_lib/guest_token.py +64 -0
- territories_dashboard_lib/superset_lib/logic.py +67 -0
- territories_dashboard_lib/superset_lib/migrations/0001_initial.py +45 -0
- territories_dashboard_lib/superset_lib/migrations/__init__.py +0 -0
- territories_dashboard_lib/superset_lib/models.py +52 -0
- territories_dashboard_lib/superset_lib/serializers.py +10 -0
- territories_dashboard_lib/superset_lib/urls.py +10 -0
- territories_dashboard_lib/superset_lib/views.py +19 -0
- territories_dashboard_lib/tracking_lib/__init__.py +0 -0
- territories_dashboard_lib/tracking_lib/enums.py +7 -0
- territories_dashboard_lib/tracking_lib/logic.py +78 -0
- territories_dashboard_lib/tracking_lib/migrations/0001_initial.py +45 -0
- territories_dashboard_lib/tracking_lib/migrations/__init__.py +0 -0
- territories_dashboard_lib/tracking_lib/models.py +79 -0
- territories_dashboard_lib/website_lib/__init__.py +0 -0
- territories_dashboard_lib/website_lib/admin.py +40 -0
- territories_dashboard_lib/website_lib/context_processors.py +27 -0
- territories_dashboard_lib/website_lib/forms.py +47 -0
- territories_dashboard_lib/website_lib/migrations/0001_initial.py +91 -0
- territories_dashboard_lib/website_lib/migrations/__init__.py +0 -0
- territories_dashboard_lib/website_lib/models.py +148 -0
- territories_dashboard_lib/website_lib/navigation.py +124 -0
- territories_dashboard_lib/website_lib/params.py +268 -0
- territories_dashboard_lib/website_lib/serializers.py +105 -0
- territories_dashboard_lib/website_lib/static_content.py +20 -0
- territories_dashboard_lib/website_lib/templatetags/htmlparams.py +75 -0
- territories_dashboard_lib/website_lib/templatetags/other_filters.py +30 -0
- territories_dashboard_lib/website_lib/views.py +212 -0
- {territories_dashboard_lib-0.1.0.dist-info → territories_dashboard_lib-0.1.1.dist-info}/METADATA +1 -1
- territories_dashboard_lib-0.1.1.dist-info/RECORD +67 -0
- territories_dashboard_lib-0.1.0.dist-info/RECORD +0 -5
- {territories_dashboard_lib-0.1.0.dist-info → territories_dashboard_lib-0.1.1.dist-info}/WHEEL +0 -0
- {territories_dashboard_lib-0.1.0.dist-info → territories_dashboard_lib-0.1.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
from .payloads import IndicatorTablePayload
|
|
2
|
+
from .query.commons import add_optional_filters, get_table_data_for_geography
|
|
3
|
+
from .query.utils import run_custom_query
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def _get_query(
|
|
7
|
+
*, indicator, props: IndicatorTablePayload, columns, orders, filters, limit=None
|
|
8
|
+
):
|
|
9
|
+
columns = [c for c in columns if c is not None]
|
|
10
|
+
orders = [o for o in orders if o is not None]
|
|
11
|
+
order_clause = f"ORDER BY {', '.join(orders)}" if len(orders) > 0 else ""
|
|
12
|
+
limitation = f"LIMIT {limit}" if limit is not None else ""
|
|
13
|
+
offset = f"OFFSET {(props.pagination - 1) * limit}" if limit is not None else ""
|
|
14
|
+
dimension_search_where = (
|
|
15
|
+
f"OR (unaccent(LOWER({indicator.flows_dimension})) LIKE unaccent(LOWER('%' || '{props.search}' || '%')))"
|
|
16
|
+
if props.flows
|
|
17
|
+
else " ".join(
|
|
18
|
+
[
|
|
19
|
+
f"OR (unaccent(LOWER({dimension.db_name})) LIKE unaccent(LOWER('%' || '{props.search}' || '%')))"
|
|
20
|
+
for dimension in indicator.dimensions.all()
|
|
21
|
+
]
|
|
22
|
+
)
|
|
23
|
+
)
|
|
24
|
+
territory_search_where = (
|
|
25
|
+
f"OR (unaccent(LOWER(territory_1)) LIKE unaccent(LOWER('%' || '{props.search}' || '%'))) OR (unaccent(LOWER(territory_2)) LIKE unaccent(LOWER('%' || '{props.search}' || '%')))"
|
|
26
|
+
if props.flows
|
|
27
|
+
else (
|
|
28
|
+
f"OR (unaccent(LOWER(lieu)) LIKE unaccent(LOWER('%' || '{props.search}' || '%')))"
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
where = (
|
|
32
|
+
f"""
|
|
33
|
+
AND (
|
|
34
|
+
(LENGTH(TRIM('{props.search}')) = 0)
|
|
35
|
+
{territory_search_where}
|
|
36
|
+
{dimension_search_where}
|
|
37
|
+
OR (CAST(annee AS TEXT) LIKE '%' || '{props.search}' || '%')
|
|
38
|
+
)
|
|
39
|
+
"""
|
|
40
|
+
if props.search
|
|
41
|
+
else ""
|
|
42
|
+
)
|
|
43
|
+
where_year = f"AND annee = {props.year}" if props.year else ""
|
|
44
|
+
regular_dimensions_filters = (
|
|
45
|
+
"" if props.flows else add_optional_filters(indicator, filters)
|
|
46
|
+
)
|
|
47
|
+
return f"""
|
|
48
|
+
SELECT
|
|
49
|
+
{", ".join(columns)}
|
|
50
|
+
FROM
|
|
51
|
+
{get_table_data_for_geography(indicator, props.territory, props.submesh, True, props.flows)}
|
|
52
|
+
{regular_dimensions_filters}
|
|
53
|
+
{where} {where_year}
|
|
54
|
+
{order_clause}
|
|
55
|
+
{f"{limitation} {offset}"}
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def get_values_columns(indicator, props, *, for_export=False):
|
|
60
|
+
if props.flows:
|
|
61
|
+
columns = [
|
|
62
|
+
"annee",
|
|
63
|
+
"territory_1",
|
|
64
|
+
"territory_2",
|
|
65
|
+
f"{indicator.flows_dimension} AS dimension"
|
|
66
|
+
if indicator.flows_dimension
|
|
67
|
+
else None,
|
|
68
|
+
"CAST(valeur AS int) as valeur",
|
|
69
|
+
]
|
|
70
|
+
if for_export:
|
|
71
|
+
columns += ["territory_1_id", "territory_2_id"]
|
|
72
|
+
else:
|
|
73
|
+
columns = (
|
|
74
|
+
["annee", "lieu"]
|
|
75
|
+
+ [f"{dimension.db_name}" for dimension in indicator.dimensions.all()]
|
|
76
|
+
+ [
|
|
77
|
+
f"valeur * {indicator.aggregation_constant if indicator.unite == '%' else 1} AS valeur",
|
|
78
|
+
"CASE WHEN valeur IS NULL THEN NULL ELSE composante_1 END AS valeur_alternative"
|
|
79
|
+
if indicator.is_composite and indicator.show_alternative
|
|
80
|
+
else None,
|
|
81
|
+
]
|
|
82
|
+
)
|
|
83
|
+
if for_export:
|
|
84
|
+
columns += ["territoryid"]
|
|
85
|
+
return columns
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def get_values_orders(indicator, props: IndicatorTablePayload):
|
|
89
|
+
territory_order = None
|
|
90
|
+
if props.column_order not in ["lieu", "territory_1", "territory_2"]:
|
|
91
|
+
territory_order = (
|
|
92
|
+
"LOWER(territory_1) ASC, LOWER(territory_2) ASC"
|
|
93
|
+
if props.flows
|
|
94
|
+
else "LOWER(lieu) ASC"
|
|
95
|
+
)
|
|
96
|
+
year_order = "annee DESC" if props.column_order != "annee" else None
|
|
97
|
+
dimension_order = None
|
|
98
|
+
if props.flows:
|
|
99
|
+
dimension = indicator.flows_dimension
|
|
100
|
+
if props.column_order != dimension:
|
|
101
|
+
dimension_order = f"{dimension} DESC"
|
|
102
|
+
else:
|
|
103
|
+
dimensions = [dimension.db_name for dimension in indicator.dimensions.all()]
|
|
104
|
+
if props.column_order not in [dimensions]:
|
|
105
|
+
dimension_order = (
|
|
106
|
+
", ".join([f"{dimension} DESC" for dimension in dimensions])
|
|
107
|
+
if dimensions
|
|
108
|
+
else None
|
|
109
|
+
)
|
|
110
|
+
orders = [
|
|
111
|
+
f"{props.column_order} {props.column_order_flow} NULLS LAST"
|
|
112
|
+
if props.column_order
|
|
113
|
+
else None,
|
|
114
|
+
year_order,
|
|
115
|
+
territory_order,
|
|
116
|
+
dimension_order,
|
|
117
|
+
]
|
|
118
|
+
return orders
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def get_count_and_data_for_indicator_table(
|
|
122
|
+
indicator, props: IndicatorTablePayload, filters
|
|
123
|
+
):
|
|
124
|
+
count_query = _get_query(
|
|
125
|
+
indicator=indicator,
|
|
126
|
+
props=props,
|
|
127
|
+
columns=["COUNT(*)"],
|
|
128
|
+
orders=[],
|
|
129
|
+
filters=filters,
|
|
130
|
+
)
|
|
131
|
+
count = run_custom_query(count_query)
|
|
132
|
+
columns = get_values_columns(indicator, props)
|
|
133
|
+
orders = get_values_orders(indicator, props)
|
|
134
|
+
data_query = _get_query(
|
|
135
|
+
indicator=indicator,
|
|
136
|
+
props=props,
|
|
137
|
+
columns=columns,
|
|
138
|
+
orders=orders,
|
|
139
|
+
limit=props.limit,
|
|
140
|
+
filters=filters,
|
|
141
|
+
)
|
|
142
|
+
data = run_custom_query(data_query)
|
|
143
|
+
return count[0]["count"], data
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def get_export_indicator_table_values(indicator, props: IndicatorTablePayload, filters):
|
|
147
|
+
query = _get_query(
|
|
148
|
+
indicator=indicator,
|
|
149
|
+
props=props,
|
|
150
|
+
columns=get_values_columns(indicator, props, for_export=True),
|
|
151
|
+
orders=get_values_orders(indicator, props),
|
|
152
|
+
filters=filters,
|
|
153
|
+
)
|
|
154
|
+
return run_custom_query(query)
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
from django.urls import path
|
|
2
|
+
|
|
3
|
+
from .views import (
|
|
4
|
+
comparison_histogram_export_view,
|
|
5
|
+
comparison_histogram_view,
|
|
6
|
+
download_indicator_methodo_view,
|
|
7
|
+
flows_view,
|
|
8
|
+
indicator_details_table_export_view,
|
|
9
|
+
indicator_details_table_view,
|
|
10
|
+
indicator_histogram_export_view,
|
|
11
|
+
indicator_histogram_view,
|
|
12
|
+
indicator_proportions_export_view,
|
|
13
|
+
indicator_statistics_view,
|
|
14
|
+
indicator_submesh_territories_view,
|
|
15
|
+
indicator_top_10_export_view,
|
|
16
|
+
indicator_top_10_view,
|
|
17
|
+
indicator_values_export_view,
|
|
18
|
+
indicator_values_view,
|
|
19
|
+
proportions_chart_view,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
app_name = "indicators-api"
|
|
23
|
+
|
|
24
|
+
urlpatterns = [
|
|
25
|
+
path("flows/", flows_view, name="flows"),
|
|
26
|
+
path(
|
|
27
|
+
"<str:name>/methodo/",
|
|
28
|
+
download_indicator_methodo_view,
|
|
29
|
+
name="download-indicator-methodo",
|
|
30
|
+
),
|
|
31
|
+
path(
|
|
32
|
+
"<str:name>/statistics/",
|
|
33
|
+
indicator_statistics_view,
|
|
34
|
+
name="statistics",
|
|
35
|
+
),
|
|
36
|
+
path(
|
|
37
|
+
"<str:name>/values/export/",
|
|
38
|
+
indicator_values_export_view,
|
|
39
|
+
name="values",
|
|
40
|
+
),
|
|
41
|
+
path(
|
|
42
|
+
"<str:name>/values/",
|
|
43
|
+
indicator_values_view,
|
|
44
|
+
name="values",
|
|
45
|
+
),
|
|
46
|
+
path(
|
|
47
|
+
"<str:name>/histogram/export/",
|
|
48
|
+
indicator_histogram_export_view,
|
|
49
|
+
name="histogram-export",
|
|
50
|
+
),
|
|
51
|
+
path(
|
|
52
|
+
"<str:name>/histogram/",
|
|
53
|
+
indicator_histogram_view,
|
|
54
|
+
name="histogram",
|
|
55
|
+
),
|
|
56
|
+
path(
|
|
57
|
+
"<str:name>/top-10/export/",
|
|
58
|
+
indicator_top_10_export_view,
|
|
59
|
+
name="top-10-export",
|
|
60
|
+
),
|
|
61
|
+
path(
|
|
62
|
+
"<str:name>/top-10/",
|
|
63
|
+
indicator_top_10_view,
|
|
64
|
+
name="top-10",
|
|
65
|
+
),
|
|
66
|
+
path(
|
|
67
|
+
"<str:name>/details/table/",
|
|
68
|
+
indicator_details_table_view,
|
|
69
|
+
name="details-table",
|
|
70
|
+
),
|
|
71
|
+
path(
|
|
72
|
+
"<str:name>/details/table/export/",
|
|
73
|
+
indicator_details_table_export_view,
|
|
74
|
+
name="details-table-export",
|
|
75
|
+
),
|
|
76
|
+
path(
|
|
77
|
+
"<str:name>/comparison-histogram/export/",
|
|
78
|
+
comparison_histogram_export_view,
|
|
79
|
+
name="comparison-histogram-export",
|
|
80
|
+
),
|
|
81
|
+
path(
|
|
82
|
+
"<str:name>/comparison-histogram/",
|
|
83
|
+
comparison_histogram_view,
|
|
84
|
+
name="comparison-histogram",
|
|
85
|
+
),
|
|
86
|
+
path(
|
|
87
|
+
"<str:name>/proportions/export/",
|
|
88
|
+
indicator_proportions_export_view,
|
|
89
|
+
name="proportions-export",
|
|
90
|
+
),
|
|
91
|
+
path(
|
|
92
|
+
"<str:name>/proportions/",
|
|
93
|
+
proportions_chart_view,
|
|
94
|
+
name="proportions",
|
|
95
|
+
),
|
|
96
|
+
path("<str:name>/submesh/", indicator_submesh_territories_view, name="submesh"),
|
|
97
|
+
]
|