-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
107 lines (83 loc) · 3.24 KB
/
handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#coding: UTF-8
"""
Event handlers for keeping track of the modifications made on historised objects.
These functions are private to the hist application.
@author: Marc-Antoine Gouillart
@copyright: Marc-Antoine Gouillart, 2009
@license: GNU GPL v3
"""
## Django imports
from django.db import models
from django.db.models.base import ModelBase
from django.db.models.signals import pre_save
## Hist imports
from exceptions import *
from models import Essence
from helpers import ModelDiff
from helpers_copy import _copy_object
def __instance_should_be_historised(instance):
"""
Checks whether an object should be followed by hist.
@param instance: the object that was modified
@return: True or False
"""
try:
history_model = instance._meta.history_model
except AttributeError:
return False
try:
if instance._meta.history_disabled:
return False
except AttributeError:
pass
return True
def __silent_copy(original, action_code, version, comment):
try:
_copy_object(original, action_code, version, comment)
except WontCopy:
pass
def after_save_event_handler(sender, instance, **kwargs):
"""Signal handler for creations"""
## Check this object really should be followed by hist
if not __instance_should_be_historised(instance):
return
history_model = instance._meta.history_model
## Check it is a new object
try:
instance.current_version
except:
__silent_copy(instance, 'creation', 0, u'création de l\'objet')
def before_save_event_handler(sender, instance, **kwargs):
"""Signal handler for updates"""
## Check this object really should be followed by hist
if not __instance_should_be_historised(instance):
return
history_model = instance._meta.history_model
## Check it is an update and not a creation
if not instance.id:
return
## Current version
version = instance.current_version + 1
## Create a new history object with the data
__silent_copy(instance, 'update', version, u'mise à jour sans commentaire')
def before_delete_event_handler(sender, instance, **kwargs):
"""signal handler for deletions"""
## Check this object really should be followed by hist
if not __instance_should_be_historised(instance):
return
history_model = instance._meta.history_model
## Deleted version
version = instance.current_version + 1
avatar = instance.current_avatar
## Reference the deletion and link it to the latest avatar of the object
ho = instance._meta.history_model(history_action = 'deletion',
history_version = version,
history_comment = u'destruction sans commentaire',
history_active_object = None,
history_linked_to_version = avatar,
essence = avatar.essence)
ho.save()
## Remove all history FK, so that the deletion won't delete the avatars
for history_object in instance.version_set.all():
history_object.history_active_object = None
history_object.save()