Project

General

Profile

Download (5.5 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / actes / models.py @ a0440975

1
# -*- coding: utf-8 -*-
2

    
3
from django.db import models
4
from django.contrib.auth.models import User
5

    
6
from calebasse.agenda.models import Event, EventType
7
from calebasse.agenda.managers import EventManager
8

    
9

    
10
class ActValidationState(models.Model):
11

    
12
    class Meta:
13
        app_label = 'actes'
14

    
15
    act = models.ForeignKey('actes.Act',
16
        verbose_name=u'Acte', editable=False)
17
    state_name = models.CharField(max_length=150)
18
    created = models.DateTimeField(u'Création', auto_now_add=True)
19
    author = \
20
        models.ForeignKey(User,
21
        verbose_name=u'Auteur', editable=False)
22
    previous_state = models.ForeignKey('ActValidationState',
23
        verbose_name=u'Etat précédent',
24
        editable=False, blank=True, null=True)
25
    # To record if the validation has be done by the automated validation
26
    auto = models.BooleanField(default=False,
27
            verbose_name=u'Vérouillage')
28

    
29

    
30
class Act(models.Model):
31
    patient = models.ForeignKey('dossiers.PatientRecord')
32
    date = models.DateTimeField()
33
    act_type = models.ForeignKey('ressources.ActType',
34
            verbose_name=u'Type d\'acte')
35
    validation_locked = models.BooleanField(default=False,
36
            verbose_name=u'Vérouillage')
37
    transport_company = models.ForeignKey('ressources.TransportCompany',
38
            blank=True,
39
            null=True,
40
            verbose_name=u'Compagnie de transport')
41
    transport_type = models.ForeignKey('ressources.TransportType',
42
            blank=True,
43
            null=True,
44
            verbose_name=u'Type de transport')
45
    doctors = models.ManyToManyField('personnes.Worker',
46
            limit_choices_to={'type__intervene': True},
47
            verbose_name=u'Thérapeutes')
48

    
49
    def is_absent(self):
50
        if self.get_state() in ('ABS_NON_EXC', 'ABS_EXC', 'ANNUL_NOUS',
51
                'ANNUL_FAMILLE', 'ABS_ESS_PPS', 'ENF_HOSP'):
52
            return True
53

    
54
    def get_state(self):
55
        return self.actvalidationstate_set.latest('created')
56

    
57
    def is_state(self, state_name):
58
        state = self.get_state()
59
        if state and state.state_name == state_name:
60
            return True
61
        return False
62

    
63
    def set_state(self, state_name, author, auto=False,
64
            change_state_check=True):
65
        if not author:
66
            raise Exception('Missing author to set state')
67
        if not state_name in VALIDATION_STATES.keys():
68
            raise Exception("Etat d'acte non existant %s")
69
        current_state = self.get_state()
70
        ActValidationState(act=self, state_name=state_name,
71
            author=author, previous_state=current_state).save()
72

    
73
    def __unicode__(self):
74
        return '{0} le {1} pour {2} avec {3}'.format(
75
                self.act_type, self.date, self.patient,
76
                ', '.join(map(unicode, self.doctors.all())))
77

    
78
    def __repr__(self):
79
        return '<%s %r %r>' % (self.__class__.__name__, unicode(self), self.id)
80

    
81
    class Meta:
82
        verbose_name = u"Acte"
83
        verbose_name_plural = u"Actes"
84
        ordering = ['-date', 'patient']
85

    
86

    
87
class EventActManager(EventManager):
88

    
89
    def create_patient_appointment(self, title, patient, participants,
90
            act_type, service, start_datetime, end_datetime, description='',
91
            room=None, note=None, **rrule_params):
92
        """
93
        This method allow you to create a new patient appointment quickly
94

    
95
        Args:
96
            title: patient appointment title (str)
97
            patient: Patient object
98
            participants: List of CalebasseUser (therapists)
99
            act_type: ActType object
100
            service: Service object. Use session service by defaut
101
            start_datetime: datetime with the start date and time
102
            end_datetime: datetime with the end date and time
103
            freq, count, until, byweekday, rrule_params:
104
            follow the ``dateutils`` API (see http://labix.org/python-dateutil)
105

    
106
        Example:
107
            Look at calebasse.agenda.tests.EventTest (test_create_appointments
108
            method)
109
        """
110

    
111
        event_type, created = EventType.objects.get_or_create(
112
                label=u"Rendez-vous patient"
113
                )
114

    
115
        act_event = EventAct.objects.create(
116
                title=title,
117
                event_type=event_type,
118
                patient=patient,
119
                act_type=act_type,
120
                date=start_datetime.date(),
121
                )
122
        act_event.doctors = participants
123

    
124
        return self._set_event(act_event, participants, description,
125
                services=[service], start_datetime=start_datetime,
126
                end_datetime=end_datetime,
127
                room=room, note=note, **rrule_params)
128

    
129

    
130
class EventAct(Act, Event):
131
    objects = EventActManager()
132

    
133
    VALIDATION_CODE_CHOICES = (
134
            ('absent', u'Absent'),
135
            ('present', u'Présent'),
136
            )
137
    attendance = models.CharField(max_length=16,
138
            choices=VALIDATION_CODE_CHOICES,
139
            default='absent',
140
            verbose_name=u'Présence')
141
    convocation_sent = models.BooleanField(blank=True,
142
            verbose_name=u'Convoqué')
143

    
144
    def __unicode__(self):
145
        return 'Rdv le {0} de {1} avec {2} pour {3}'.format(
146
                self.occurrence_set.all()[0].start_time, self.patient,
147
                ', '.join(map(unicode, self.participants.all())),
148
                self.act_type)
149

    
150
    def __repr__(self):
151
        return '<%s %r %r>' % (self.__class__.__name__, unicode(self),
152
            self.id)
153

    
154
    class Meta:
155
        verbose_name = 'Rendez-vous patient'
156
        verbose_name_plural = 'Rendez-vous patient'
157
        ordering = ['-date', 'patient']
(4-4/8)