From a02cfe78c856640dfee7ae67abe99a80362395d2 Mon Sep 17 00:00:00 2001 From: LePetitTim Date: Mon, 15 May 2023 23:02:11 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20watershed=20types=20portals?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/changelog.rst | 1 + georiviere/portal/serializers/map.py | 4 ++-- .../portal/tests/test_views/test_zoning.py | 16 ++++++++++------ georiviere/portal/urls.py | 2 +- georiviere/portal/views/zoning.py | 4 +++- .../migrations/0002_watershedtype_portals.py | 19 +++++++++++++++++++ georiviere/watershed/models.py | 3 +++ 7 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 georiviere/watershed/migrations/0002_watershedtype_portals.py diff --git a/docs/changelog.rst b/docs/changelog.rst index bf97639d..2a3f14fb 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -12,6 +12,7 @@ CHANGELOG * Add flatpages module * Add valorization POIs * Add sensitivity module +* Add public portals on watershed types allowing to publish them **Bug fixes** diff --git a/georiviere/portal/serializers/map.py b/georiviere/portal/serializers/map.py index 8cb63488..f6364d31 100644 --- a/georiviere/portal/serializers/map.py +++ b/georiviere/portal/serializers/map.py @@ -44,7 +44,7 @@ def get_geojson_url(self, obj): layer_type = obj.layer_type.split('-') # TODO: Make lang dynamic reverse_kwargs = {'lang': 'fr', 'format': 'geojson'} - if layer_type[0] in ['pois', 'streams', 'contributions']: + if layer_type[0] in ['waterhseds', 'pois', 'streams', 'contributions']: reverse_kwargs['portal_pk'] = obj.portal.pk if len(layer_type) == 2: filter_type = layer_type[-1] @@ -58,7 +58,7 @@ def get_url(self, obj): return None # TODO: Make lang dynamic reverse_kwargs = {'lang': 'fr'} - if layer_type[0] in ['pois', 'streams', 'contributions']: + if layer_type[0] in ['waterhseds', 'pois', 'streams', 'contributions']: reverse_kwargs['portal_pk'] = obj.portal.pk return reverse(f'api_portal:{layer_type[0]}-list', kwargs=reverse_kwargs) diff --git a/georiviere/portal/tests/test_views/test_zoning.py b/georiviere/portal/tests/test_views/test_zoning.py index be6942c3..d0f9041b 100644 --- a/georiviere/portal/tests/test_views/test_zoning.py +++ b/georiviere/portal/tests/test_views/test_zoning.py @@ -1,7 +1,8 @@ from django.test import TestCase from django.urls import reverse -from georiviere.watershed.tests.factories import WatershedFactory +from georiviere.portal.tests.factories import PortalFactory +from georiviere.watershed.tests.factories import WatershedFactory, WatershedTypeFactory from geotrek.zoning.tests.factories import CityFactory, DistrictFactory @@ -9,11 +10,14 @@ class WatershedViewTest(TestCase): @classmethod def setUpTestData(cls): - cls.watershed = WatershedFactory.create() + cls.portal = PortalFactory.create() + watershed_type = WatershedTypeFactory.create() + watershed_type.portals.add(cls.portal) + cls.watershed = WatershedFactory.create(watershed_type=watershed_type) def test_watershed_detail_geojson_structure(self): url = reverse('api_portal:watersheds-detail', - kwargs={'pk': self.watershed.pk, 'lang': 'fr', 'format': 'geojson'}) + kwargs={'portal_pk': self.portal.pk, 'pk': self.watershed.pk, 'lang': 'fr', 'format': 'geojson'}) response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -21,7 +25,7 @@ def test_watershed_detail_geojson_structure(self): def test_watershed_list_geojson_structure(self): url = reverse('api_portal:watersheds-list', - kwargs={'lang': 'fr', 'format': 'geojson'}) + kwargs={'portal_pk': self.portal.pk, 'lang': 'fr', 'format': 'geojson'}) response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -29,13 +33,13 @@ def test_watershed_list_geojson_structure(self): def test_watershed_detail_json(self): url = reverse('api_portal:watersheds-detail', - kwargs={'pk': self.watershed.pk, 'lang': 'fr', 'format': 'json'}) + kwargs={'portal_pk': self.portal.pk, 'pk': self.watershed.pk, 'lang': 'fr', 'format': 'json'}) response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_watershed_list_json(self): url = reverse('api_portal:watersheds-list', - kwargs={'lang': 'fr', 'format': 'json'}) + kwargs={'portal_pk': self.portal.pk, 'lang': 'fr', 'format': 'json'}) response = self.client.get(url) self.assertEqual(response.status_code, 404) diff --git a/georiviere/portal/urls.py b/georiviere/portal/urls.py index a0cee3a6..22d66db1 100644 --- a/georiviere/portal/urls.py +++ b/georiviere/portal/urls.py @@ -24,7 +24,7 @@ router.register('portal', PortalViewSet, basename='portal') router.register('cities', CityViewSet, basename='cities') router.register('districts', DistrictViewSet, basename='districts') -router.register('watersheds', WatershedViewSet, basename='watersheds') +router.register(r'(?P\d+)/watersheds', WatershedViewSet, basename='watersheds') router.register('sensitivities', SensitivityViewSet, basename='sensitivities') app_name = 'api_portal' diff --git a/georiviere/portal/views/zoning.py b/georiviere/portal/views/zoning.py index 37ca2e75..3d8e8947 100644 --- a/georiviere/portal/views/zoning.py +++ b/georiviere/portal/views/zoning.py @@ -35,4 +35,6 @@ class WatershedViewSet(viewsets.ReadOnlyModelViewSet): renderer_classes = [GeoJSONRenderer, ] def get_queryset(self): - return Watershed.objects.annotate(geom_transformed=Transform(F('geom'), settings.API_SRID)) + portal_pk = self.kwargs['portal_pk'] + queryset = Watershed.objects.select_related('watershed_type').filter(watershed_type__portals__id=portal_pk) + return queryset.annotate(geom_transformed=Transform(F('geom'), settings.API_SRID)) diff --git a/georiviere/watershed/migrations/0002_watershedtype_portals.py b/georiviere/watershed/migrations/0002_watershedtype_portals.py new file mode 100644 index 00000000..5954083c --- /dev/null +++ b/georiviere/watershed/migrations/0002_watershedtype_portals.py @@ -0,0 +1,19 @@ +# Generated by Django 3.1.14 on 2023-05-15 21:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('portal', '0003_auto_20230515_2019'), + ('watershed', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='watershedtype', + name='portals', + field=models.ManyToManyField(blank=True, related_name='watersheds', to='portal.Portal', verbose_name='Published portals'), + ), + ] diff --git a/georiviere/watershed/models.py b/georiviere/watershed/models.py index 65dbfe81..d238af3a 100644 --- a/georiviere/watershed/models.py +++ b/georiviere/watershed/models.py @@ -9,6 +9,9 @@ class WatershedType(models.Model): name = models.CharField(max_length=200, verbose_name=_("Name")) color = ColorField(verbose_name=_("Color"), default='#444444', help_text=_("Color shown on map")) + portals = models.ManyToManyField('portal.Portal', + blank=True, related_name='watersheds', + verbose_name=_("Published portals")) class Meta: verbose_name = _("Watershed type")