Skip to content

Commit

Permalink
adds an age property to the demographcis model
Browse files Browse the repository at this point in the history
  • Loading branch information
fredkingham committed Feb 22, 2019
1 parent 167024c commit 2c46977
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
layout python python3.4

5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 0.14.0 (Major Release)

Adds a `age` property to demographics that returns the patient's age.


### 0.13.0 (Major Release)

#### Removes support for Python 2.x
Expand Down
10 changes: 10 additions & 0 deletions opal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import random
import os
from dateutil.relativedelta import relativedelta

from django.utils import timezone
from django.db import models, transaction
Expand Down Expand Up @@ -1413,6 +1414,15 @@ class Demographics(PatientSubrecord):
def name(self):
return '{0} {1}'.format(self.first_name, self.surname)

@property
def age(self):
if self.date_of_birth:
today = datetime.date.today()
return relativedelta(
today,
self.date_of_birth
).years

class Meta:
abstract = True
verbose_name_plural = "Demographics"
Expand Down
10 changes: 10 additions & 0 deletions opal/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,16 @@ def test_name(self):
middle_name='Obsidian')
self.assertEqual('Jane Doe', d.name)

def test_age_no_date_of_birth(self):
d = models.Demographics(date_of_birth=None)
self.assertIsNone(d.age)

@patch("opal.models.datetime")
def test_age(self, dt):
dt.date.today.return_value = datetime.date(2017, 3, 1)
d = models.Demographics(date_of_birth=datetime.date(2016, 1, 28))
self.assertEqual(d.age, 1)


class ExternalSystemTestCase(OpalTestCase):
def test_get_footer(self):
Expand Down

0 comments on commit 2c46977

Please sign in to comment.