diff --git a/edc_glucose/model_mixins/fasting_model_mixin.py b/edc_glucose/model_mixins/fasting_model_mixin.py index 4e02cd9..9495cee 100644 --- a/edc_glucose/model_mixins/fasting_model_mixin.py +++ b/edc_glucose/model_mixins/fasting_model_mixin.py @@ -3,6 +3,7 @@ from django.db import models from edc_constants.choices import YES_NO_NA from edc_constants.constants import NOT_APPLICABLE +from edc_model.utils import duration_hm_to_timedelta from edc_model.validators import hm_validator @@ -13,6 +14,12 @@ def fasting_model_mixin_factory( verbose_names = verbose_names or {} class AbstractModel(models.Model): + def save(self, *args, **kwargs): + if duration_str := getattr(self, f"{prefix}fasting_duration_str", None): + tdelta = duration_hm_to_timedelta(duration_str) + setattr(self, f"{prefix}fasting_duration_delta", tdelta) + super().save(*args, **kwargs) + class Meta: abstract = True @@ -39,8 +46,10 @@ class Meta: "For example 1h23m, 12h7m, etc" ), ), - f"{prefix}fasting_duration_minutes": models.IntegerField( - null=True, blank=True, help_text="system calculated value" + f"{prefix}fasting_duration_delta": models.DurationField( + null=True, + blank=True, + help_text="system calculated to microseconds. (hours=microseconds/3.6e+9)", ), } diff --git a/edc_glucose/model_mixins/glucose_model_mixin.py b/edc_glucose/model_mixins/glucose_model_mixin.py index 3ad9117..016dc8f 100644 --- a/edc_glucose/model_mixins/glucose_model_mixin.py +++ b/edc_glucose/model_mixins/glucose_model_mixin.py @@ -5,7 +5,7 @@ from edc_lab.choices import GLUCOSE_UNITS_NA, RESULT_QUANTIFIER_NA from ..constants import GLUCOSE_HIGH_READING -from . import fasting_model_mixin_factory +from .fasting_model_mixin import fasting_model_mixin_factory def glucose_model_mixin_factory(utest_id: str, verbose_names: dict | None = None, **kwargs): diff --git a/edc_glucose/utils.py b/edc_glucose/utils.py index cf25540..b2e8685 100644 --- a/edc_glucose/utils.py +++ b/edc_glucose/utils.py @@ -7,7 +7,10 @@ from .constants import GLUCOSE_HIGH_READING -def validate_glucose_as_millimoles_per_liter(prefix: str, cleaned_data: dict = None) -> None: +def validate_glucose_as_millimoles_per_liter( + prefix: str, cleaned_data: dict = None +) -> None | Decimal: + converted_value = None min_val = Decimal("0.00") max_val = Decimal("30.00") high_value = Decimal(f"{GLUCOSE_HIGH_READING}") @@ -31,3 +34,4 @@ def validate_glucose_as_millimoles_per_liter(prefix: str, cleaned_data: dict = N ) } ) + return converted_value