From f623155fc8d961a612c16e1243bb5ba2472db4a8 Mon Sep 17 00:00:00 2001 From: "Alexis A." Date: Wed, 20 Nov 2024 16:39:41 +0100 Subject: [PATCH] feat(project): only generate cover image once per territory --- ...0102_alter_project_cover_image_and_more.py | 24 +++++++++++ project/models/project_base.py | 10 ++++- project/tasks/project.py | 42 ++++++++++++++----- 3 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 project/migrations/0102_alter_project_cover_image_and_more.py diff --git a/project/migrations/0102_alter_project_cover_image_and_more.py b/project/migrations/0102_alter_project_cover_image_and_more.py new file mode 100644 index 000000000..2744f9df4 --- /dev/null +++ b/project/migrations/0102_alter_project_cover_image_and_more.py @@ -0,0 +1,24 @@ +# Generated by Django 4.2.13 on 2024-11-20 15:10 + +import config.storages +from django.db import migrations, models +import project.models.project_base + + +class Migration(migrations.Migration): + dependencies = [ + ("project", "0101_remove_projectcommune_commune_insee"), + ] + + operations = [ + migrations.AlterField( + model_name="project", + name="cover_image", + field=models.ImageField( + blank=True, + null=True, + storage=config.storages.PublicMediaStorage(), + upload_to=project.models.project_base.get_path_in_version_folder, + ), + ), + ] diff --git a/project/models/project_base.py b/project/models/project_base.py index 4c1064110..c6f7aad13 100644 --- a/project/models/project_base.py +++ b/project/models/project_base.py @@ -59,6 +59,10 @@ def upload_in_project_folder(project: "Project", filename: str) -> str: return f"diagnostics/{project.get_folder_name()}/{filename}" +def get_path_in_version_folder(project: "Project", filename: str) -> str: + return f"figures/{settings.OFFICIAL_VERSION}/{filename}" + + class BaseProject(models.Model): class Status(models.TextChoices): MISSING = "MISSING", "Emprise à renseigner" @@ -88,7 +92,9 @@ class Meta: class ProjectCommune(models.Model): project = models.ForeignKey("project.Project", on_delete=models.CASCADE) - commune = models.ForeignKey("public_data.Commune", on_delete=models.PROTECT, to_field="insee") + commune = models.ForeignKey( + "public_data.Commune", on_delete=models.DO_NOTHING, to_field="insee", related_name="commune" + ) group_name = models.CharField("Nom du groupe", max_length=100, blank=True, null=True) @@ -252,7 +258,7 @@ def get_public_key(self) -> str: ) cover_image = models.ImageField( - upload_to=upload_in_project_folder, + upload_to=get_path_in_version_folder, blank=True, null=True, storage=PublicMediaStorage(), diff --git a/project/tasks/project.py b/project/tasks/project.py index 5d8414f14..be64144c9 100644 --- a/project/tasks/project.py +++ b/project/tasks/project.py @@ -17,7 +17,9 @@ from matplotlib.lines import Line2D from matplotlib_scalebar.scalebar import ScaleBar +from config.storages import PublicMediaStorage from project.models import Emprise, Project, Request, RequestedDocumentChoices +from project.models.project_base import get_path_in_version_folder from public_data.domain.containers import PublicDataContainer from public_data.models import AdminRef, ArtificialArea, Land, OcsgeDiff from public_data.models.gpu import ArtifAreaZoneUrba, ZoneUrba @@ -38,6 +40,11 @@ def race_protection_save(project_id: int, fields: Dict[str, Any]) -> None: diagnostic.save_without_historical_record(update_fields=fields_name) +def check_if_file_exists_for_property(project, path: str) -> bool: + storage = PublicMediaStorage() + return storage.exists(name=path) + + def race_protection_save_map( project_id: int, field_flag_name: str, @@ -225,6 +232,21 @@ def generate_cover_image(self, project_id) -> None: logger.info("Start generate_cover_image, project_id=%d", project_id) try: diagnostic = Project.objects.get(id=int(project_id)) + filename = f"cover_{diagnostic.get_public_key()}.jpg" + field_img_name = "cover_image" + field_flag_name = "async_cover_image_done" + path = get_path_in_version_folder(diagnostic, filename) + + if check_if_file_exists_for_property( + project=diagnostic, + path=path, + ): + diagnostic.cover_image = path + diagnostic.async_cover_image_done = True + diagnostic.save(update_fields=[field_img_name, field_flag_name]) + logger.info(f"Cover image already exists, skip generation for project_id={project_id}") + return + geom = diagnostic.combined_emprise polygons = shapely.wkt.loads(geom.wkt) @@ -258,11 +280,11 @@ def generate_cover_image(self, project_id) -> None: plt.close() race_protection_save_map( - diagnostic.pk, - "async_cover_image_done", - "cover_image", - f"cover_{project_id}.jpg", - img_data, + project_id=diagnostic.pk, + field_flag_name=field_flag_name, + field_img_name=field_img_name, + img_name=filename, + img_data=img_data, ) except Project.DoesNotExist as exc: @@ -521,7 +543,7 @@ def generate_theme_map_conso(self, project_id) -> None: diagnostic.pk, "async_generate_theme_map_conso_done", "theme_map_conso", - f"theme_map_conso_{project_id}.jpg", + f"theme_map_conso_{diagnostic.get_public_key()}.jpg", img_data, ) @@ -568,7 +590,7 @@ def generate_theme_map_artif(self, project_id) -> None: diagnostic.pk, "async_generate_theme_map_artif_done", "theme_map_artif", - f"theme_map_artif_{project_id}.jpg", + f"theme_map_artif_{diagnostic.get_public_key()}.jpg", img_data, ) @@ -678,7 +700,7 @@ def generate_theme_map_understand_artif(self, project_id) -> None: diagnostic.pk, "async_theme_map_understand_artif_done", "theme_map_understand_artif", - f"theme_map_understand_artif_{project_id}.jpg", + f"theme_map_understand_artif_{diagnostic.get_public_key()}.jpg", img_data, ) @@ -736,7 +758,7 @@ def generate_theme_map_gpu(self, project_id) -> None: diagnostic.pk, "async_theme_map_gpu_done", "theme_map_gpu", - f"theme_map_gpu_{project_id}.jpg", + f"theme_map_gpu_{diagnostic.get_public_key()}.jpg", img_data, ) @@ -809,7 +831,7 @@ def generate_theme_map_fill_gpu(self, project_id) -> None: diagnostic.pk, "async_theme_map_fill_gpu_done", "theme_map_fill_gpu", - f"theme_map_fill_gpu_{project_id}.jpg", + f"theme_map_fill_gpu_{diagnostic.get_public_key()}.jpg", img_data, )