diff --git a/konova/models/geometry.py b/konova/models/geometry.py index 82a51dbc..52cd829d 100644 --- a/konova/models/geometry.py +++ b/konova/models/geometry.py @@ -10,6 +10,7 @@ import json from django.contrib.gis.db.models import MultiPolygonField from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned from django.db import models, transaction +from django.db.models import Q from django.utils import timezone from django.contrib.gis.geos import MultiPolygon @@ -109,17 +110,26 @@ class Geometry(BaseResource): objs (list): The list of objects """ objs = [] - sets = [ + + # Some related data sets can be processed rather easily + regular_sets = [ self.intervention_set, - self.compensation_set, self.ema_set, self.ecoaccount_set, ] - for _set in sets: + for _set in regular_sets: set_objs = _set.filter( deleted=None ) objs += set_objs + + # ... but we need a special treatment for compensations, since they can be deleted directly OR inherit their + # de-facto-deleted status from their deleted parent intervention + comp_objs = self.compensation_set.filter( + Q(deleted=None) & Q(intervention__deleted=None) + ) + objs += comp_objs + return objs def get_data_object(self): diff --git a/konova/models/object.py b/konova/models/object.py index f1a41c97..573dbf8c 100644 --- a/konova/models/object.py +++ b/konova/models/object.py @@ -677,12 +677,12 @@ class GeoReferencedMixin(models.Model): return request instance_objs = [] - conflicts = self.geometry.conflicts_geometries.all() + conflicts = self.geometry.conflicts_geometries.iterator() for conflict in conflicts: instance_objs += conflict.affected_geometry.get_data_objects() - conflicts = self.geometry.conflicted_by_geometries.all() + conflicts = self.geometry.conflicted_by_geometries.iterator() for conflict in conflicts: instance_objs += conflict.conflicting_geometry.get_data_objects()