Skip to content

Commit

Permalink
Add timepoint date, show diff against appt date
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWillitts committed Feb 9, 2024
1 parent e6620ee commit 415942b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions edc_appointment/admin/appointment_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING, Any

from django.contrib import admin
from django.db.models import DurationField, ExpressionWrapper, F
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.html import format_html
Expand Down Expand Up @@ -50,7 +51,9 @@ class AppointmentAdmin(
"appointment_subject",
"full_visit_code",
"appt_actions",
"timepoint_date",
"appointment_date",
"appt_timepoint_difference_summary",
"appointment_type",
"appt_status",
"timing",
Expand Down Expand Up @@ -173,6 +176,20 @@ def appointment_date(self, obj=None):
def appointment_type(self, obj=None):
return obj.get_appt_type_display()

@admin.display(description="Timepoint date", ordering="timepoint_datetime")
def timepoint_date(self, obj=None):
timepoint_date = obj.timepoint_datetime.date()
weekday = calendar.day_abbr[obj.timepoint_datetime.weekday()]
return f"{timepoint_date} {weekday}"

@admin.display(description="Difference", ordering="appt_timepoint_difference")
def appt_timepoint_difference_summary(self, obj=None):
if obj.appt_datetime.time() >= obj.timepoint_datetime.time():
difference_in_days = obj.appt_timepoint_difference.days
else:
difference_in_days = obj.appt_timepoint_difference.days + 1
return f"{'+' if difference_in_days > 0 else ''}{difference_in_days} days"

@admin.display(description="Subject", ordering="subject_identifier")
def appointment_subject(self, obj=None):
return obj.subject_identifier
Expand Down Expand Up @@ -238,3 +255,15 @@ def allow_skipped_appointments(self, request) -> bool:
Relates to use of `SKIPPED_APPT` feature.
"""
return True if get_allow_skipped_appt_using() else False

def get_queryset(self, request):
return (
super()
.get_queryset(request)
.annotate(
appt_timepoint_difference=ExpressionWrapper(
(F("appt_datetime") - F("timepoint_datetime")),
output_field=DurationField(),
)
)
)

0 comments on commit 415942b

Please sign in to comment.