Skip to content

Commit

Permalink
fasting_duration_minutes to fasting_duration_delta, calc delta on save()
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed May 20, 2024
1 parent f80df30 commit 024c186
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
13 changes: 11 additions & 2 deletions edc_glucose/model_mixins/fasting_model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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

Expand All @@ -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)",
),
}

Expand Down
2 changes: 1 addition & 1 deletion edc_glucose/model_mixins/glucose_model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 5 additions & 1 deletion edc_glucose/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -31,3 +34,4 @@ def validate_glucose_as_millimoles_per_liter(prefix: str, cleaned_data: dict = N
)
}
)
return converted_value

0 comments on commit 024c186

Please sign in to comment.