Project

General

Profile

« Previous | Next » 

Revision d714d23e

Added by Mikaël Ates over 12 years ago

dossiers: add member methods to FileRecord for states management.

View differences:

calebasse/dossiers/models.py
57 57
        editable=False)
58 58
    contacts = models.ManyToManyField('personnes.People',
59 59
            related_name='contact_of')
60

  
61
    def __init__(self, *args, **kwargs):
62
        super(PatientRecord, self).__init__(*args, **kwargs)
63
        if not hasattr(self, 'service'):
64
            raise Exception('The field service is mandatory.')
65

  
66
    def get_state(self):
67
        last_state = self.filestate_set.latest('date_selected')
68
        multiple = self.filestate_set.\
69
            filter(date_selected=last_state.date_selected)
70
        if len(multiple) > 1:
71
            last_state = multiple.latest('created')
72
        return last_state
73

  
74
    def get_state_at_day(self, date):
75
        state = self.get_state()
76
        while(state):
77
            if datetime(state.date_selected.year,
78
                    state.date_selected.month, state.date_selected.day) <= \
79
                    datetime(date.year, date.month, date.day):
80
                return state
81
            state = state.previous_state
82
        return None
83

  
84
    def was_in_state_at_day(self, date, state_name):
85
        state_at_day = self.get_state_at_day(date)
86
        if state_at_day and state_at_day.state_name == state_name:
87
            return True
88
        return False
89

  
90
    def get_states_history(self):
91
        return self.filestate_set.order_by('date_selected')
92

  
93
    def set_state(self, state_name, author, date_selected=None, comment=None):
94
        if not author:
95
            raise Exception('Missing author to set state')
96
        if not state_name in STATES[self.service.name].keys():
97
            raise Exception('Etat de dossier '
98
                'non existant dans le service %s.' % self.service.name)
99
        if not date_selected:
100
            date_selected = datetime.now()
101
        current_state = self.get_state()
102
        if not current_state:
103
            raise Exception('Invalid patient record. '
104
                'Missing current state.')
105
        if date_selected < current_state.date_selected:
106
            raise Exception('You cannot set a state starting the %s that '
107
                'is before the previous state starting at day %s.' % \
108
                (str(date_selected), str(current_state.date_selected)))
109
        FileState(patient=self, state_name=state_name,
110
            date_selected=date_selected, author=author, comment=comment,
111
            previous_state=current_state).save()
112

  
113
    def change_day_selected_of_state(self, state, new_date):
114
        if state.previous_state:
115
            if new_date < state.previous_state.date_selected:
116
                raise Exception('You cannot set a state starting the %s '
117
                    'before the previous state starting at day %s.' % \
118
                    (str(new_date), str(state.previous_state.date_selected)))
119
        next_state = state.get_next_state()
120
        if next_state:
121
            if new_date > next_state.date_selected:
122
                raise Exception('You cannot set a state starting the %s '
123
                    'after the following state starting at day %s.' % \
124
                    (str(new_date), str(next_state.date_selected)))
125
        state.date_selected = new_date
126
        state.save()
127

  
128
    def remove_state(self, state):
129
        if state.patient.id != self.id:
130
            raise Exception('The state given is not about this patient '
131
                'record but about %s' % state.patient)
132
        next_state = state.get_next_state()
133
        if not next_state:
134
            self.remove_last_state()
135
        else:
136
            next_state.previous_state = state.previous_state
137
            next_state.save()
138
            state.delete()
139

  
140
    def remove_last_state(self):
141
        try:
142
            self.get_state().delete()
143
        except:
144
            pass

Also available in: Unified diff