79 lines
2.9 KiB
Python
79 lines
2.9 KiB
Python
"""
|
|
Django admin configuration for referrals app.
|
|
"""
|
|
|
|
from django.contrib import admin
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.utils.html import format_html
|
|
from simple_history.admin import SimpleHistoryAdmin
|
|
|
|
from .models import Referral
|
|
|
|
|
|
@admin.register(Referral)
|
|
class ReferralAdmin(SimpleHistoryAdmin):
|
|
"""Admin interface for Referral model."""
|
|
|
|
list_display = ['patient', 'from_clinic', 'to_clinic', 'priority', 'status', 'referred_by', 'created_at']
|
|
list_filter = ['priority', 'status', 'referral_type', 'from_clinic', 'to_clinic', 'tenant', 'created_at']
|
|
search_fields = ['patient__mrn', 'patient__first_name_en', 'patient__last_name_en', 'reason']
|
|
readonly_fields = ['id', 'created_at', 'updated_at', 'acknowledged_at', 'appointment_booked_at',
|
|
'completed_at', 'declined_at', 'get_status_badge']
|
|
date_hierarchy = 'created_at'
|
|
|
|
fieldsets = (
|
|
(_('Patient & Clinics'), {
|
|
'fields': ('patient', 'tenant', 'from_clinic', 'to_clinic')
|
|
}),
|
|
(_('Referral Details'), {
|
|
'fields': ('referral_type', 'priority', 'reason', 'clinical_notes')
|
|
}),
|
|
(_('Workflow'), {
|
|
'fields': ('referred_by', 'referred_to', 'status', 'get_status_badge')
|
|
}),
|
|
(_('Acknowledgment'), {
|
|
'fields': ('acknowledged_at', 'acknowledged_by'),
|
|
'classes': ('collapse',)
|
|
}),
|
|
(_('Appointment'), {
|
|
'fields': ('appointment', 'appointment_booked_at', 'appointment_booked_by'),
|
|
'classes': ('collapse',)
|
|
}),
|
|
(_('Completion'), {
|
|
'fields': ('completed_at', 'outcome_notes'),
|
|
'classes': ('collapse',)
|
|
}),
|
|
(_('Decline/Cancel'), {
|
|
'fields': ('declined_at', 'declined_by', 'decline_reason'),
|
|
'classes': ('collapse',)
|
|
}),
|
|
(_('Metadata'), {
|
|
'fields': ('id', 'created_at', 'updated_at'),
|
|
'classes': ('collapse',)
|
|
}),
|
|
)
|
|
|
|
def get_status_badge(self, obj):
|
|
"""Display status with color badge."""
|
|
colors = {
|
|
'PENDING': '#ffc107',
|
|
'ACKNOWLEDGED': '#17a2b8',
|
|
'APPOINTMENT_BOOKED': '#28a745',
|
|
'COMPLETED': '#6c757d',
|
|
'DECLINED': '#dc3545',
|
|
'CANCELLED': '#6c757d',
|
|
}
|
|
return format_html(
|
|
'<span style="background-color: {}; color: white; padding: 5px 10px; border-radius: 3px; font-weight: bold;">{}</span>',
|
|
colors.get(obj.status, '#6c757d'),
|
|
obj.get_status_display()
|
|
)
|
|
|
|
get_status_badge.short_description = _('Status')
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
"""Set referred_by on creation."""
|
|
if not change:
|
|
obj.referred_by = request.user
|
|
super().save_model(request, obj, form, change)
|