-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Add migration generation distance to source
- Loading branch information
1 parent
c2def4e
commit f585203
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
georiviere/main/migrations/0012_generate_distancetosource.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Generated by Django 3.1.14 on 2023-07-17 08:57 | ||
|
||
from django.conf import settings | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.db import migrations | ||
|
||
|
||
def generate_distancetosource(apps, schema_editor): | ||
Stream = apps.get_model('river', 'Stream') | ||
DistanceToSource = apps.get_model('main', 'DistanceToSource') | ||
from georiviere.main.signals import annotate_distance_to_source | ||
for model in apps.get_models(): | ||
field_names = [field.name for field in model._meta.get_fields()] | ||
if any(i in field_names for i in ['topology', 'geom']) and model._meta.model_name != "stream": | ||
for instance in model.objects.all(): | ||
if instance._meta.model_name in ['morphology', 'status']: | ||
stream = annotate_distance_to_source(Stream.objects.all(), instance).get( | ||
pk=instance.topology.stream_id) | ||
DistanceToSource.objects.update_or_create( | ||
object_id=instance.pk, | ||
content_type_id=ContentType.objects.get(model=instance._meta.model_name).pk, | ||
stream=stream, | ||
defaults={"distance": stream.locate.m} | ||
) | ||
else: | ||
if not instance.geom: | ||
return Stream.objects.none() | ||
geom = instance.geom | ||
area = geom.buffer(settings.BASE_INTERSECTION_MARGIN) | ||
qs = Stream.objects.all() | ||
if "geom" in [field.name for field in Stream._meta.get_fields()]: | ||
qs = qs.filter(geom__intersects=area) | ||
elif "_geom" in [field.name for field in Stream._meta.get_fields()]: | ||
qs = qs.filter(_geom__intersects=area) | ||
streams = annotate_distance_to_source(qs, instance) | ||
|
||
for stream in streams: | ||
DistanceToSource.objects.update_or_create( | ||
object_id=instance.pk, | ||
content_type_id=ContentType.objects.get_for_model( | ||
instance._meta.model).pk, | ||
stream=stream, | ||
defaults={"distance": stream.locate.m} | ||
) | ||
DistanceToSource.objects.filter(object_id=instance.pk, | ||
content_type_id=ContentType.objects.get_for_model( | ||
instance._meta.model).pk, | ||
).exclude(stream__in=streams).delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('main', '0011_auto_20230602_0901'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(generate_distancetosource, reverse_code=migrations.RunPython.noop) | ||
] |