From 85a3fd05dcccf02b956bffe4d51519453f2ede35 Mon Sep 17 00:00:00 2001 From: "Alexis A." Date: Tue, 1 Oct 2024 10:57:19 +0200 Subject: [PATCH] fix(project): remove unused fields and add alpha validator --- project/models/create.py | 1 - project/models/project_base.py | 66 +---------- .../commands/create_rnu_diagnostics.py | 109 ------------------ .../commands/create_rnu_packages.py | 62 ---------- 4 files changed, 4 insertions(+), 234 deletions(-) delete mode 100644 public_data/management/commands/create_rnu_diagnostics.py delete mode 100644 public_data/management/commands/create_rnu_packages.py diff --git a/project/models/create.py b/project/models/create.py index ab984d5ff..b8f6de3cd 100644 --- a/project/models/create.py +++ b/project/models/create.py @@ -104,7 +104,6 @@ def create_from_public_key( user=user if user and user.is_authenticated else None, ) project._change_reason = ProjectChangeReason.CREATED_FROM_PUBLIC_KEY - project.set_success(save=True) trigger_async_tasks(project, public_key) diff --git a/project/models/project_base.py b/project/models/project_base.py index d7f83a05b..44c5f9f51 100644 --- a/project/models/project_base.py +++ b/project/models/project_base.py @@ -1,6 +1,5 @@ import collections import logging -import traceback from decimal import Decimal from typing import Dict, List, Literal @@ -16,7 +15,6 @@ from django.db.models import Case, Count, DecimalField, F, Q, QuerySet, Sum, Value, When from django.db.models.functions import Cast, Coalesce, Concat from django.urls import reverse -from django.utils import timezone from django.utils.functional import cached_property from simple_history.models import HistoricalRecords @@ -44,6 +42,7 @@ from public_data.models.gpu import ArtifAreaZoneUrba, ZoneUrba from public_data.models.mixins import DataColorationMixin from utils.db import cast_sum_area +from utils.validators import is_alpha_validator logger = logging.getLogger(__name__) @@ -74,20 +73,7 @@ class Status(models.TextChoices): blank=True, null=True, ) - name = models.CharField("Nom", max_length=100) - # fields to track the shape files importation into the database - import_error = models.TextField( - "Message d'erreur traitement emprise", - null=True, - blank=True, - ) - import_date = models.DateTimeField("Date et heure d'import", null=True, blank=True) - import_status = models.CharField( - "Statut import", - max_length=10, - choices=Status.choices, - default=Status.MISSING, - ) + name = models.CharField("Nom", max_length=100, validators=[is_alpha_validator]) @cached_property def combined_emprise(self) -> MultiPolygon: @@ -101,23 +87,6 @@ def combined_emprise(self) -> MultiPolygon: def __str__(self): return self.name - def set_success(self, save=True): - self.import_status = self.Status.SUCCESS - self.import_date = timezone.now() - self.import_error = None - if save: - self.save_without_historical_record() - - def set_failed(self, save=True, trace=None): - self.import_status = self.Status.FAILED - self.import_date = timezone.now() - if trace: - self.import_error = trace - else: - self.import_error = traceback.format_exc() - if save: - self.save_without_historical_record() - class Meta: abstract = True @@ -318,7 +287,8 @@ def get_public_key(self) -> str: max_length=250, blank=True, null=True, - help_text=("C'est le nom qui est utilisé pour désigner votre territoire, notamment " "dans le rapport word."), + help_text="C'est le nom qui est utilisé pour désigner votre territoire, notamment dans le rapport word.", + validators=[is_alpha_validator], ) cover_image = models.ImageField( @@ -658,20 +628,6 @@ def get_look_a_like(self): self.save(update_fields=["look_a_like"]) return sorted(lands, key=lambda x: x.name) - # calculated fields - # Following field contains calculated dict : - # { - # '2015': { # millésime - # 'couverture': { # covering type - # 'cs1.1.1': 123, # code and area in km square - # 'cs1.1.2': 23, - # }, - # 'usage': { ... }, # same as couverture - # }, - # '2018': { ... }, # same as 2015 - # } - couverture_usage = models.JSONField(blank=True, null=True) - def get_cerema_cities(self, group_name=None): if not group_name: code_insee = self.cities.all().values_list("insee", flat=True) @@ -737,8 +693,6 @@ def get_bilan_conso_time_scoped(self): analyze_start_data and analyze_end_date) Evaluation is based on city consumption, not geo work.""" qs = self.get_cerema_cities() - # if not qs.exists(): - # return 0 fields = Cerema.get_art_field(self.analyse_start_date, self.analyse_end_date) sum_function = sum([F(f) for f in fields]) qs = qs.annotate(line_sum=sum_function) @@ -840,17 +794,6 @@ def get_look_a_like_pop_change_per_year( def get_absolute_url(self): return reverse("project:detail", kwargs={"pk": self.pk}) - def reset(self, save=False): - """Remove everything from project dependencies - ..TODO:: overload delete to remove files""" - self.emprise_set.all().delete() - self.import_status = BaseProject.Status.MISSING - self.import_date = None - self.import_error = None - self.couverture_usage = None - if save: - self.save() - def get_artif_area(self): """Return artificial surface total for all city inside diagnostic""" result = self.cities.all().aggregate(total=Sum("surface_artif")) @@ -1014,7 +957,6 @@ def get_bounding_box(self): return list(result["bbox"]) def get_centroid(self): - # result = self.emprise_set.aggregate(bbox=Extent('mpoly')) result = self.emprise_set.aggregate(center=Centroid(Union("mpoly"))) return result["center"] diff --git a/public_data/management/commands/create_rnu_diagnostics.py b/public_data/management/commands/create_rnu_diagnostics.py deleted file mode 100644 index 78eafbaff..000000000 --- a/public_data/management/commands/create_rnu_diagnostics.py +++ /dev/null @@ -1,109 +0,0 @@ -import logging - -from django.core.management.base import BaseCommand -from django.utils import timezone - -from project.models import Emprise, Project, Request -from project.models.create import trigger_async_tasks_rnu_pakage_one_off -from public_data.models import Commune, Land -from public_data.models.sudocuh import DocumentUrbanismeChoices, Sudocuh -from users.models import User -from utils.db import fix_poly - -logger = logging.getLogger("management.commands") - - -class Command(BaseCommand): - help = "create_rnu_diagnostics" - - def add_arguments(self, parser): - parser.add_argument("--departement", type=str) - - def handle(self, *args, **options): - mondiagartif_user, _ = User.objects.get_or_create( - email="rnu.package@mondiagartif.beta.gouv.fr", - first_name="Alexis", - last_name="Athlani", - organism=User.ORGANISMS.DDT, - function="Développeur", - defaults={"email_checked": timezone.now()}, - ) - - projects = [] - - communes = Commune.objects.filter( - 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( - user=mondiagartif_user, - land_id__in=[str(commune.id) for commune in communes], - ) - - request_to_delete = Request.objects.filter(project__in=projects_to_delete) - - _, detail_request_deleted = request_to_delete.delete() - _, detail_project_deleted = projects_to_delete.delete() - - logger.info(f"Deleted : {detail_request_deleted}") - logger.info(f"Deleted : {detail_project_deleted}") - - for commune in communes: - land = Land(public_key=f"COMM_{commune.id}") - project = Project.objects.create( - name=f"Diagnostic de {land.name}", - is_public=True, - analyse_start_date="2011", - analyse_end_date="2022", - level="COMM", - land_id=str(commune.id), - land_type=land.land_type, - territory_name=land.name, - user=mondiagartif_user, - import_status=Project.Status.SUCCESS, - import_date=timezone.now(), - import_error=None, - async_add_city_done=True, - first_year_ocsge=commune.first_millesime, - last_year_ocsge=commune.last_millesime, - available_millesimes=[commune.first_millesime, commune.last_millesime], - async_find_first_and_last_ocsge_done=True, - ocsge_coverage_status=Project.OcsgeCoverageStatus.COMPLETE_UNIFORM - if commune.ocsge_available - else Project.OcsgeCoverageStatus.NO_DATA, - async_ocsge_coverage_status_done=True, - async_set_combined_emprise_done=True, - async_theme_map_gpu_done=True, - async_theme_map_fill_gpu_done=True, - async_add_comparison_lands_done=True, - ) - - project.cities.add(commune) - - Emprise.objects.create( - mpoly=fix_poly(commune.mpoly), - srid_source=commune.srid_source, - project=project, - ) - - similar_lands_public_keys = [ - comparison_land.public_key for comparison_land in project.get_comparison_lands() - ] - - project.refresh_from_db() - - project.add_look_a_like(public_key=similar_lands_public_keys, many=True) - - projects.append(project) - - logger.info(f"Created {len(projects)} projects") - - for project in projects: - trigger_async_tasks_rnu_pakage_one_off(project) - - logger.info("All projects have been created and async tasks have been triggered") diff --git a/public_data/management/commands/create_rnu_packages.py b/public_data/management/commands/create_rnu_packages.py deleted file mode 100644 index e409d0924..000000000 --- a/public_data/management/commands/create_rnu_packages.py +++ /dev/null @@ -1,62 +0,0 @@ -import logging - -import celery -from django.core.management.base import BaseCommand - -from project.models import Request -from project.tasks import create_zip_departement_rnu_package_one_off -from public_data.models import Commune, Departement, Sudocuh -from public_data.models.sudocuh import DocumentUrbanismeChoices -from users.models import User - -logger = logging.getLogger("management.commands") - - -class Command(BaseCommand): - help = "create_rnu_packages" - - def add_arguments(self, parser): - parser.add_argument("--departement", type=str, required=False) - - def check_requests_are_created_for_departement(self, departement): - communes = Commune.objects.filter( - insee__in=[Sudocuh.objects.filter(du_opposable=DocumentUrbanismeChoices.RNU).values("code_insee")], - departement=departement, - ) - - commune_count = communes.count() - request_for_communes_count = Request.objects.filter( - done=True, - project__land_id__in=[str(commune.id) for commune in communes], - user=User.objects.get(email="rnu.package@mondiagartif.beta.gouv.fr"), - ).count() - - if commune_count != request_for_communes_count: - logger.error( - ( - f"Commune count ({commune_count}) does not match " - f"request for communes count ({request_for_communes_count})" - ) - ) - return False - else: - logger.info( - f"Commune count ({commune_count}) matches request for communes count ({request_for_communes_count})" - ) - - return True - - def handle(self, *args, **options): - tasks = [] - - departements = Departement.objects.all() - - if options["departement"]: - departements = departements.filter(source_id=options["departement"]) - - for departement in departements: - if self.check_requests_are_created_for_departement(departement): - logger.info(f"Creating RNU package for departement {departement.source_id}") - tasks.append(create_zip_departement_rnu_package_one_off.si(departement.source_id)) - - celery.group(*tasks).apply_async(queue="long")