Skip to content

Commit

Permalink
✨ Add watershed types portals
Browse files Browse the repository at this point in the history
  • Loading branch information
LePetitTim committed Jul 20, 2023
1 parent 3f41127 commit a02cfe7
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
4 changes: 2 additions & 2 deletions georiviere/portal/serializers/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)

Expand Down
16 changes: 10 additions & 6 deletions georiviere/portal/tests/test_views/test_zoning.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
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


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)

self.assertSetEqual(set(response.json().keys()), {'geometry', 'properties', 'type'})

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)

self.assertSetEqual(set(response.json().keys()), {'features', 'type'})

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)

Expand Down
2 changes: 1 addition & 1 deletion georiviere/portal/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<portal_pk>\d+)/watersheds', WatershedViewSet, basename='watersheds')
router.register('sensitivities', SensitivityViewSet, basename='sensitivities')

app_name = 'api_portal'
Expand Down
4 changes: 3 additions & 1 deletion georiviere/portal/views/zoning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
19 changes: 19 additions & 0 deletions georiviere/watershed/migrations/0002_watershedtype_portals.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
3 changes: 3 additions & 0 deletions georiviere/watershed/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit a02cfe7

Please sign in to comment.