Project

General

Profile

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

calebasse / calebasse / common / models.py @ b58c4ed6

1
from django.db.models.signals import post_save, pre_delete
2
from ..middleware.request import get_request
3

    
4
import django_journal
5

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

    
11
def object_save(sender, **kwargs):
12
    model_name = sender.__name__.lower()
13
    if kwargs.get('created'):
14
        tag = 'new-%s' % model_name
15
        log = '{obj} created'
16
    else:
17
        tag = '%s-save' % model_name
18
        log = '{obj} saved'
19

    
20
    obj = kwargs['instance']
21
    try:
22
        get_request().record(tag, log, obj=obj)
23
    except AttributeError:
24
        django_journal.record(tag, '{obj} from shell', obj=obj)
25

    
26
def object_delete(sender, **kwargs):
27
    model_name = sender.__name__.lower()
28
    try:
29
        get_request().record('delete-%s' % model_name,
30
                             '{obj}',
31
                             obj=kwargs['instance'])
32
    except AttributeError:
33
        django_journal.record('delete-%s' % model_name,
34
                              '{obj} from shell', obj=kwargs['instance'])
35

    
36
for model in (Event, EventWithAct, EventType, ActValidationState, Act,
37
              FileState, PatientRecord, PatientAddress, PatientContact,
38
              People, Worker, ExternalWorker, ExternalTherapist, Holiday):
39
    post_save.connect(object_save, sender=model)
40
    pre_delete.connect(object_delete, sender=model)
41

    
(2-2/3)