Skip to content

Commit

Permalink
Merge branch 'release/0.4.19' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Feb 15, 2024
2 parents 289807c + d058d5e commit 6ae8c0d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- "-x *test*.py"

- repo: https://github.com/psf/black
rev: 24.1.1
rev: 24.2.0
hooks:
- id: black
language_version: python3.11
Expand Down Expand Up @@ -42,7 +42,7 @@ repos:
- id: detect-private-key

- repo: https://github.com/adrienverge/yamllint
rev: v1.33.0
rev: v1.34.0
hooks:
- id: yamllint
args:
Expand Down
30 changes: 30 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 @@ -51,6 +52,7 @@ class AppointmentAdmin(
"full_visit_code",
"appt_actions",
"appointment_date",
"days_from_timepoint_datetime",
"appointment_type",
"appt_status",
"timing",
Expand Down Expand Up @@ -173,6 +175,22 @@ 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="Timepoint", ordering="appt_timepoint_delta")
def days_from_timepoint_datetime(self, obj=None):
if obj.appt_datetime.time() >= obj.timepoint_datetime.time():
days = obj.appt_timepoint_delta.days
else:
days = obj.appt_timepoint_delta.days + 1
if days == 0:
return None
return f"{'+' if days > 0 else ''}{days}d"

@admin.display(description="Subject", ordering="subject_identifier")
def appointment_subject(self, obj=None):
return obj.subject_identifier
Expand Down Expand Up @@ -238,3 +256,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_delta=ExpressionWrapper(
(F("appt_datetime") - F("timepoint_datetime")),
output_field=DurationField(),
)
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.conf import settings
from django.utils.translation import gettext as _
from edc_utils import convert_php_dateformat, formatted_date, to_utc
from edc_utils.date import ceil_datetime, floor_secs
from edc_utils.date import ceil_datetime, floor_secs, to_local
from edc_visit_schedule.exceptions import (
ScheduledVisitWindowError,
UnScheduledVisitWindowError,
Expand Down Expand Up @@ -89,11 +89,15 @@ def datetime_in_window_or_raise(
UNSCHEDULED_WINDOW_ERROR,
)
except ScheduledVisitWindowError:
lower = floor_secs(get_lower_datetime(appointment)).strftime(datetimestring)
upper = floor_secs(ceil_datetime(get_upper_datetime(appointment))).strftime(
lower = floor_secs(to_local(get_lower_datetime(appointment))).strftime(
datetimestring
)
upper = floor_secs(
to_local(ceil_datetime(get_upper_datetime(appointment)))
).strftime(datetimestring)
proposed = floor_secs(to_local(proposed_appt_datetime)).strftime(
datetimestring
)
proposed = floor_secs(proposed_appt_datetime).strftime(datetimestring)
# TODO: check this is correctly limiting (e.g. requistions)
self.raise_validation_error(
{
Expand Down
9 changes: 6 additions & 3 deletions edc_appointment/form_validators/appointment_form_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
from edc_consent import ConsentDefinitionDoesNotExist
from edc_consent import ConsentDefinitionDoesNotExist, NotConsentedError
from edc_consent.utils import consent_datetime_or_raise
from edc_facility.utils import get_facilities
from edc_form_validators import INVALID_ERROR
Expand Down Expand Up @@ -213,10 +213,11 @@ def validate_appt_datetime_not_before_consent_datetime(self: Any) -> None:
appt_datetime = self.cleaned_data.get("appt_datetime")
appt_status = self.cleaned_data.get("appt_status")
if appt_datetime and appt_status != NEW_APPT:
appt_datetime_utc = to_utc(appt_datetime)
consent_datetime = self.get_consent_datetime_or_raise(
appt_datetime=to_utc(appt_datetime)
appt_datetime=appt_datetime_utc
)
if to_utc(appt_datetime).date() < consent_datetime.date():
if appt_datetime_utc.date() < consent_datetime.date():
formatted_date = formatted_datetime(
to_local(consent_datetime), format_as_date=True
)
Expand Down Expand Up @@ -576,6 +577,8 @@ def get_consent_datetime_or_raise(self: Any, appt_datetime: datetime) -> datetim
)
except ConsentDefinitionDoesNotExist as e:
self.raise_validation_error({"appt_datetime": str(e)}, INVALID_APPT_DATE)
except NotConsentedError as e:
self.raise_validation_error({"appt_datetime": str(e)}, INVALID_APPT_DATE)
return consent_datetime

def validate_subject_on_schedule(self: Any) -> None:
Expand Down
2 changes: 2 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"edc_sites.apps.AppConfig",
"edc_auth.apps.AppConfig",
"edc_action_item.apps.AppConfig",
"edc_adverse_event.apps.AppConfig",
"adverse_event_app.apps.AppConfig",
"edc_offstudy.apps.AppConfig",
"edc_consent.apps.AppConfig",
"edc_crf.apps.AppConfig",
Expand Down

0 comments on commit 6ae8c0d

Please sign in to comment.