Ajout de quelques commentaires
[django-historique.git] / django_historique / models.py
CommitLineData
6ca2f470 1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3from django.db import models
4from django.db.models.base import ModelBase
5from django.db import connection, transaction
6from copy import deepcopy
7import datetime
8
9
6ca2f470 10def log_instance (i, prefix=""):
6a680ab4 11 "Pour debug, affichage d'un objet et de ses attributs"
6ca2f470 12 print prefix, i, i.__class__.__name__
13 for f in i._meta.fields:
14 print prefix, " ", f.name, f.__class__.__name__
15
16class History (models.Model):
6a680ab4 17 "Classe abstraite de base pour l'historique"
6ca2f470 18 history_datetime = models.DateTimeField(default=datetime.datetime.now)
6a680ab4 19 history_objectid = models.PositiveIntegerField()
20 history_revision = models.PositiveIntegerField()
6ca2f470 21 history_comment = models.CharField(max_length=1024, default="")
22
23 def save_log (self, instance):
6a680ab4 24 "Enregistrement de l'instance courante avant l'ecriture de la nouvelle"
6ca2f470 25 iClass = instance.__class__
26 hClass = self.__class__
6a680ab4 27
6ca2f470 28 instance = iClass.objects.get(id = instance.id)
29 self.history_objectid = instance.id
30 self.history_comment = "pre_save history item <%s>" \
31 % (repr(instance))
32 self.history_revision = hClass.objects.filter(history_objectid=instance.id).count()+1
6a680ab4 33
6ca2f470 34 for field in instance._meta.fields:
35 if field.__class__.__name__ != 'AutoField':
6ca2f470 36 if getattr(field, 'unique', False):
37 field._unique = False
38 setattr(self, field.name, getattr(instance, field.name))
6a680ab4 39
40 #log_instance (instance, "I")
41 #log_instance (self, "S")
42
6ca2f470 43 self.save ()
44
6ca2f470 45 class Meta:
46 abstract = True
47
48