Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimisation : génération unique des fichiers statiques #749

Draft
wants to merge 1 commit into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions project/migrations/0102_alter_project_cover_image_and_more.py
Original file line number Diff line number Diff line change
@@ -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,
),
),
]
10 changes: 8 additions & 2 deletions project/models/project_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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(),
Expand Down
42 changes: 32 additions & 10 deletions project/tasks/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand Down