From c135dd4c66e9547b7874383435385f85095cc4ea Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Thu, 2 Jan 2025 15:04:16 +0100 Subject: [PATCH 1/2] :bug: [#4969] Add missing natural key support to map tile layer model --- bin/generate_default_map_tile_layers_fixture.sh | 7 ++++++- src/openforms/config/models/map.py | 12 ++++++++++++ src/openforms/fixtures/default_map_tile_layers.json | 2 -- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/bin/generate_default_map_tile_layers_fixture.sh b/bin/generate_default_map_tile_layers_fixture.sh index 6293f8bc60..f8552f33dc 100755 --- a/bin/generate_default_map_tile_layers_fixture.sh +++ b/bin/generate_default_map_tile_layers_fixture.sh @@ -8,4 +8,9 @@ # # Run this script from the root of the repository -src/manage.py dumpdata --indent=4 --natural-foreign --natural-primary config.MapTileLayer > src/openforms/fixtures/default_map_tile_layers.json +src/manage.py dumpdata \ + --indent=4 \ + --natural-foreign \ + --natural-primary \ + config.MapTileLayer \ + > src/openforms/fixtures/default_map_tile_layers.json diff --git a/src/openforms/config/models/map.py b/src/openforms/config/models/map.py index 1d22532fa2..f68d4056bb 100644 --- a/src/openforms/config/models/map.py +++ b/src/openforms/config/models/map.py @@ -1,7 +1,14 @@ +from __future__ import annotations + from django.db import models from django.utils.translation import gettext_lazy as _ +class MapTileLayerManager(models.Manager["MapTileLayer"]): + def get_by_natural_key(self, identifier: str) -> MapTileLayer: + return self.get(identifier=identifier) + + class MapTileLayer(models.Model): identifier = models.SlugField( _("identifier"), @@ -27,6 +34,8 @@ class MapTileLayer(models.Model): ), ) + objects = MapTileLayerManager() + class Meta: verbose_name = _("map tile layer") verbose_name_plural = _("map tile layers") @@ -34,3 +43,6 @@ class Meta: def __str__(self): return self.label + + def natural_key(self): + return (self.identifier,) diff --git a/src/openforms/fixtures/default_map_tile_layers.json b/src/openforms/fixtures/default_map_tile_layers.json index ece464ffb0..04fd146c78 100644 --- a/src/openforms/fixtures/default_map_tile_layers.json +++ b/src/openforms/fixtures/default_map_tile_layers.json @@ -1,7 +1,6 @@ [ { "model": "config.maptilelayer", - "pk": 1, "fields": { "identifier": "brt", "url": "https://service.pdok.nl/brt/achtergrondkaart/wmts/v2_0/standaard/EPSG:28992/{z}/{x}/{y}.png", @@ -10,7 +9,6 @@ }, { "model": "config.maptilelayer", - "pk": 2, "fields": { "identifier": "luchtfoto", "url": "https://service.pdok.nl/hwh/luchtfotorgb/wmts/v1_0/Actueel_orthoHR/EPSG:28992/{z}/{x}/{y}.png", From 4d9b8ea50f6fbdf84bcd5c2dc567957b2da721c5 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Thu, 2 Jan 2025 15:15:55 +0100 Subject: [PATCH 2/2] :sparkles: [#4969] Ensure map tile layers are loaded on deploy Set up a post-migrate hook to load the map tile layers fixture. --- src/openforms/config/apps.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/openforms/config/apps.py b/src/openforms/config/apps.py index ea793502ae..c8798b3ba7 100644 --- a/src/openforms/config/apps.py +++ b/src/openforms/config/apps.py @@ -1,6 +1,16 @@ +from io import StringIO + from django.apps import AppConfig +from django.core.management import call_command +from django.db.models.signals import post_migrate +from django.dispatch import receiver class OpenFormsConfigConfig(AppConfig): name = "openforms.config" verbose_name = "Configuration" + + +@receiver(post_migrate, dispatch_uid="load_default_map_tile_layers") +def update_map_tile_layers(sender, **kwargs): + call_command("loaddata", "default_map_tile_layers", verbosity=0, stdout=StringIO())