Project

General

Profile

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

calebasse / calebasse / agenda / forms.py @ 76974b6f

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

    
3
from datetime import datetime, timedelta
4

    
5
from django import forms
6

    
7
from ..dossiers.models import PatientRecord
8
from ..personnes.models import Worker
9
from ..ressources.models import ActType
10
from ..middleware.request import get_request
11

    
12
from ajax_select import make_ajax_field
13
from models import Event, EventWithAct, EventType
14

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

    
21
    participants = make_ajax_field(EventWithAct, 'participants', 'worker-or-group', True)
22
    patient = make_ajax_field(EventWithAct, 'patient', 'patientrecord', False)
23

    
24
    class Meta:
25
        model = EventWithAct
26
        fields = (
27
                'date',
28
                'time',
29
                'duration',
30
                'patient',
31
                'participants',
32
                'room',
33
                'act_type',
34
        )
35

    
36

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

    
50
    def clean_duration(self):
51
        duration = self.cleaned_data['duration']
52
        try:
53
            return int(duration)
54
        except ValueError:
55
            return 0
56

    
57
    def save(self, commit=True):
58
        appointment = super(NewAppointmentForm, self).save(commit=False)
59
        appointment.start_datetime = datetime.combine(self.cleaned_data['date'],
60
                    self.cleaned_data['time'])
61
        appointment.end_datetime = appointment.start_datetime + timedelta(
62
                minutes=self.cleaned_data['duration'])
63
        appointment.recurrence_end_date = appointment.start_datetime.date()
64
        appointment.creator = get_request().user
65
        appointment.title = appointment.patient.display_name
66
        appointment.service = self.service
67
        appointment.clean()
68
        if commit:
69
            appointment.save()
70
        return appointment
71

    
72

    
73
class UpdateAppointmentForm(NewAppointmentForm):
74
    pass
75

    
76

    
77
class NewEventForm(forms.ModelForm):
78

    
79
    title = forms.CharField(label=u"Complément de l'intitulé", max_length=32, required=False)
80
    date = forms.DateField(label=u'Date')
81
    time = forms.TimeField(label=u'Heure de début')
82
    duration = forms.CharField(label=u'Durée',
83
            help_text=u'en minutes; vous pouvez utiliser la roulette de votre souris.')
84

    
85
    participants = make_ajax_field(Event, 'participants', 'worker-or-group', True)
86

    
87
    class Meta:
88
        model = Event
89
        fields = (
90
                'title',
91
                'date',
92
                'time',
93
                'duration',
94
                'room',
95
                'participants',
96
                'event_type'
97
        )
98

    
99
    def __init__(self, instance, **kwargs):
100
        super(NewEventForm, self).__init__(instance=instance, **kwargs)
101
        self.fields['date'].css = 'datepicker'
102
        self.fields['event_type'].queryset = \
103
                    EventType.objects.exclude(id=1).exclude(id=3).order_by('rank', 'label')
104

    
105
    def clean_duration(self):
106
        duration = self.cleaned_data['duration']
107
        try:
108
            return int(duration)
109
        except:
110
            return None
111

    
112
    def save(self, commit=True):
113
        event = super(NewEventForm, self).save(commit=False)
114
        event.start_datetime = datetime.combine(self.cleaned_data['date'],
115
                    self.cleaned_data['time'])
116
        event.end_datetime = event.start_datetime + timedelta(
117
                minutes=self.cleaned_data['duration'])
118
        event.recurrence_end_date = event.start_datetime.date()
119
        event.creator = get_request().user
120
        event.service = self.service
121
        event.clean()
122
        if commit:
123
            event.save()
124
        return event
125

    
126
    def clean(self):
127
        cleaned_data = super(NewEventForm, self).clean()
128
        event_type = cleaned_data.get('event_type')
129
        if event_type and event_type.id == 4 and not cleaned_data.get('title'): # 'Autre'
130
            self._errors['title'] = self.error_class([
131
                            u"Ce champ est obligatoire pour les événements de type « Autre »."])
132
        return cleaned_data
(5-5/10)