Skip to content

Commit

Permalink
feat(rnu-packages): misc
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisig committed Jul 18, 2024
1 parent 469ae93 commit 782c1cb
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 136 deletions.
43 changes: 32 additions & 11 deletions home/templates/home/download.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,38 @@
{% block content %}
<section class="fr-py-6w">
<div class="fr-container">
<h1>Téléchargements</h1>
<h2>Trames de rapport triennal local des communes sous RNU</h2>
<p>Mon Diagnostic Artificialisation met à disposition des DDT les trames de rapport triennal local par paquets de leurs communes sous RNU</p>
<ul>
{% for departement in departements %}
<li class="fr-mb-2w">
<div>{{ departement.source_id }} {{ departement.name }}</div>
<div><a href="#" target="_blank">Lien de téléchargement</a></div>
</li>
{% endfor %}
</ul>
<h1>Trames de rapport triennal local des communes au RNU</h1>
<p>
Mon Diagnostic Artificialisation met à disposition des DDT les trames de rapport triennal local par paquets de leurs communes au RNU
</p>
<div class="fr-table fr-table--bordered">
<div class="fr-table__wrapper">
<div class="fr-table__container">
<div class="fr-table__content fr-table--no-scrol">
<table>
<thead>
<tr>
<th scope="col">Département</th>
<th scope="col">Nombre de communes au RNU</th>
<th scope="col">Date de création</th>
<th scope="col">Lien</th>
</tr>
</thead>
<tbody>
{% for package in rnu_packages %}
<tr>
<td>{{ package.departement_official_id }} - {{ package.departement.name }}</td>
<td>{{ package.communes|length }}</td>
<td>{{ package.created_at }}</td>
<td><a target="_blank" rel="noopener noreferrer" href="{{ package.file.url }}">Lien de téléchargement</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</section>
{% include "home/partials/newsletter_form.html" %}
Expand Down
2 changes: 1 addition & 1 deletion home/templates/home/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h1 class="fr-mb-0">Mon Diagnostic Artificialisation vous aide à analyser et ma
<p class="fr-badge fr-badge--new">Nouveau</p>
<p class="fr-text--sm">
Exportez une trame de votre rapport triennal local de suivi de l'artificialisation des sols conformément à l'<a href="https://www.legifrance.gouv.fr/codes/article_lc/LEGIARTI000048470630" rel="noopener external" target="_blank">article L. 2231-1 du code général des collectivités territoriales</a>.<br>
<strong>Pour les DDT, ces trames sont disponibles en <a href="{% url 'home:downloads' %}">téléchargement par paquets</a> pour les communes sous RNU.</strong>
<strong>Pour les DDT, ces trames sont disponibles en <a href="{% url 'home:downloads' %}">téléchargement par paquets</a> pour les communes au RNU.</strong>
</p>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion home/templates/home/home_rapport_local.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h1 class="fr-mb-0">Préparer le rapport triennal local de suivi de l’artifici
<div class="fr-container">
<div class="fr-notice__body">
<p class="fr-text--sm">
<strong>Pour les DDT, ces trames sont disponibles en <a href="{% url 'home:downloads' %}">téléchargement par paquets</a> pour les communes sous RNU.</strong>
<strong>Pour les DDT, ces trames sont disponibles en <a href="{% url 'home:downloads' %}">téléchargement par paquets</a> pour les communes au RNU.</strong>
</p>
</div>
</div>
Expand Down
9 changes: 4 additions & 5 deletions home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.mixins import UserPassesTestMixin
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.db.models import F, Value
from django.http import HttpRequest, HttpResponse, HttpResponseGone
from django.shortcuts import redirect
Expand All @@ -12,8 +12,7 @@
from django_app_parameter import app_parameter

from brevo.connectors import Brevo
from project.models import Request
from public_data.models import Departement
from project.models import Request, RNUPackage
from users.models import User
from utils.functions import get_url_with_domain
from utils.htmx import HtmxRedirectMixin, StandAloneMixin
Expand All @@ -36,13 +35,13 @@ def get_context_data(self, **kwargs):
return super().get_context_data(**kwargs)


class DownloadView(BreadCrumbMixin, TemplateView):
class DownloadView(LoginRequiredMixin, BreadCrumbMixin, TemplateView):
template_name = "home/download.html"

def get_context_data(self, **kwargs):
kwargs |= {
"form": NewsletterForm(),
"departements": Departement.objects.all().order_by("source_id"),
"rnu_packages": RNUPackage.objects.all().order_by("departement_official_id"),
}
return super().get_context_data(**kwargs)

Expand Down
15 changes: 15 additions & 0 deletions project/models/RNUPackage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from django.db import models
from django.utils.functional import cached_property

from public_data.models import Departement
from public_data.models.sudocuh import DocumentUrbanismeChoices, Sudocuh


class RNUPackage(models.Model):
Expand All @@ -10,3 +14,14 @@ class RNUPackage(models.Model):
max_length=10,
unique=True,
)

@cached_property
def departement(self):
return Departement.objects.get(source_id=self.departement_official_id)

@cached_property
def communes(self):
sudocuh = Sudocuh.objects.filter(du_opposable=DocumentUrbanismeChoices.RNU)
return self.departement.commune_set.filter(
insee__in=sudocuh.values("code_insee"),
)
12 changes: 11 additions & 1 deletion project/tasks/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from public_data.domain.containers import PublicDataContainer
from public_data.models import AdminRef, ArtificialArea, Departement, Land, OcsgeDiff
from public_data.models.gpu import ArtifAreaZoneUrba, ZoneUrba
from public_data.storages import DataStorage
from utils.db import fix_poly
from utils.emails import SibTemplateEmail
from utils.functions import get_url_with_domain
Expand Down Expand Up @@ -890,9 +891,18 @@ def create_zip_departement_rnu_package_one_off(departement_id: str) -> None:
project__land_id__in=commune_in_departement_ids_as_string,
)

notice_file_path = f"rnu_packages/NOTICE_{departement_id}.pdf"
rnu_communes_map_file_path = f"rnu_packages/COMM_DU_{departement_id}.pdf"

file_name = f"rnu_package_departement_{departement_id}.zip"

with zipfile.ZipFile(file_name, "a", compression=zipfile.ZIP_DEFLATED) as zipf:
with zipfile.ZipFile(file_name, "a", compression=zipfile.ZIP_DEFLATED, compresslevel=9) as zipf:
notice_file = DataStorage().open(notice_file_path, "rb")
rnu_communes_map_file = DataStorage().open(rnu_communes_map_file_path, "rb")

zipf.writestr(f"NOTICE_{departement_id}.pdf", notice_file.read())
zipf.writestr(f"COMM_DU_{departement_id}.pdf", rnu_communes_map_file.read())

for request in requests_created_by_the_rnu_package_service_account:
if not request.sent_file:
raise ValueError(f"Request {request.id} has no sent file")
Expand Down
83 changes: 83 additions & 0 deletions project/templates/project/rnu_package_notice.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{% load sri %}
{% sri_static "assets/styles/main.css" %}

<!DOCTYPE html>
<html>
<head>
<style>
a:visited { color:blue }
body {
height: 842px;
width: 595px;
/* to centre page on screen*/
margin-left: auto;
margin-right: auto;
}
h1 {
font-size: 2em;
}
</style>
</head>
<body>
<h1>Paquet de rapports locaux des communes au RNU du département {{ object.departement.name }} ({{ object.departement.source_id }})</h1>
<ul>
<li>Date de production du paquet : {{ object.created_at }}</li>
<li>Source de donnée ayant permis de déterminer la liste des communes au RNU :
<a target="_blank" rel="noreferrer noopener" href="https://www.data.gouv.fr/fr/datasets/planification-nationale-des-documents-durbanisme-plu-plui-cc-rnu-donnees-sudocuh-dernier-etat-des-lieux-annuel-au-31-decembre-2023/#/resources">
Sudocuh
</a>
</li>
</ul>
<p>
Ce paquet de trames de rapport local s'adresse aux DDT.
Il contient un rapport local par commune au RNU du département {{ object.departement.name }} ({{ object.departement.source_id }}).
</p>
<p>
Une version web de ces diagnostics est disponible.
Un lien est disponible dans le bas de page de chaque rapport.
</p>

<div class="fr-table fr-table--bordered">
<div class="fr-table__wrapper">
<div class="fr-table__container">
<div class="fr-table__content fr-table--no-scrol">
<table>
<caption>
Liste des fichiers du paquet
</caption>
<thead>
<tr>
<th scope="col">Fichier</th>
<th scope="col">Chemin</th>
</tr>
</thead>
<tbody>
<tr class="meta_row">
<td>Ce document</td>
<td>NOTICE_{{ object.departement_official_id }}.pdf</td>
</tr>
<tr class="meta_row">
<td>Carte des communes au RNU du département</td>
<td>COMM_DU_{{ object.departement_official_id }}.pdf</td>
</tr>
{% for commune in communes %}
<tr>
<td>Rapport de <a target="_blank" rel="noreferrer noopener" href="https://www.insee.fr/fr/statistiques/2011101?geo=COM-{{ commune.insee }}">
{{ commune.name }}
</a></td>
<td>{{ commune.departement.source_id }}_COMM_{{ commune.insee }}.docx</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
<hr>
<p>
Crée par <a target="_blank" rel="noopener noreferrer" href="https://mondiagartif.beta.gouv.fr">Mon Diagnostic Artificialisation</a>
</p>

</body>
</html>
1 change: 0 additions & 1 deletion project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@
path("<int:pk>/export-excel", views.ExportExcelView.as_view(), name="export-excel"),
# SUB APPS
path("test", views.TestView.as_view(), name="test"),
path(route="rnu-packages-progress", view=views.RNUPackagesProgressView.as_view(), name="rnu-packages-progress"),
]


Expand Down
92 changes: 0 additions & 92 deletions project/views/RNUPackagesProgressView.py

This file was deleted.

1 change: 0 additions & 1 deletion project/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
from .export import *
from .map import *
from .report import *
from .RNUPackagesProgressView import RNUPackagesProgressView
6 changes: 4 additions & 2 deletions public_data/management/commands/create_rnu_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Command(BaseCommand):
help = "create_rnu_diagnostics"

def add_arguments(self, parser):
parser.add_argument("--departement", type=str, required=True)
parser.add_argument("--departement", type=str)

def handle(self, *args, **options):
mondiagartif_user, _ = User.objects.get_or_create(
Expand All @@ -32,10 +32,12 @@ def handle(self, *args, **options):
projects = []

communes = Commune.objects.filter(
departement__source_id=options["departement"],
insee__in=[Sudocuh.objects.filter(du_opposable=DocumentUrbanismeChoices.RNU).values("code_insee")],
)

if options["departement"]:
communes = communes.filter(departement__source_id=options["departement"])

logger.info(f"Found {len(communes)} RNU communes")

projects_to_delete = Project.objects.filter(
Expand Down
Loading

0 comments on commit 782c1cb

Please sign in to comment.