Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 99e237b

Browse files
authored
Merge pull request #19 from entrepreneur-interet-general/sp7
Sp7
2 parents 2475d3d + 5ff35ac commit 99e237b

File tree

8 files changed

+46
-9
lines changed

8 files changed

+46
-9
lines changed

core/services/context_data.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,12 @@ def format_table(self, table, format_for_web: bool = True) -> None:
120120

121121
row.append(label)
122122
for siren_id in self.list_sirens():
123-
if prop["type"] == "numeric" and format_for_web:
124-
row.append(format_number(self.context_data[siren_id][field]))
123+
if prop["type"] == "numeric":
124+
row.append(
125+
format_number(
126+
self.context_data[siren_id][field], format_for_web
127+
)
128+
)
125129
elif prop["type"] == "boolean":
126130
row.append(self.format_boolean(siren_id, prop))
127131
else:

core/services/utils.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,19 @@ def init_payload(page_title: str, links: list = []):
3131
return {"context": context, "title": page_title, "breadcrumb_data": breadcrumb_data}
3232

3333

34-
def format_number(n):
34+
def format_number(n, format_for_web):
3535
"""
36-
Format the number with French locale and rounds to one decimal place if needed
36+
Rounds a number to one decimal place if needed.
37+
38+
If format_for_web is True, then it also applies French locale (comma
39+
for decimal separator, narrow no-break space for thousands separator)
3740
"""
3841
if type(n) in [int, float]:
39-
return format_decimal(n, locale="fr_FR", format="#,##0.#")
42+
if format_for_web:
43+
return format_decimal(n, locale="fr_FR", format="#,##0.#")
44+
else:
45+
# Useful for csv export
46+
return format_decimal(n, locale="en_US", format="###0.#")
4047
elif n is None:
4148
return ""
4249
else:

core/templates/core/commune_compare.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ <h2 id="donnees-contexte">Données de contexte</h2>
1919
href="{{ data.export_link }}"
2020
target="_blank"
2121
rel="noopener"
22-
class="fr-link fr-fi-arrow-right-line fr-link--icon-right"
22+
class="fr-link fr-fi-download-line fr-link--icon-right"
2323
>
2424
Télécharger le comparatif des communes sélectionnées au format csv
2525
</a>

core/templates/core/commune_detail.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
{% endblock %}
1717

1818
{% block content %}
19-
<div class="fr-container">
19+
<div class="fr-container oc-padding-top-2em oc-padding-bottom-3em">
2020
{% include "core/dsfr/breadcrumb.html" with breadcrumb_data=breadcrumb_data %}
2121

2222
{# Intro #}
@@ -101,6 +101,18 @@ <h2 id="ressources-financieres-fiscales">
101101
Plus d’informations sur le site dotations de la DGCL
102102
</a>
103103

104+
<p class="oc-padding-top-2em">
105+
<a
106+
title="Export données de la commune"
107+
href="{% url 'core:csv_commune_export' slug %}"
108+
target="_blank"
109+
rel="noopener"
110+
class="fr-link fr-fi-download-line fr-link--icon-right"
111+
>
112+
Télécharger les données de la commune au format csv
113+
</a>
114+
</p>
115+
104116
<h2 id="comparaison-autres-communes">
105117
Comparaison avec d’autres communes
106118
</h2>

core/templates/core/departement_detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ <h2 id="perimetre">Périmètre</h2>
6969
href="{% url 'core:csv_departement_compare_communes' slug %}"
7070
target="_blank"
7171
rel="noopener"
72-
class="fr-link fr-fi-arrow-right-line fr-link--icon-right"
72+
class="fr-link fr-fi-download-line fr-link--icon-right"
7373
>
7474
Télécharger le comparatif des {{ data.communes_count }} communes au format csv
7575
</a>

core/templates/core/epci_detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ <h3 id="perimetre">Périmètre</h3>
137137
href="{% url 'core:csv_epci_compare_communes' slug %}"
138138
target="_blank"
139139
rel="noopener"
140-
class="fr-link fr-fi-arrow-right-line fr-link--icon-right"
140+
class="fr-link fr-fi-download-line fr-link--icon-right"
141141
>
142142
Télécharger le comparatif des {{ data.members.count }} communes au format csv
143143
</a>

core/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
views.page_commune_compare,
5454
name="page_commune_compare",
5555
),
56+
path(
57+
"export/commune/<slug:slug>",
58+
views.csv_commune_export,
59+
name="csv_commune_export",
60+
),
5661
re_path(
5762
r"^compare_export\/commune\/(?P<slug1>[-\w]+)\/(?P<slug2>[-\w]+)\/?(?P<slug3>[-\w]+)?\/?(?P<slug4>[-\w]+)?\/?",
5863
views.csv_compare_communes_from_list,

core/views.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def page_commune_detail(request, slug):
6969
commune = get_object_or_404(Commune, slug=slug)
7070

7171
payload = init_payload(f"Fiche commune : {commune.name}")
72+
payload["slug"] = slug
7273
payload["siren"] = commune.siren
7374
payload["commune_name"] = commune.name
7475
payload["data"] = commune_data(commune.siren)
@@ -404,6 +405,14 @@ def page_sitemap(request):
404405
###############
405406
# CSV exports #
406407
###############
408+
@require_safe
409+
def csv_commune_export(request, slug):
410+
commune = Commune.objects.filter(slug=slug)
411+
# Commune.objects.filter is used instead of get_object_or_404 because
412+
# compare_communes_for_export expects a QuerySet
413+
filename = f"export-commune-{slug}"
414+
response = compare_communes_for_export(commune, filename)
415+
return response
407416

408417

409418
@require_safe

0 commit comments

Comments
 (0)