Ajout exemple
[django-historique.git] / django_historique / models.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 from django.db import models
4 from django.db.models.base import ModelBase
5 from django.db import connection, transaction
6 from copy import deepcopy
7 import datetime
8
9
10
11 def log_instance (i, prefix=""):
12 print prefix, i, i.__class__.__name__
13 for f in i._meta.fields:
14 print prefix, " ", f.name, f.__class__.__name__
15
16 class History (models.Model):
17 history_datetime = models.DateTimeField(default=datetime.datetime.now)
18 history_objectid = models.PositiveIntegerField() # these two are
19 history_revision = models.PositiveIntegerField() # unique_together ?
20 history_comment = models.CharField(max_length=1024, default="")
21
22 def save_log (self, instance):
23 iClass = instance.__class__
24 hClass = self.__class__
25 instance = iClass.objects.get(id = instance.id)
26 self.history_objectid = instance.id
27 self.history_comment = "pre_save history item <%s>" \
28 % (repr(instance))
29 self.history_revision = hClass.objects.filter(history_objectid=instance.id).count()+1
30 for field in instance._meta.fields:
31 if field.__class__.__name__ != 'AutoField':
32 print field.name
33 if getattr(field, 'unique', False):
34 field._unique = False
35 setattr(self, field.name, getattr(instance, field.name))
36 log_instance (instance, "I")
37 log_instance (self, "S")
38 self.save ()
39
40 class Meta:
41 abstract = True
42
43