Added locations app
Moved contacts to new model
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
from django.contrib import admin
|
||||
from django.db import models
|
||||
from django.urls import reverse
|
||||
from django.utils.html import format_html
|
||||
from rangefilter.filters import DateRangeFilterBuilder
|
||||
|
||||
from common.admin import MyImportExportModelAdmin
|
||||
from traveller.adminClone import CloneModelAdmin
|
||||
from traveller.admin_mixins import SchoolRollMixin, TravellerRollMixin
|
||||
from traveller.models import Family, TravellerRoute, Traveller, School
|
||||
from traveller.models import Family, TravellerRoute, Traveller, School, ContactPerson
|
||||
|
||||
|
||||
class FamilyInline(admin.StackedInline):
|
||||
@@ -13,6 +15,70 @@ class FamilyInline(admin.StackedInline):
|
||||
classes = ['collapse']
|
||||
extra = 0
|
||||
clone_parent = "traveller"
|
||||
autocomplete_fields = [
|
||||
"location",
|
||||
"postal_location",
|
||||
"contact_A",
|
||||
"contact_B",
|
||||
"emergency_contact_A",
|
||||
"emergency_contact_B",
|
||||
]
|
||||
|
||||
readonly_fields = [
|
||||
"contact_A_phone_display",
|
||||
"contact_B_phone_display",
|
||||
"emergency_contact_A_phone_display",
|
||||
"emergency_contact_B_phone_display",
|
||||
]
|
||||
|
||||
fieldsets = (
|
||||
("Residential Address", {
|
||||
"fields": ("location",)
|
||||
}),
|
||||
|
||||
("Postal Address", {
|
||||
"fields": ("postal_location",)
|
||||
}),
|
||||
|
||||
("Parents / Guardians", {
|
||||
"fields": (
|
||||
"contact_A_relation",
|
||||
"contact_A",
|
||||
"contact_A_phone_display",
|
||||
"contact_B_relation",
|
||||
"contact_B",
|
||||
"contact_B_phone_display",
|
||||
)
|
||||
}),
|
||||
|
||||
("Emergency Contacts", {
|
||||
"fields": (
|
||||
"emergency_contact_A_relation",
|
||||
"emergency_contact_A",
|
||||
"emergency_contact_A_phone_display",
|
||||
"emergency_contact_B_relation",
|
||||
"emergency_contact_B",
|
||||
"emergency_contact_B_phone_display",
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
def contact_A_phone_display(self, obj):
|
||||
return obj.contact_A.phone if obj.contact_A else "-"
|
||||
|
||||
def contact_B_phone_display(self, obj):
|
||||
return obj.contact_B.phone if obj.contact_B else "-"
|
||||
|
||||
def emergency_contact_A_phone_display(self, obj):
|
||||
return obj.emergency_contact_A.phone if obj.emergency_contact_A else "-"
|
||||
|
||||
def emergency_contact_B_phone_display(self, obj):
|
||||
return obj.emergency_contact_B.phone if obj.emergency_contact_B else "-"
|
||||
|
||||
contact_A_phone_display.short_description = "Phone"
|
||||
contact_B_phone_display.short_description = "Phone"
|
||||
emergency_contact_A_phone_display.short_description = "Phone"
|
||||
emergency_contact_B_phone_display.short_description = "Phone"
|
||||
|
||||
|
||||
class TravellerRouteInline(admin.TabularInline):
|
||||
@@ -87,6 +153,85 @@ class TravellerAdmin(MyImportExportModelAdmin, CloneModelAdmin, TravellerRollMix
|
||||
# obj.is_archived = False
|
||||
# super().save_model(request, obj, form, change)
|
||||
|
||||
@admin.register(ContactPerson)
|
||||
class ContactPersonAdmin(MyImportExportModelAdmin):
|
||||
list_display = [
|
||||
"first_name",
|
||||
"last_name",
|
||||
"phone",
|
||||
"email"
|
||||
]
|
||||
|
||||
search_fields = [
|
||||
"first_name",
|
||||
"last_name",
|
||||
"phone",
|
||||
"email",
|
||||
]
|
||||
|
||||
readonly_fields = [
|
||||
"traveller_relationship_summary"
|
||||
]
|
||||
|
||||
fieldsets = (
|
||||
(None, {
|
||||
"fields": ("first_name", "last_name", "phone", "email")
|
||||
}),
|
||||
|
||||
("Relationships", {
|
||||
"fields": ("traveller_relationship_summary",)
|
||||
}),
|
||||
)
|
||||
|
||||
def traveller_relationship_summary(self, obj):
|
||||
from django.utils.html import format_html
|
||||
|
||||
families = Family.objects.filter(
|
||||
models.Q(contact_A=obj) |
|
||||
models.Q(contact_B=obj) |
|
||||
models.Q(emergency_contact_A=obj) |
|
||||
models.Q(emergency_contact_B=obj)
|
||||
).select_related("traveller", "location")
|
||||
|
||||
if not families.exists():
|
||||
return "No traveller relationships"
|
||||
|
||||
output = []
|
||||
|
||||
for family in families:
|
||||
traveller = family.traveller
|
||||
|
||||
relation = ""
|
||||
if family.contact_A == obj:
|
||||
relation = family.contact_A_relation
|
||||
elif family.contact_B == obj:
|
||||
relation = family.contact_B_relation
|
||||
elif family.emergency_contact_A == obj:
|
||||
relation = family.emergency_contact_A_relation
|
||||
elif family.emergency_contact_B == obj:
|
||||
relation = family.emergency_contact_B_relation
|
||||
|
||||
address = family.location if family.location else "No address"
|
||||
|
||||
url = reverse(
|
||||
"admin:traveller_traveller_change",
|
||||
args=[traveller.id]
|
||||
)
|
||||
|
||||
output.append(
|
||||
format_html(
|
||||
'<a href="{}">{}</a> ({})<br>'
|
||||
'<small style="color:#888">{}</small>',
|
||||
url,
|
||||
f"{traveller.first_name} {traveller.last_name}",
|
||||
relation,
|
||||
address
|
||||
)
|
||||
)
|
||||
|
||||
return format_html("".join(output))
|
||||
|
||||
traveller_relationship_summary.short_description = "Traveller Relationships"
|
||||
|
||||
# @admin.register(Family)
|
||||
class FamilyAdmin(MyImportExportModelAdmin, admin.ModelAdmin):
|
||||
|
||||
Reference in New Issue
Block a user