diff --git a/CHANGES b/CHANGES index 3d70ea3..fc56059 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,13 @@ Changes ------- +0.3.26 +------ +- add signal to recalculate Endpoint record if either model + GlucoseFBG or Glucose is saved. +- queue message on subject dashboard if subject reaches the protocol + glucose / DM endpoint. + 0.3.25 ------ - refactor GlucoseEndpointsByDate diff --git a/meta_analytics/dataframes/glucose_endpoints/glucose_endpoints_by_date.py b/meta_analytics/dataframes/glucose_endpoints/glucose_endpoints_by_date.py index ce6db26..b68b6e6 100644 --- a/meta_analytics/dataframes/glucose_endpoints/glucose_endpoints_by_date.py +++ b/meta_analytics/dataframes/glucose_endpoints/glucose_endpoints_by_date.py @@ -436,14 +436,14 @@ def merge_with_final_endpoints(self): self.df = self.df.sort_values(by=["subject_identifier", "fbg_datetime"]) self.df = self.df.reset_index(drop=True) - def to_model(self, model: str | None = None, subject_identifiers: list[str] | None = None): + def to_model(self): """Write endpoint_only_df to the Endpoints model""" df = self.endpoint_only_df - model = model or "meta_reports.endpoints" + model = "meta_reports.endpoints" now = get_utcnow() model_cls = django_apps.get_model(model) - if subject_identifiers: - model_cls.objects.filter(subject_identifier__in=subject_identifiers).delete() + if self.subject_identifiers: + model_cls.objects.filter(subject_identifier__in=self.subject_identifiers).delete() else: model_cls.objects.all().delete() created = 0 diff --git a/meta_dashboard/views/subject/dashboard/dashboard_view.py b/meta_dashboard/views/subject/dashboard/dashboard_view.py index f18c94a..329d5bf 100644 --- a/meta_dashboard/views/subject/dashboard/dashboard_view.py +++ b/meta_dashboard/views/subject/dashboard/dashboard_view.py @@ -1,9 +1,35 @@ +from typing import Any + +from django.core.checks import messages +from django.core.exceptions import ObjectDoesNotExist +from django.urls import reverse +from django.utils.html import format_html from django.utils.translation import gettext_lazy as _ from edc_subject_dashboard.views import SubjectDashboardView +from meta_reports.models import Endpoints, GlucoseSummary + class DashboardView(SubjectDashboardView): consent_model = "meta_consent.subjectconsentv1" navbar_selected_item = "consented_subject" visit_model = "meta_subject.subjectvisit" history_button_label = _("Audit") + + def get_context_data(self, **kwargs) -> dict[str, Any]: + context = super().get_context_data(**kwargs) + try: + Endpoints.objects.get(subject_identifier=self.subject_identifier) + except ObjectDoesNotExist: + pass + else: + url = reverse("meta_reports_admin:meta_reports_glucosesummary_changelist") + url = f"{url}?q={self.subject_identifier}" + message = _( + format_html( + f"Subject has reached the protocol endpoint. " + f'See {GlucoseSummary._meta.verbose_name}' + ) + ) + self.message_user(message, level=messages.WARNING) + return context diff --git a/meta_reports/tasks.py b/meta_reports/tasks.py index fb1c5a5..249d883 100644 --- a/meta_reports/tasks.py +++ b/meta_reports/tasks.py @@ -9,4 +9,4 @@ def update_endpoints_table(subject_identifiers: list[str] | None = None): subject_identifiers = [] cls = GlucoseEndpointsByDate(subject_identifiers=subject_identifiers) cls.run() - return cls.to_model(subject_identifiers=subject_identifiers) + return cls.to_model() diff --git a/meta_subject/models/__init__.py b/meta_subject/models/__init__.py index 8d1cd8e..246889f 100644 --- a/meta_subject/models/__init__.py +++ b/meta_subject/models/__init__.py @@ -36,6 +36,7 @@ from .sf12 import Sf12 from .signals import ( study_medication_on_pre_save, + update_glucose_endpoints_for_subject_on_post_save, update_pregnancy_notification_on_delivery_post_save, update_schedule_on_delivery_post_save, ) diff --git a/meta_subject/models/signals.py b/meta_subject/models/signals.py index b5d9867..073e147 100644 --- a/meta_subject/models/signals.py +++ b/meta_subject/models/signals.py @@ -11,6 +11,7 @@ from meta_pharmacy.constants import METFORMIN from meta_visit_schedule.constants import SCHEDULE_PREGNANCY +from . import Glucose, GlucoseFbg from .delivery import Delivery from .study_medication import StudyMedication from .subject_visit import SubjectVisit @@ -105,3 +106,21 @@ def update_schedule_on_delivery_post_save(sender, instance, raw, **kwargs): subject_identifier=instance.subject_identifier, offschedule_datetime=offschedule_datetime, ) + + +@receiver( + post_save, + weak=False, + dispatch_uid="update_glucose_endpoints_for_subject_on_post_save", +) +def update_glucose_endpoints_for_subject_on_post_save( + sender, instance, raw, update_fields, **kwargs +): + if not raw and not update_fields and sender in [Glucose, GlucoseFbg]: + from meta_analytics.dataframes import GlucoseEndpointsByDate + + cls = GlucoseEndpointsByDate( + subject_identifiers=[instance.subject_visit.subject_identifier] + ) + cls.run() + cls.to_model()