Project

General

Profile

« Previous | Next » 

Revision 76974b6f

Added by Benjamin Dauvergne almost 12 years ago

agenda/actes/dossiers: move Occurence fields into Event, add recurring events support

View differences:

calebasse/agenda/forms.py
4 4

  
5 5
from django import forms
6 6

  
7
from calebasse.dossiers.models import PatientRecord
8
from calebasse.personnes.models import Worker
9
from calebasse.actes.models import EventAct
10
from calebasse.agenda.models import Event, EventType
11
from calebasse.ressources.models import ActType
12
from calebasse.middleware.request import get_request
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
13 11

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

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

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

  
25 24
    class Meta:
26
        model = EventAct
25
        model = EventWithAct
27 26
        fields = (
28 27
                'date',
29 28
                'time',
......
52 51
        duration = self.cleaned_data['duration']
53 52
        try:
54 53
            return int(duration)
55
        except:
56
            return None
54
        except ValueError:
55
            return 0
57 56

  
58
    def save(self, commit=False):
59
        start_datetime = datetime.combine(self.cleaned_data['date'],
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 60
                    self.cleaned_data['time'])
61
        end_datetime = start_datetime + timedelta(
61
        appointment.end_datetime = appointment.start_datetime + timedelta(
62 62
                minutes=self.cleaned_data['duration'])
63
        patient = self.cleaned_data['patient']
64
        creator = get_request().user
65
        self.instance = EventAct.objects.create_patient_appointment(
66
                creator=creator,
67
                title=patient.display_name,
68
                patient=patient,
69
                participants=self.cleaned_data['participants'],
70
                act_type=self.cleaned_data['act_type'],
71
                service=self.service,
72
                start_datetime=start_datetime,
73
                end_datetime=end_datetime,
74
                description='',
75
                room=self.cleaned_data['room'],
76
                note=None,)
77
        return self.instance
78

  
79
class UpdateAppointmentForm(NewAppointmentForm):
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
80 71

  
81
    def __init__(self, instance, service=None, occurrence=None, **kwargs):
82
        super(UpdateAppointmentForm, self).__init__(instance=instance,
83
                                                    service=service, **kwargs)
84
        self.occurrence = occurrence
85 72

  
86

  
87
    def save(self):
88
        self.occurrence.start_time = datetime.combine(
89
                self.cleaned_data['date'],
90
                self.cleaned_data['time'])
91
        self.occurrence.end_time = self.occurrence.start_time + timedelta(
92
                minutes=self.cleaned_data['duration'])
93
        self.occurrence.save()
94
        patient = self.cleaned_data['patient']
95
        creator = get_request().user
96
        self.instance.title = patient.display_name
97
        self.instance.participants = self.cleaned_data['participants']
98
        self.instance.save()
99
        return self.instance
73
class UpdateAppointmentForm(NewAppointmentForm):
74
    pass
100 75

  
101 76

  
102 77
class NewEventForm(forms.ModelForm):
......
106 81
    time = forms.TimeField(label=u'Heure de début')
107 82
    duration = forms.CharField(label=u'Durée',
108 83
            help_text=u'en minutes; vous pouvez utiliser la roulette de votre souris.')
109
    participants = make_ajax_field(EventAct, 'participants', 'worker-or-group', True)
84

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

  
111 87
    class Meta:
112 88
        model = Event
113
        widgets = {'services': forms.CheckboxSelectMultiple}
114 89
        fields = (
115 90
                'title',
116 91
                'date',
......
118 93
                'duration',
119 94
                'room',
120 95
                'participants',
121
                'services',
122 96
                'event_type'
123 97
        )
124 98

  
......
135 109
        except:
136 110
            return None
137 111

  
138
    def save(self, commit=False):
139
        start_datetime = datetime.combine(self.cleaned_data['date'],
112
    def save(self, commit=True):
113
        event = super(NewEventForm, self).save(commit=False)
114
        event.start_datetime = datetime.combine(self.cleaned_data['date'],
140 115
                    self.cleaned_data['time'])
141
        end_datetime = start_datetime + timedelta(
116
        event.end_datetime = event.start_datetime + timedelta(
142 117
                minutes=self.cleaned_data['duration'])
143
        self.instance = Event.objects.create_event(
144
                title=self.cleaned_data['title'],
145
                event_type=self.cleaned_data['event_type'],
146
                participants=self.cleaned_data['participants'],
147
                services=self.cleaned_data['services'],
148
                start_datetime=start_datetime,
149
                end_datetime=end_datetime,
150
                description='',
151
                room=self.cleaned_data['room'],
152
                note=None,)
153
        return self.instance
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
154 125

  
155 126
    def clean(self):
156 127
        cleaned_data = super(NewEventForm, self).clean()
......
159 130
            self._errors['title'] = self.error_class([
160 131
                            u"Ce champ est obligatoire pour les événements de type « Autre »."])
161 132
        return cleaned_data
162

  
163

  
164
class UpdateEventForm(NewEventForm):
165

  
166
    def __init__(self, instance, occurrence=None, **kwargs):
167
        super(UpdateEventForm, self).__init__(instance=instance, **kwargs)
168
        self.occurrence = occurrence
169

  
170
    def save(self):
171
        self.occurrence.start_time = datetime.combine(
172
                self.cleaned_data['date'],
173
                self.cleaned_data['time'])
174
        self.occurrence.end_time = self.occurrence.start_time + timedelta(
175
                minutes=self.cleaned_data['duration'])
176
        self.occurrence.save()
177
        creator = get_request().user
178
        self.instance.participants = self.cleaned_data['participants']
179
        self.instance.services = self.cleaned_data['services']
180
        self.instance.save()
181
        return self.instance

Also available in: Unified diff