Skip to content

Commit

Permalink
add shared contribution_at field
Browse files Browse the repository at this point in the history
  • Loading branch information
submarcos committed Apr 25, 2024
1 parent 7897bf1 commit 710526e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
8 changes: 5 additions & 3 deletions georiviere/portal/serializers/contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ class Meta:


class CustomContributionSerializer(serializers.ModelSerializer):
contributed_at = serializers.DateTimeField(required=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
custom_type = self.context.get("custom_type")
Expand Down Expand Up @@ -228,15 +230,15 @@ def create(self, validated_data):

class Meta:
model = CustomContribution
exclude = ("data", "custom_type")
exclude = ("data", "custom_type", "validated")


class CustomContributionSerializerGeoJSONSerializer(
geo_serializers.GeoFeatureModelSerializer,
CustomContributionSerializer,
geo_serializers.GeoFeatureModelSerializer,
):
geometry = geo_serializers.GeometryField(read_only=True, precision=7)

class Meta(CustomContributionSerializer.Meta):
geo_field = "geometry"
exclude = ("geom", "data", "custom_type")
exclude = ("geom", "data", "custom_type", "validated")
66 changes: 49 additions & 17 deletions georiviere/portal/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,70 @@
from georiviere.valorization.models import POICategory


@receiver(post_delete, sender=POICategory, dispatch_uid='delete_category_maplayer')
@receiver(post_delete, sender=POICategory, dispatch_uid="delete_category_maplayer")
def delete_category_maplayer(sender, instance, **kwargs):
# Remove every map layer (all portals) when a category is deleted
MapLayer.objects.filter(layer_type__startswith='pois',
layer_type__endswith=instance.pk).delete()
MapLayer.objects.filter(
layer_type__startswith="pois", layer_type__endswith=instance.pk
).delete()


@receiver(post_save, sender=POICategory, dispatch_uid='save_category')
@receiver(post_save, sender=POICategory, dispatch_uid="save_category")
def save_category_maplayer(sender, instance, created, **kwargs):
if created:
for portal in Portal.objects.all():
# Create a map layer for each portal for this POICategory
MapLayer.objects.create(label=instance.label, order=0, layer_type=f'pois-{instance.pk}',
portal=portal)
MapLayer.objects.create(
label=instance.label,
order=0,
layer_type=f"pois-{instance.pk}",
portal=portal,
)


@receiver(post_save, sender=Portal, dispatch_uid='save_portal')
@receiver(post_save, sender=Portal, dispatch_uid="save_portal")
def save_portal(sender, instance, created, **kwargs):
if created:
# Generate a default base layer
MapBaseLayer.objects.create(label='OSM', order=0, url='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
attribution='© Contributeurs OpenStreetMap', portal=instance)
MapBaseLayer.objects.create(
label="OSM",
order=0,
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
attribution="© Contributeurs OpenStreetMap",
portal=instance,
)
# Generate all layers
MapLayer.objects.create(label=_('Watershed'), order=0, layer_type='watersheds', portal=instance)
MapLayer.objects.create(label=_('City'), order=0, layer_type='cities', portal=instance)
MapLayer.objects.create(label=_('Sensitivity'), order=0, layer_type='sensitivities', portal=instance)
MapLayer.objects.create(label=_('District'), order=0, layer_type='districts', portal=instance)
MapLayer.objects.create(label=_('Contribution'), order=0, layer_type='contributions', portal=instance)
MapLayer.objects.create(
label=_("Watershed"), order=0, layer_type="watersheds", portal=instance
)
MapLayer.objects.create(
label=_("City"), order=0, layer_type="cities", portal=instance
)
MapLayer.objects.create(
label=_("Sensitivity"), order=0, layer_type="sensitivities", portal=instance
)
MapLayer.objects.create(
label=_("District"), order=0, layer_type="districts", portal=instance
)
MapLayer.objects.create(
label=_("Contribution"),
order=0,
layer_type="contributions",
portal=instance,
)
# We generate a map layer for each category of poi with the layer type separated by a -
# We use it after in the serializer / view to generate a geojson url for each of them
for category in POICategory.objects.all():
MapLayer.objects.create(label=f'{category.label}', order=0, layer_type=f'pois-{category.pk}',
portal=instance)
MapLayer.objects.create(
label=f"{category.label}",
order=0,
layer_type=f"pois-{category.pk}",
portal=instance,
)

MapLayer.objects.create(label=_('Stream'), order=0, layer_type='streams', portal=instance)
MapLayer.objects.create(
label=_("Stream"), order=0, layer_type="streams", portal=instance
)
MapLayer.objects.create(
label=_("Station"), order=0, layer_type="stations", portal=instance
)
3 changes: 1 addition & 2 deletions georiviere/portal/views/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ class StationViewSet(GeoriviereAPIMixin, viewsets.ReadOnlyModelViewSet):
geojson_serializer_class = StationGeojsonSerializer

def get_queryset(self):
# portal_pk = self.kwargs["portal_pk"]
# Station.objects.filter(custom_contribution_types__portal__id=portal_pk)
Station.objects.filter(custom_contribution_types__isnull=False).distinct('pk') # filter station linked to custom contrib
return Station.objects.all().annotate(geom_transformed=Transform(F("geom"), settings.API_SRID))
12 changes: 9 additions & 3 deletions georiviere/river/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ def test_update_stream_move_topologies(self):

self.assertEqual(str(stream), stream.name)

morphologies = Morphology.objects.values_list('geom', flat=True)
status = Status.objects.values_list('geom', flat=True)
stream_1_morpho_geom = stream.morphologies.first().geom.ewkt
stream_2_morpho_geom = stream_2.morphologies.first().geom.ewkt

self.assertEqual(morphologies[0], morphologies[1], f"{morphologies[0].ewkt} - {morphologies[1].ewkt}", )
status = Status.objects.values_list("geom", flat=True)

self.assertEqual(
stream_1_morpho_geom,
stream_2_morpho_geom,
f"{stream_1_morpho_geom} - {stream_2_morpho_geom}",
)
self.assertEqual(status[0], status[1])

0 comments on commit 710526e

Please sign in to comment.