Project

General

Profile

0001-logging-actions-on-models-with-django_journal.patch

Serghei Mihai, 15 July 2014 04:25 PM

Download (13.1 KB)

View differences:

Subject: [PATCH] logging actions on models with django_journal

Closes #1988
 calebasse/actes/models.py     | 10 +++++++++-
 calebasse/actes/views.py      | 11 +++++++++++
 calebasse/agenda/forms.py     | 10 ++++++++--
 calebasse/agenda/models.py    | 10 ++++++++++
 calebasse/agenda/views.py     |  1 -
 calebasse/dossiers/models.py  | 12 ++++++++++++
 calebasse/personnes/models.py |  4 ++++
 calebasse/settings.py         |  2 ++
 8 files changed, 56 insertions(+), 4 deletions(-)
calebasse/actes/models.py
8 8
from calebasse.ressources.models import ServiceLinkedAbstractModel
9 9
from ..middleware.request import get_request
10 10

  
11

  
12 11
class ActValidationState(models.Model):
13 12

  
14 13
    class Meta:
......
48 47
                act=act,state_name='NON_VALIDE',
49 48
                author=author, previous_state=None)
50 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 51
        act.save()
52 52
        return act
53 53

  
......
201 201
            self.valide = True
202 202
        else:
203 203
            self.valide = False
204
        get_request().record('act-update', '{obj_id} state changed to {state} by {user} from {ip}',
205
                              obj_id=self.id, state=last_validation_state, user=author)
204 206
        self.save()
205 207

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

  
227 230
    def duration(self):
228 231
        '''Return a displayable duration for this field.'''
......
262 265
            pass
263 266
        return None
264 267

  
268
    def delete(self):
269
        obj_id = self.id
270
        super(Act, self).delete()
271
        get_request().record('act-delete', '{obj_id} deleted by {user} from {ip}', obj_id=obj_id)
272

  
265 273
    class Meta:
266 274
        verbose_name = u"Acte"
267 275
        verbose_name_plural = u"Actes"
calebasse/actes/views.py
74 74
    def get_context_data(self, **kwargs):
75 75
        ctx = super(ActListingView, self).get_context_data(**kwargs)
76 76
        ctx['search_form'] = self.search_form
77
        self.request.record('acts-view', 'act listing by {user} from {ip}')
77 78
        return ctx
78 79

  
79 80
class NewAct(NewAppointmentView):
......
85 86
    def form_valid(self, form):
86 87
        result = super(NewAct, self).form_valid(form)
87 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)
88 92
        return result
89 93

  
90 94
act_listing = ActListingView.as_view()
......
100 104
        if self.object.event:
101 105
            self.object.event.delete()
102 106
        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)
103 109
            self.object.delete()
104 110

  
105 111
        return HttpResponse(status=204)
......
119 125
            self.object.event.participants =  doctors
120 126
            self.object.event.act_type =  self.object.act_type
121 127
            self.object.event.save()
128
            self.request.record('act-update',
129
                                '{obj_id} updated by {user} from {ip} with: {changes}',
130
                                obj_id=self.object.id,
131
                                changes={'participants': doctors, 'act_type': self.object.act_type})
122 132
        return result
123 133

  
124 134
update_act = UpdateActView.as_view()
......
133 143
        act.is_billed = False
134 144
        act.healthcare = None
135 145
        act.save()
146
        self.request.record('rebill-act', '{obj_id} rebilled by {user} from {ip}', obj_id=act.id)
136 147
        return super(RebillActView, self).post(request, *args, **kwarg)
137 148

  
138 149
rebill_act = RebillActView.as_view()
calebasse/agenda/forms.py
43 43
                'start_datetime': forms.HiddenInput,
44 44
        }
45 45

  
46

  
47

  
48 46
    def __init__(self, instance, service=None, **kwargs):
49 47
        self.service = None
50 48
        super(NewAppointmentForm, self).__init__(instance=instance, **kwargs)
......
100 98
            if commit:
101 99
                appointment.patient = patient
102 100
                appointment.save()
101
                get_request().record('new-eventwithact',
102
                                    '{obj_id} created by {user} from {ip}',
103
                                     obj_id=appointment.id)
103 104
                self.save_m2m()
104 105
                appointment.services = [self.service]
105 106

  
......
120 121
        appointment.clean()
121 122
        if commit:
122 123
            appointment.save()
124
            get_request().record('update-eventwithact',
125
                                 '{obj_id} saved by {user} from {ip}',
126
                                 obj_id=appointment.id)
123 127
            self.save_m2m()
124 128
            appointment.services = [self.service]
125 129
        return appointment
......
205 209
        event.clean()
206 210
        if commit:
207 211
            event.save()
212
            get_request().record('new-event', '{obj_id} created by {user} from {ip}',
213
                                 obj_id=event.id)
208 214
            event.services = [self.service]
209 215
        return event
210 216

  
calebasse/agenda/models.py
11 11
from calebasse.agenda import managers
12 12
from calebasse.utils import weeks_since_epoch, weekday_ranks
13 13
from calebasse.personnes.models import Holiday
14

  
15
from ..middleware.request import get_request
16

  
14 17
from interval import Interval
15 18

  
16 19
__all__ = (
......
357 360
        assert self.start_datetime is not None
358 361
        self.sanitize() # init periodicity fields
359 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)
360 365
        self.acts_cleaning()
361 366

  
362 367
    def delete(self, *args, **kwargs):
......
533 538
    def update_act(self, act):
534 539
        '''Update an act to match details of the meeting'''
535 540
        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)
536 545
        act.save()
537 546

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

  
553 563
    def is_event_absence(self):
554 564
        return self.act.is_absent()
calebasse/agenda/views.py
239 239
        messages.add_message(self.request, messages.INFO, u'Evénement enregistré avec succès.')
240 240
        return super(NewEventView, self).form_valid(form)
241 241

  
242

  
243 242
class BaseEventView(UpdateView):
244 243
    model = Event
245 244
    form_class = UpdateEventForm
calebasse/dossiers/models.py
20 20
        NamedAbstractModel)
21 21
from calebasse.actes.models import Act
22 22

  
23
from ..middleware.request import get_request
24

  
23 25
DEFAULT_ACT_NUMBER_DIAGNOSTIC = 6
24 26
DEFAULT_ACT_NUMBER_TREATMENT = 30
25 27
DEFAULT_ACT_NUMBER_PROLONGATION = 10
......
237 239
                datetime(self.date_selected.year,
238 240
                        self.date_selected.month, self.date_selected.day)
239 241
        super(FileState, self).save(**kwargs)
242
        get_request().record('filestate-save', '{obj_id} saved by {user} from {ip}', obj_id=self.id)
240 243

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

  
254 260
class PatientAddress(models.Model):
255 261

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

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

  
573 581
    def get_state(self):
574 582
        return self.last_state
......
626 634

  
627 635
    def delete(self, *args, **kwargs):
628 636
        if self.can_be_deleted():
637
            obj_id = self.id
629 638
            super(PatientRecord, self).delete(*args, **kwargs)
639
            get_request().record('patientrecord-delete', '{obj_id} by {user} from {ip}', obj_id=obj_id)
630 640

  
631 641
    def get_ondisk_directory(self, service):
632 642
        if not settings.PATIENT_FILES_BASE_DIRECTORY:
......
1015 1025
    patient.save()
1016 1026
    patient.policyholder = patient.patientcontact
1017 1027
    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)
1018 1030
    return patient
calebasse/personnes/models.py
21 21
from model_utils import Choices
22 22
from model_utils.managers import PassThroughManager
23 23

  
24
from ..middleware.request import get_request
25

  
24 26
class Role(NamedAbstractModel):
25 27
    users = models.ManyToManyField(User,
26 28
                verbose_name=u'Utilisateurs', blank=True)
......
49 51
        else:
50 52
            self.display_name = self.last_name.upper()
51 53
        super(People, self).save(**kwargs)
54
        get_request().record('people-save', '{obj_id} saved by {user} from {ip}', obj_id=self.id)
52 55

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

  
97 101
    def is_active(self):
98 102
        return self.enabled
calebasse/settings.py
113 113
    'reversion.middleware.RevisionMiddleware',
114 114
    # Entr'ouvert wsgi middleware to expose version
115 115
    'entrouvert.djommon.middleware.VersionMiddleware',
116
    'django_journal.middleware.JournalMiddleware',
116 117
)
117 118

  
118 119
ROOT_URLCONF = 'calebasse.urls'
......
165 166
    'calebasse.statistics',
166 167
    'calebasse.middleware.request',
167 168
    'south',
169
    'django_journal',
168 170
)
169 171

  
170 172
INTERNAL_IPS=('127.0.0.1',)
171
-