Skip to content

Commit

Permalink
updates save and update methods in Organisation
Browse files Browse the repository at this point in the history
  • Loading branch information
eatyourpeas committed Aug 30, 2024
1 parent 09b558d commit 692f43a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions rcpch_nhs_organisations/hospitals/models/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
FloatField,
BooleanField,
)
from django.contrib.gis.geos import Point
from django.db.models.signals import pre_save
from django.dispatch import receiver

# 3rd party
from .time_and_user_abstract_base_classes import TimeStampAbstractBaseClass
Expand Down Expand Up @@ -130,3 +133,25 @@ class Meta:

def __str__(self) -> str:
return self.name

def save(self, *args, **kwargs):
if self.latitude and self.longitude and not self.geocode_coordinates:
self.geocode_coordinates = Point(
x=self.longitude, y=self.latitude, srid=4326
)
super(Organisation, self).save(*args, **kwargs)


# This ensures that the geocode coordinates are set before saving, even if update is called
@receiver(pre_save, sender=Organisation)
def set_geocode_coordinates(sender, instance, **kwargs):
from django.contrib.gis.geos import Point

if instance.latitude and instance.longitude and not instance.geocode_coordinates:
instance.geocode_coordinates = Point(
x=instance.longitude, y=instance.latitude, srid=4326
)


# Ensure the signal is connected
pre_save.connect(set_geocode_coordinates, sender=Organisation)

0 comments on commit 692f43a

Please sign in to comment.