Project

General

Profile

« Previous | Next » 

Revision 8e6a3c86

Added by Serghei Mihai over 9 years ago

logging actions on models improved using post_save and pre_delete signals

View differences:

calebasse/actes/models.py
47 47
                act=act,state_name='NON_VALIDE',
48 48
                author=author, previous_state=None)
49 49
        act.last_validation_state = last_validation_state
50
        get_request().record('new-act','{obj_id} created by {user} from {ip}', obj_id=act.id)
51 50
        act.save()
52 51
        return act
53 52

  
......
225 224
    def save(self, *args, **kwargs):
226 225
        if self.parent_event and not self.parent_event.canceled:
227 226
            super(Act, self).save(*args, **kwargs)
228
            get_request().record('act-save', '{obj_id} saved by {user} from {ip}', obj_id=self.id)
229 227

  
230 228
    def duration(self):
231 229
        '''Return a displayable duration for this field.'''
......
268 266
    def delete(self):
269 267
        obj_id = self.id
270 268
        super(Act, self).delete()
271
        get_request().record('act-delete', '{obj_id} deleted by {user} from {ip}', obj_id=obj_id)
272 269

  
273 270
    class Meta:
274 271
        verbose_name = u"Acte"
calebasse/actes/views.py
86 86
    def form_valid(self, form):
87 87
        result = super(NewAct, self).form_valid(form)
88 88
        self.object.act.save()
89
        self.request.record('new-act',
90
                            '{obj_id} created by {user} from {ip}',
91
                            obj_id=self.object.id)
92 89
        return result
93 90

  
94 91
act_listing = ActListingView.as_view()
......
104 101
        if self.object.event:
105 102
            self.object.event.delete()
106 103
        if not self.object.is_billed:
107
            self.request.record('act-delete', '{obj_id} deleted by {user} from {ip}',
108
                                obj_id=self.object.id)
109 104
            self.object.delete()
110 105

  
111 106
        return HttpResponse(status=204)
calebasse/agenda/forms.py
122 122
            if commit:
123 123
                appointment.patient = patient
124 124
                appointment.save()
125
                get_request().record('new-eventwithact',
126
                                    '{obj_id} created by {user} from {ip}',
127
                                     obj_id=appointment.id)
128 125
                self.save_m2m()
129 126
                appointment.services = [self.service]
130 127

  
......
145 142
        appointment.clean()
146 143
        if commit:
147 144
            appointment.save()
148
            get_request().record('update-eventwithact',
149
                                 '{obj_id} saved by {user} from {ip}',
150
                                 obj_id=appointment.id)
151 145
            self.save_m2m()
152 146
            appointment.services = [self.service]
153 147
        return appointment
......
233 227
        event.clean()
234 228
        if commit:
235 229
            event.save()
236
            get_request().record('new-event', '{obj_id} created by {user} from {ip}',
237
                                 obj_id=event.id)
238 230
            event.services = [self.service]
239 231
        return event
240 232

  
calebasse/agenda/models.py
360 360
        assert self.start_datetime is not None
361 361
        self.sanitize() # init periodicity fields
362 362
        super(Event, self).save(*args, **kwargs)
363
        get_request().record('event-save', '{obj_id} saved by {user} from {ip}',
364
                             obj_id=self.id)
365 363
        self.acts_cleaning()
366 364

  
367 365
    def delete(self, *args, **kwargs):
......
538 536
    def update_act(self, act):
539 537
        '''Update an act to match details of the meeting'''
540 538
        self.init_act(act)
541
        changes = {'delta': self.timedelta(), 'act_type': self.act_type,
542
                   'patient': self.patient, 'date': self.start_datetime.date(),
543
                   'time': self.start_datetime.time(), 'parent': self}
544
        get_request().record('act-update', '{obj_id} updated by {user} from {ip} with: {changes}', changes=changes)
545 539
        act.save()
546 540

  
547 541
    def init_act(self, act):
......
558 552
        '''Force event_type to be patient meeting.'''
559 553
        self.event_type = EventType(id=1)
560 554
        super(EventWithAct, self).save(*args, **kwargs)
561
        get_request().record('eventwithact-save', '{obj_id} saved by {user} from {ip}', obj_id=self.id)
562 555

  
563 556
    def is_event_absence(self):
564 557
        return self.act.is_absent()
calebasse/common/models.py
1
from django.db.models.signals import post_save, pre_delete
2
from ..middleware.request import get_request
3

  
4
from calebasse.agenda.models import Event, EventWithAct, EventType
5
from calebasse.actes.models import ActValidationState, Act
6
from calebasse.dossiers.models import FileState, PatientRecord, PatientAddress, PatientContact
7
from calebasse.personnes.models import People, Worker, ExternalWorker, ExternalTherapist, Holiday
8

  
9
def object_save(sender, **kwargs):
10
    model_name = sender.__name__.lower()
11
    if kwargs.get('created'):
12
        tag = 'new-%s' % model_name
13
        log = '{obj_id} created by {user} from {ip}'
14
    else:
15
        tag = '%s-save' % model_name
16
        log = '{obj_id} saved by {user} from {ip}'
17

  
18
    obj_id = kwargs['instance'].id
19
    get_request().record(tag, log, obj_id=obj_id)
20

  
21
def object_delete(sender, **kwargs):
22
    model_name = sender.__name__.lower()
23
    get_request().record('delete-%s' % model_name,
24
                         '{obj_id} deleted by {user} from {ip}',
25
                         obj_id=kwargs['instance'].id)
26

  
27
for model in (Event, EventWithAct, EventType, ActValidationState, Act,
28
              FileState, PatientRecord, PatientAddress, PatientContact,
29
              People, Worker, ExternalWorker, ExternalTherapist, Holiday):
30
    post_save.connect(object_save, sender=model)
31
    pre_delete.connect(object_delete, sender=model)
32

  
calebasse/common/views.py
1
# Create your views here.
calebasse/dossiers/models.py
239 239
                datetime(self.date_selected.year,
240 240
                        self.date_selected.month, self.date_selected.day)
241 241
        super(FileState, self).save(**kwargs)
242
        get_request().record('filestate-save', '{obj_id} saved by {user} from {ip}', obj_id=self.id)
243 242

  
244 243
    def __unicode__(self):
245 244
        return self.status.name + ' ' + str(self.date_selected)
......
254 253
            self.patient.save()
255 254
        obj_id = self.id
256 255
        super(FileState, self).delete(*args, **kwargs)
257
        get_request().record('filestate-delete', '{obj_id} deleted by {user} from {ip}',
258
                             obj_id=obj_id)
259 256

  
260 257
class PatientAddress(models.Model):
261 258

  
......
297 294
        if self.city:
298 295
            self.display_name += self.city + ' '
299 296
        super(PatientAddress, self).save(**kwargs)
300
        get_request().record('patientaddress-save', '{obj_id} saved by {user} from {ip}', obj_id=self.id)
301 297

  
302 298
class PatientContact(People):
303 299
    class Meta:
......
576 572
        if not getattr(self, 'service', None):
577 573
            raise Exception('The field service is mandatory.')
578 574
        super(PatientRecord, self).save(*args, **kwargs)
579
        get_request().record('patientrecord-save', '{obj_id} saved by {user} from {ip}', obj_id=self.id)
580 575

  
581 576
    def get_state(self):
582 577
        return self.last_state
......
636 631
        if self.can_be_deleted():
637 632
            obj_id = self.id
638 633
            super(PatientRecord, self).delete(*args, **kwargs)
639
            get_request().record('patientrecord-delete', '{obj_id} by {user} from {ip}', obj_id=obj_id)
640 634

  
641 635
    def get_ondisk_directory(self, service):
642 636
        if not settings.PATIENT_FILES_BASE_DIRECTORY:
......
1025 1019
    patient.save()
1026 1020
    patient.policyholder = patient.patientcontact
1027 1021
    patient.save()
1028
    get_request().record('new-patient', '{first_name} {last_name} ({id}) created by {user} from {ip}',
1029
                         first_name=patient.first_name, last_name=patient.last_name, id=patient.id)
1030 1022
    return patient
calebasse/personnes/models.py
51 51
        else:
52 52
            self.display_name = self.last_name.upper()
53 53
        super(People, self).save(**kwargs)
54
        get_request().record('people-save', '{obj_id} saved by {user} from {ip}', obj_id=self.id)
55 54

  
56 55
    def __unicode__(self):
57 56
        return self.display_name
......
96 95
        if not self.initials:
97 96
            self.initials = self.get_initials()
98 97
        super(Worker, self).save(**kwargs)
99
        get_request().record('worker-save', '{obj_id} saved by {user} from {ip}', obj_id=self.id)
100 98

  
101 99
    def is_active(self):
102 100
        return self.enabled
calebasse/settings.py
164 164
    'calebasse.personnes',
165 165
    'calebasse.ressources',
166 166
    'calebasse.statistics',
167
    'calebasse.common',
167 168
    'calebasse.middleware.request',
168 169
    'south',
169 170
    'django_journal',
setup.py
37 37
            'django-tastypie == 0.9.14',
38 38
            'django-select2 < 4.3',
39 39
            'interval == 1.0.0',
40
            'python-entrouvert >= 1.3'
40
            'python-entrouvert >= 1.3',
41 41
            'django-localflavor',
42 42
            'xhtml2pdf',
43 43
            'M2Crypto',
44
            'django_journal',
44 45
        ],
45 46
        dependency_links = [
46 47
            'http://django-swingtime.googlecode.com/files/django-swingtime-0.2.1.tar.gz#egg=django-swingtime-0.2.1',

Also available in: Unified diff