From 1af9e727bc584ad6f2b59414243cdd663fe8d0c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 10 Sep 2014 22:27:15 +0200 Subject: [PATCH] dossiers: make the age format configurable via service settings Closes #5469 --- calebasse/dossiers/models.py | 35 +++++++++++++----- calebasse/dossiers/tests.py | 84 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 110 insertions(+), 9 deletions(-) diff --git a/calebasse/dossiers/models.py b/calebasse/dossiers/models.py index 3373926..091c930 100644 --- a/calebasse/dossiers/models.py +++ b/calebasse/dossiers/models.py @@ -21,6 +21,7 @@ from calebasse.ressources.models import (ServiceLinkedAbstractModel, from calebasse.actes.models import Act from ..middleware.request import get_request +from ..utils import get_service_setting DEFAULT_ACT_NUMBER_DIAGNOSTIC = 6 DEFAULT_ACT_NUMBER_TREATMENT = 30 @@ -368,18 +369,36 @@ class PatientContact(People): return None return None - def age(self): + def age(self, age_format=None): if not self.birthdate: return 'inconnu' + + if not age_format: + age_format = get_service_setting('age_format') + now = datetime.today().date() age = relativedelta(now, self.birthdate) - if age.years < 2: - # for children < 2 years, return the number of months - months = age.years * 12 + age.months - if months: - return '%s mois' % months - return '%s jours' % age.days - return '%s ans' % age.years + + # by default we return the number of months for children < 2 years, but + # there's a service setting to have it always displayed that way. + months = age.years * 12 + age.months + if months == 0: + components = [] + elif age.years < 2 or age_format == 'months_only': + components = ['%s mois' % months] + else: + components = ['%s ans' % age.years] + if age.months: + components.append('%s mois' % age.months) + + # under three months, we also display the number of days + if months < 3: + if age.days == 1: + components.append("%s jour" % age.days) + elif age.days > 1: + components.append('%s jours' % age.days) + + return ' et '.join(components) class PatientRecordManager(models.Manager): diff --git a/calebasse/dossiers/tests.py b/calebasse/dossiers/tests.py index a64f539..f22799d 100644 --- a/calebasse/dossiers/tests.py +++ b/calebasse/dossiers/tests.py @@ -1,8 +1,10 @@ # -*- coding: utf-8 -*- +import datetime + from django.test import TestCase from django.contrib.auth.models import User -from models import create_patient +from models import create_patient, PatientContact from calebasse.dossiers.models import Status from calebasse.ressources.models import Service @@ -15,3 +17,83 @@ class PatientRecordTest(TestCase): patient = create_patient('John', 'Doe', service, creator) for status in Status.objects.filter(services=service): patient.set_state(status, creator) + + +class AgeTest(TestCase): + def test_unknown(self): + patient = PatientContact() + self.assertEqual(patient.age(), "inconnu") + + def test_one_day(self): + patient = PatientContact() + patient.birthdate = datetime.date.today() - datetime.timedelta(days=1) + self.assertEqual(patient.age(), "1 jour") + + def test_three_days(self): + patient = PatientContact() + patient.birthdate = datetime.date.today() - datetime.timedelta(days=3) + self.assertEqual(patient.age(), "3 jours") + + def test_two_months(self): + patient = PatientContact() + today = datetime.date.today() + if today.day > 28: + # don't run this test on such months, we can't be sure the exact + # date will exist + return + date = today - datetime.timedelta(days=65) + patient.birthdate = datetime.date(date.year, date.month, today.day) + self.assertEqual(patient.age(), "2 mois") + + def test_two_months_one_day(self): + patient = PatientContact() + today = datetime.date.today() + if today.day > 28: + # don't run this test on such months, we can't be sure the exact + # date will exist + return + date = today - datetime.timedelta(days=62) + patient.birthdate = datetime.date(date.year, date.month, today.day) - datetime.timedelta(days=1) + self.assertEqual(patient.age(), "2 mois et 1 jour") + + def test_two_months_three_days(self): + patient = PatientContact() + today = datetime.date.today() + if today.day > 25: + # don't run this test on such months, we can't be sure the exact + # date will exist + return + date = today - datetime.timedelta(days=62) + patient.birthdate = datetime.date(date.year, date.month, today.day) - datetime.timedelta(days=3) + self.assertEqual(patient.age(), "2 mois et 3 jours") + + def test_six_months(self): + patient = PatientContact() + today = datetime.date.today() + date = today - datetime.timedelta(days=31*6) + patient.birthdate = datetime.date(date.year, date.month, date.day) + self.assertEqual(patient.age(), "6 mois") + + def test_thirtheen_months(self): + patient = PatientContact() + today = datetime.date.today() + date = today - datetime.timedelta(days=31*13) + patient.birthdate = datetime.date(date.year, date.month, date.day) + self.assertEqual(patient.age(), "13 mois") + + def test_two_years(self): + patient = PatientContact() + today = datetime.date.today() + date = today + patient.birthdate = datetime.date(date.year-2, date.month, date.day) + self.assertEqual(patient.age(), "2 ans") + self.assertEqual(patient.age(age_format='months_only'), "24 mois") + + def test_two_years_one_month(self): + patient = PatientContact() + today = datetime.date.today() + date = today + patient.birthdate = datetime.date(date.year-2, date.month, date.day) - \ + datetime.timedelta(days=31) + self.assertEqual(patient.age(), "2 ans et 1 mois") + self.assertEqual(patient.age(age_format='months_only'), "25 mois") -- 2.1.0