Projet

Général

Profil

« Précédent | Suivant » 

Révision 1af9e727

Ajouté par Frédéric Péters il y a plus de 9 ans

dossiers: make the age format configurable via service settings

Closes #5469

Voir les différences:

calebasse/dossiers/models.py
21 21
from calebasse.actes.models import Act
22 22

  
23 23
from ..middleware.request import get_request
24
from ..utils import get_service_setting
24 25

  
25 26
DEFAULT_ACT_NUMBER_DIAGNOSTIC = 6
26 27
DEFAULT_ACT_NUMBER_TREATMENT = 30
......
368 369
                return None
369 370
        return None
370 371

  
371
    def age(self):
372
    def age(self, age_format=None):
372 373
        if not self.birthdate:
373 374
            return 'inconnu'
375

  
376
        if not age_format:
377
            age_format = get_service_setting('age_format')
378

  
374 379
        now = datetime.today().date()
375 380
        age = relativedelta(now, self.birthdate)
376
        if age.years < 2:
377
            # for children < 2 years, return the number of months
378
            months = age.years * 12 + age.months
379
            if months:
380
                return '%s mois' % months
381
            return '%s jours' % age.days
382
        return '%s ans' % age.years
381

  
382
        # by default we return the number of months for children < 2 years, but
383
        # there's a service setting to have it always displayed that way.
384
        months = age.years * 12 + age.months
385
        if months == 0:
386
            components = []
387
        elif age.years < 2 or age_format == 'months_only':
388
            components = ['%s mois' % months]
389
        else:
390
            components = ['%s ans' % age.years]
391
            if age.months:
392
                components.append('%s mois' % age.months)
393

  
394
        # under three months, we also display the number of days
395
        if months < 3:
396
            if age.days == 1:
397
                components.append("%s jour" % age.days)
398
            elif age.days > 1:
399
                components.append('%s jours' % age.days)
400

  
401
        return ' et '.join(components)
383 402

  
384 403

  
385 404
class PatientRecordManager(models.Manager):
calebasse/dossiers/tests.py
1 1
# -*- coding: utf-8 -*-
2
import datetime
3

  
2 4
from django.test import TestCase
3 5
from django.contrib.auth.models import User
4 6

  
5
from models import create_patient
7
from models import create_patient, PatientContact
6 8
from calebasse.dossiers.models import Status
7 9
from calebasse.ressources.models import Service
8 10

  
......
15 17
            patient = create_patient('John', 'Doe', service, creator)
16 18
            for status in Status.objects.filter(services=service):
17 19
                patient.set_state(status, creator)
20

  
21

  
22
class AgeTest(TestCase):
23
    def test_unknown(self):
24
        patient = PatientContact()
25
        self.assertEqual(patient.age(), "inconnu")
26

  
27
    def test_one_day(self):
28
        patient = PatientContact()
29
        patient.birthdate = datetime.date.today() - datetime.timedelta(days=1)
30
        self.assertEqual(patient.age(), "1 jour")
31

  
32
    def test_three_days(self):
33
        patient = PatientContact()
34
        patient.birthdate = datetime.date.today() - datetime.timedelta(days=3)
35
        self.assertEqual(patient.age(), "3 jours")
36

  
37
    def test_two_months(self):
38
        patient = PatientContact()
39
        today = datetime.date.today()
40
        if today.day > 28:
41
            # don't run this test on such months, we can't be sure the exact
42
            # date will exist
43
            return
44
        date = today - datetime.timedelta(days=65)
45
        patient.birthdate = datetime.date(date.year, date.month, today.day)
46
        self.assertEqual(patient.age(), "2 mois")
47

  
48
    def test_two_months_one_day(self):
49
        patient = PatientContact()
50
        today = datetime.date.today()
51
        if today.day > 28:
52
            # don't run this test on such months, we can't be sure the exact
53
            # date will exist
54
            return
55
        date = today - datetime.timedelta(days=62)
56
        patient.birthdate = datetime.date(date.year, date.month, today.day) - datetime.timedelta(days=1)
57
        self.assertEqual(patient.age(), "2 mois et 1 jour")
58

  
59
    def test_two_months_three_days(self):
60
        patient = PatientContact()
61
        today = datetime.date.today()
62
        if today.day > 25:
63
            # don't run this test on such months, we can't be sure the exact
64
            # date will exist
65
            return
66
        date = today - datetime.timedelta(days=62)
67
        patient.birthdate = datetime.date(date.year, date.month, today.day) - datetime.timedelta(days=3)
68
        self.assertEqual(patient.age(), "2 mois et 3 jours")
69

  
70
    def test_six_months(self):
71
        patient = PatientContact()
72
        today = datetime.date.today()
73
        date = today - datetime.timedelta(days=31*6)
74
        patient.birthdate = datetime.date(date.year, date.month, date.day)
75
        self.assertEqual(patient.age(), "6 mois")
76

  
77
    def test_thirtheen_months(self):
78
        patient = PatientContact()
79
        today = datetime.date.today()
80
        date = today - datetime.timedelta(days=31*13)
81
        patient.birthdate = datetime.date(date.year, date.month, date.day)
82
        self.assertEqual(patient.age(), "13 mois")
83

  
84
    def test_two_years(self):
85
        patient = PatientContact()
86
        today = datetime.date.today()
87
        date = today
88
        patient.birthdate = datetime.date(date.year-2, date.month, date.day)
89
        self.assertEqual(patient.age(), "2 ans")
90
        self.assertEqual(patient.age(age_format='months_only'), "24 mois")
91

  
92
    def test_two_years_one_month(self):
93
        patient = PatientContact()
94
        today = datetime.date.today()
95
        date = today
96
        patient.birthdate = datetime.date(date.year-2, date.month, date.day) - \
97
                datetime.timedelta(days=31)
98
        self.assertEqual(patient.age(), "2 ans et 1 mois")
99
        self.assertEqual(patient.age(age_format='months_only'), "25 mois")

Formats disponibles : Unified diff