Skip to content

Commit 024c186

Browse files
committed
fasting_duration_minutes to fasting_duration_delta, calc delta on save()
1 parent f80df30 commit 024c186

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

edc_glucose/model_mixins/fasting_model_mixin.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.db import models
44
from edc_constants.choices import YES_NO_NA
55
from edc_constants.constants import NOT_APPLICABLE
6+
from edc_model.utils import duration_hm_to_timedelta
67
from edc_model.validators import hm_validator
78

89

@@ -13,6 +14,12 @@ def fasting_model_mixin_factory(
1314
verbose_names = verbose_names or {}
1415

1516
class AbstractModel(models.Model):
17+
def save(self, *args, **kwargs):
18+
if duration_str := getattr(self, f"{prefix}fasting_duration_str", None):
19+
tdelta = duration_hm_to_timedelta(duration_str)
20+
setattr(self, f"{prefix}fasting_duration_delta", tdelta)
21+
super().save(*args, **kwargs)
22+
1623
class Meta:
1724
abstract = True
1825

@@ -39,8 +46,10 @@ class Meta:
3946
"For example 1h23m, 12h7m, etc"
4047
),
4148
),
42-
f"{prefix}fasting_duration_minutes": models.IntegerField(
43-
null=True, blank=True, help_text="system calculated value"
49+
f"{prefix}fasting_duration_delta": models.DurationField(
50+
null=True,
51+
blank=True,
52+
help_text="system calculated to microseconds. (hours=microseconds/3.6e+9)",
4453
),
4554
}
4655

edc_glucose/model_mixins/glucose_model_mixin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from edc_lab.choices import GLUCOSE_UNITS_NA, RESULT_QUANTIFIER_NA
66

77
from ..constants import GLUCOSE_HIGH_READING
8-
from . import fasting_model_mixin_factory
8+
from .fasting_model_mixin import fasting_model_mixin_factory
99

1010

1111
def glucose_model_mixin_factory(utest_id: str, verbose_names: dict | None = None, **kwargs):

edc_glucose/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
from .constants import GLUCOSE_HIGH_READING
88

99

10-
def validate_glucose_as_millimoles_per_liter(prefix: str, cleaned_data: dict = None) -> None:
10+
def validate_glucose_as_millimoles_per_liter(
11+
prefix: str, cleaned_data: dict = None
12+
) -> None | Decimal:
13+
converted_value = None
1114
min_val = Decimal("0.00")
1215
max_val = Decimal("30.00")
1316
high_value = Decimal(f"{GLUCOSE_HIGH_READING}")
@@ -31,3 +34,4 @@ def validate_glucose_as_millimoles_per_liter(prefix: str, cleaned_data: dict = N
3134
)
3235
}
3336
)
37+
return converted_value

0 commit comments

Comments
 (0)