Project

General

Profile

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

calebasse / calebasse / agenda / forms.py @ af376234

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

    
3
from datetime import datetime, timedelta
4

    
5
from django import forms
6

    
7
from calebasse.dossiers.models import PatientRecord
8
from calebasse.personnes.models import Worker
9
from calebasse.actes.models import EventAct
10

    
11
from ajax_select import make_ajax_field
12

    
13
class NewAppointmentForm(forms.ModelForm):
14
    time = forms.TimeField(label=u'Heure de début')
15
    duration = forms.CharField(label=u'Durée',
16
            help_text=u'en minutes; vous pouvez utiliser la roulette de votre souris.')
17

    
18
    participants = make_ajax_field(EventAct, 'participants', 'worker', True)
19
    patient = make_ajax_field(EventAct, 'patient', 'patientrecord', False)
20

    
21
    class Meta:
22
        model = EventAct
23
        fields = (
24
                'date',
25
                'time',
26
                'duration',
27
                'patient',
28
                'participants',
29
                'room',
30
                'act_type',
31
        )
32

    
33

    
34
    def __init__(self, instance, service=None, **kwargs):
35
        self.service = None
36
        super(NewAppointmentForm, self).__init__(instance=instance, **kwargs)
37
        self.fields['date'].css = 'datepicker'
38
        if service:
39
            self.service = service
40
            self.fields['participants'].queryset = \
41
                    Worker.objects.for_service(service)
42
            self.fields['patient'].queryset = \
43
                    PatientRecord.objects.for_service(service)
44

    
45
    def clean_duration(self):
46
        duration = self.cleaned_data['duration']
47
        try:
48
            return int(duration)
49
        except:
50
            return None
51

    
52
    def save(self, commit=False):
53
        start_datetime = datetime.combine(self.cleaned_data['date'],
54
                    self.cleaned_data['time'])
55
        end_datetime = start_datetime + timedelta(
56
                minutes=self.cleaned_data['duration'])
57
        patient = self.cleaned_data['patient']
58
        self.instance = EventAct.objects.create_patient_appointment(
59
                title=+ patient.display_name,
60
                patient=patient,
61
                participants=self.cleaned_data['participants'],
62
                act_type=self.cleaned_data['act_type'],
63
                service=self.service,
64
                start_datetime=start_datetime,
65
                end_datetime=end_datetime,
66
                description='',
67
                room=self.cleaned_data['room'],
68
                note=None,)
69
        return self.instance
70

    
(5-5/10)