Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds an age property to the demographcis model #1689

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

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


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

A User's UserProfile is now automatically created when you create a user in a post save signal.

### 0.13.1 (Minor Release)
Expand Down
32 changes: 32 additions & 0 deletions doc/docs/reference/subrecords.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,35 @@ fields external_system and external_identifier to allow us to track where
they come from and how they are referenced by that system.

These fields are then often used in forms to make the data read only


### Specific subrecords

#### Demographics
Opal ships with a demographics model that models personally identifying information for a patient.

This model maps

- `hospital_number`
- `nhs_number`
- `surname`
- `first_name`
- `middle name`
- `title`
- `date_of_birth`
- `marital_status`
- `religion`
- `date_of_death`
- `post_code`
- `birth_place`
- `ethnicity`
- `death_indicator`
- `sex`

It also has the properties:

- `name` - The first name and the surname combined.
- `age` - The current age of the patient (today - their date of birth)



18 changes: 18 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 @@ -1412,8 +1413,25 @@ class Demographics(PatientSubrecord):

@property
def name(self):
"""
Property that returns the name of the patient constructed from
`first_name` and `surname`
"""
return '{0} {1}'.format(self.first_name, self.surname)

@property
def age(self):
"""
Property that returns the age of the patient in years
or None if `date_of_birth` is not set.
"""
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 @@ -820,6 +820,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