Added signal for traveller updates
Added contact emails to family inline
This commit is contained in:
@@ -26,9 +26,13 @@ class FamilyInline(admin.StackedInline):
|
|||||||
|
|
||||||
readonly_fields = [
|
readonly_fields = [
|
||||||
"contact_A_phone_display",
|
"contact_A_phone_display",
|
||||||
|
"contact_A_email_display",
|
||||||
"contact_B_phone_display",
|
"contact_B_phone_display",
|
||||||
|
"contact_B_email_display",
|
||||||
"emergency_contact_A_phone_display",
|
"emergency_contact_A_phone_display",
|
||||||
|
"emergency_contact_A_email_display",
|
||||||
"emergency_contact_B_phone_display",
|
"emergency_contact_B_phone_display",
|
||||||
|
"emergency_contact_B_email_display",
|
||||||
]
|
]
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
@@ -45,9 +49,11 @@ class FamilyInline(admin.StackedInline):
|
|||||||
"contact_A_relation",
|
"contact_A_relation",
|
||||||
"contact_A",
|
"contact_A",
|
||||||
"contact_A_phone_display",
|
"contact_A_phone_display",
|
||||||
|
"contact_A_email_display",
|
||||||
"contact_B_relation",
|
"contact_B_relation",
|
||||||
"contact_B",
|
"contact_B",
|
||||||
"contact_B_phone_display",
|
"contact_B_phone_display",
|
||||||
|
"contact_B_email_display",
|
||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -56,9 +62,11 @@ class FamilyInline(admin.StackedInline):
|
|||||||
"emergency_contact_A_relation",
|
"emergency_contact_A_relation",
|
||||||
"emergency_contact_A",
|
"emergency_contact_A",
|
||||||
"emergency_contact_A_phone_display",
|
"emergency_contact_A_phone_display",
|
||||||
|
"emergency_contact_A_email_display",
|
||||||
"emergency_contact_B_relation",
|
"emergency_contact_B_relation",
|
||||||
"emergency_contact_B",
|
"emergency_contact_B",
|
||||||
"emergency_contact_B_phone_display",
|
"emergency_contact_B_phone_display",
|
||||||
|
"emergency_contact_B_email_display",
|
||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
@@ -66,19 +74,35 @@ class FamilyInline(admin.StackedInline):
|
|||||||
def contact_A_phone_display(self, obj):
|
def contact_A_phone_display(self, obj):
|
||||||
return obj.contact_A.phone if obj.contact_A else "-"
|
return obj.contact_A.phone if obj.contact_A else "-"
|
||||||
|
|
||||||
|
def contact_A_email_display(self, obj):
|
||||||
|
return obj.contact_A.email if obj.contact_A else "-"
|
||||||
|
|
||||||
def contact_B_phone_display(self, obj):
|
def contact_B_phone_display(self, obj):
|
||||||
return obj.contact_B.phone if obj.contact_B else "-"
|
return obj.contact_B.phone if obj.contact_B else "-"
|
||||||
|
|
||||||
|
def contact_B_email_display(self, obj):
|
||||||
|
return obj.contact_B.email if obj.contact_B else "-"
|
||||||
|
|
||||||
def emergency_contact_A_phone_display(self, obj):
|
def emergency_contact_A_phone_display(self, obj):
|
||||||
return obj.emergency_contact_A.phone if obj.emergency_contact_A else "-"
|
return obj.emergency_contact_A.phone if obj.emergency_contact_A else "-"
|
||||||
|
|
||||||
|
def emergency_contact_A_email_display(self, obj):
|
||||||
|
return obj.emergency_contact_A.email if obj.emergency_contact_A else "-"
|
||||||
|
|
||||||
def emergency_contact_B_phone_display(self, obj):
|
def emergency_contact_B_phone_display(self, obj):
|
||||||
return obj.emergency_contact_B.phone if obj.emergency_contact_B else "-"
|
return obj.emergency_contact_B.phone if obj.emergency_contact_B else "-"
|
||||||
|
|
||||||
|
def emergency_contact_B_email_display(self, obj):
|
||||||
|
return obj.emergency_contact_B.email if obj.emergency_contact_B else "-"
|
||||||
|
|
||||||
contact_A_phone_display.short_description = "Phone"
|
contact_A_phone_display.short_description = "Phone"
|
||||||
|
contact_A_email_display.short_description = "Email"
|
||||||
contact_B_phone_display.short_description = "Phone"
|
contact_B_phone_display.short_description = "Phone"
|
||||||
|
contact_B_email_display.short_description = "Email"
|
||||||
emergency_contact_A_phone_display.short_description = "Phone"
|
emergency_contact_A_phone_display.short_description = "Phone"
|
||||||
|
emergency_contact_A_email_display.short_description = "Email"
|
||||||
emergency_contact_B_phone_display.short_description = "Phone"
|
emergency_contact_B_phone_display.short_description = "Phone"
|
||||||
|
emergency_contact_B_email_display.short_description = "Email"
|
||||||
|
|
||||||
|
|
||||||
class TravellerRouteInline(admin.TabularInline):
|
class TravellerRouteInline(admin.TabularInline):
|
||||||
|
|||||||
@@ -4,3 +4,6 @@ from django.apps import AppConfig
|
|||||||
class TravellerConfig(AppConfig):
|
class TravellerConfig(AppConfig):
|
||||||
default_auto_field = 'django.db.models.BigAutoField'
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
name = 'traveller'
|
name = 'traveller'
|
||||||
|
|
||||||
|
def ready(self):
|
||||||
|
import traveller.signals
|
||||||
@@ -86,11 +86,6 @@ class Traveller(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.first_name} {self.last_name}"
|
return f"{self.first_name} {self.last_name}"
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
|
||||||
self._update_active_status()
|
|
||||||
self._repopulate_address()
|
|
||||||
super(Traveller, self).save(*args, **kwargs)
|
|
||||||
|
|
||||||
def _is_active(self, date=None):
|
def _is_active(self, date=None):
|
||||||
if date is None:
|
if date is None:
|
||||||
today = datetime.today()
|
today = datetime.today()
|
||||||
@@ -104,22 +99,23 @@ class Traveller(models.Model):
|
|||||||
end_date = datetime(self.travel_end_date.year, self.travel_end_date.month, self.travel_end_date.day)
|
end_date = datetime(self.travel_end_date.year, self.travel_end_date.month, self.travel_end_date.day)
|
||||||
return end_date >= date
|
return end_date >= date
|
||||||
|
|
||||||
def _update_active_status(self):
|
def recalculate_travel_dates(self):
|
||||||
new_start_date = None
|
routes = TravellerRoute.objects.filter(traveller=self)
|
||||||
new_end_date = None
|
|
||||||
|
|
||||||
for travellerRoute in TravellerRoute.objects.filter(traveller=self.id):
|
new_start = None
|
||||||
route_start = travellerRoute.travel_start_date
|
new_end = None
|
||||||
route_end = travellerRoute.travel_end_date
|
|
||||||
if route_start is not None:
|
|
||||||
if new_start_date is None or new_start_date > route_start:
|
|
||||||
new_start_date = route_start
|
|
||||||
if route_end is not None:
|
|
||||||
if new_end_date is None or new_end_date < route_end:
|
|
||||||
new_end_date = route_end
|
|
||||||
|
|
||||||
self.travel_start_date = new_start_date
|
for route in routes:
|
||||||
self.travel_end_date = new_end_date
|
if route.travel_start_date:
|
||||||
|
if new_start is None or route.travel_start_date < new_start:
|
||||||
|
new_start = route.travel_start_date
|
||||||
|
|
||||||
|
if route.travel_end_date:
|
||||||
|
if new_end is None or route.travel_end_date > new_end:
|
||||||
|
new_end = route.travel_end_date
|
||||||
|
|
||||||
|
self.travel_start_date = new_start
|
||||||
|
self.travel_end_date = new_end
|
||||||
self.is_active = self._is_active()
|
self.is_active = self._is_active()
|
||||||
|
|
||||||
def fare_paying(self):
|
def fare_paying(self):
|
||||||
@@ -139,15 +135,14 @@ class Traveller(models.Model):
|
|||||||
stops = 1
|
stops = 1
|
||||||
return f"${round(cost*stops)}"
|
return f"${round(cost*stops)}"
|
||||||
|
|
||||||
def _repopulate_address(self):
|
def update_address_from_families(self):
|
||||||
families = self.get_families()
|
addresses = {
|
||||||
if families.count() == 0:
|
str(f.location)
|
||||||
self.address = ""
|
for f in self.get_families()
|
||||||
elif families.count() == 1:
|
if f.location
|
||||||
family = families.first()
|
}
|
||||||
self.address = f"{family.residential_address} {family.residential_suburb}"
|
|
||||||
else:
|
self.address = " | ".join(sorted(addresses)) if addresses else ""
|
||||||
self.address = "Multiple"
|
|
||||||
|
|
||||||
def get_families(self):
|
def get_families(self):
|
||||||
return Family.objects.filter(traveller__id__exact=self.id)
|
return Family.objects.filter(traveller__id__exact=self.id)
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
from django.db.models.signals import post_save, post_delete
|
||||||
|
from django.dispatch import receiver
|
||||||
|
|
||||||
|
from traveller.models import TravellerRoute, Family
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(post_save, sender=TravellerRoute)
|
||||||
|
@receiver(post_delete, sender=TravellerRoute)
|
||||||
|
def update_traveller_dates(sender, instance, **kwargs):
|
||||||
|
traveller = instance.traveller
|
||||||
|
traveller.recalculate_travel_dates()
|
||||||
|
traveller.save(update_fields=[
|
||||||
|
"travel_start_date",
|
||||||
|
"travel_end_date",
|
||||||
|
"is_active",
|
||||||
|
])
|
||||||
|
|
||||||
|
@receiver(post_save, sender=Family)
|
||||||
|
@receiver(post_delete, sender=Family)
|
||||||
|
def update_traveller_address(sender, instance, **kwargs):
|
||||||
|
traveller = instance.traveller
|
||||||
|
traveller.update_address_from_families()
|
||||||
|
traveller.save(update_fields=["address"])
|
||||||
Reference in New Issue
Block a user