Added locations app

Moved contacts to new model
This commit is contained in:
st01765
2026-02-26 13:34:32 +11:00
parent a24daed8f6
commit 4025c28eae
25 changed files with 576 additions and 75 deletions
+47 -15
View File
@@ -1,10 +1,10 @@
from datetime import datetime
import phonenumbers
from django.core.exceptions import ValidationError
from django.db import models
from common.models import Suburb
from locations.models import Location
class School(models.Model):
@@ -152,6 +152,20 @@ class Traveller(models.Model):
def get_families(self):
return Family.objects.filter(traveller__id__exact=self.id)
class ContactPerson(models.Model):
first_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100, blank=True)
phone = models.CharField(max_length=15, blank=True)
email = models.EmailField(blank=True)
class Meta:
indexes = [
models.Index(fields=["phone"]),
models.Index(fields=["last_name"]),
]
def __str__(self):
return f"{self.first_name} {self.last_name}"
class Family(models.Model):
RELATIONS = [
@@ -169,6 +183,9 @@ class Family(models.Model):
]
traveller = models.ForeignKey(Traveller, on_delete=models.CASCADE)
location = models.ForeignKey(Location, on_delete=models.PROTECT, blank=True, null=True)
postal_location = models.ForeignKey(Location, on_delete=models.PROTECT, null=True, blank=True,
related_name="family_postal_locations")
residential_address = models.CharField(max_length=50, blank=True)
residential_suburb = models.ForeignKey(Suburb, on_delete=models.PROTECT, blank=True, null=True,
related_name='family_residential_suburb')
@@ -191,14 +208,29 @@ class Family(models.Model):
emergency_contact_B_lastname = models.CharField(max_length=50, blank=True)
emergency_contact_B_phone = models.CharField(max_length=15, blank=True)
emergency_contact_B_relation = models.CharField(max_length=50, choices=RELATIONS, blank=True)
contact_A_relation = models.CharField(max_length=50, choices=RELATIONS, blank=True)
contact_A = models.ForeignKey(ContactPerson, on_delete=models.SET_NULL, blank=True, null=True, related_name='family_contact_A')
contact_B_relation = models.CharField(max_length=50, choices=RELATIONS, blank=True)
contact_B = models.ForeignKey(ContactPerson, on_delete=models.SET_NULL, blank=True, null=True, related_name='family_contact_B')
emergency_contact_A = models.ForeignKey(ContactPerson, on_delete=models.SET_NULL, blank=True, null=True, related_name='family_emergency_contact_A')
emergency_contact_B = models.ForeignKey(ContactPerson, on_delete=models.SET_NULL, blank=True, null=True, related_name='family_emergency_contact_B')
created_on = models.DateTimeField(auto_now_add=True, blank=True, null=True)
last_edit = models.DateTimeField(auto_now=True, blank=True, null=True)
class Meta:
verbose_name_plural = "Families"
indexes = [
models.Index(fields=["contact_A"]),
models.Index(fields=["contact_B"]),
models.Index(fields=["emergency_contact_A"]),
models.Index(fields=["emergency_contact_B"]),
]
def __str__(self):
return self.parent_names()
name = self.parent_names()
if name:
return f"{self.traveller}: {name}"
return str(self.traveller)
def clean(self):
valid_numbers, failed_numbers = self.get_parsed_numbers(True, True)
@@ -206,8 +238,8 @@ class Family(models.Model):
raise ValidationError(f"Phone number {failed_numbers[0]} not valid")
def parent_names(self):
a_name = self.parent_A_firstname
b_name = self.parent_B_firstname
a_name = self.contact_A.first_name
b_name = self.contact_B.first_name
if a_name:
if b_name:
return f"{a_name} and {b_name}"
@@ -217,24 +249,24 @@ class Family(models.Model):
return ""
def get_parsed_numbers(self, parents=False, emergency=False):
import phonenumbers
numbers = []
if parents:
if self.parent_A_phone:
numbers.append(self.parent_A_phone)
if self.parent_B_phone:
numbers.append(self.parent_B_phone)
for contact in [self.contact_A, self.contact_B]:
if contact and contact.phone:
numbers.append(contact.phone)
if emergency:
if self.emergency_contact_A_phone:
numbers.append(self.emergency_contact_A_phone)
if self.emergency_contact_B_phone:
numbers.append(self.emergency_contact_B_phone)
for contact in [self.emergency_contact_A, self.emergency_contact_B]:
if contact and contact.phone:
numbers.append(contact.phone)
valid_numbers = []
failed_numbers = []
for number in numbers:
try:
num = phonenumbers.parse(number, "AU")
if phonenumbers.is_valid_number(num):
valid_numbers.append(f"+{num.country_code}{num.national_number}")
parsed = phonenumbers.parse(number, "AU")
if phonenumbers.is_valid_number(parsed):
valid_numbers.append(f"+{parsed.country_code}{parsed.national_number}")
else:
failed_numbers.append(number)
except: