Done with historique
[auf_rh_dae.git] / project / rh / models.py
index 5e3d003..f7c67e0 100644 (file)
@@ -1075,6 +1075,97 @@ reversion.register(Dossier, format='xml', follow=[
 ])
 
 
+class RHDossierClassementRecord(models.Model):
+    classement = models.ForeignKey(
+        'Classement',
+        related_name='classement_records',
+        )
+    dossier = models.ForeignKey(
+        'Dossier',
+        related_name='classement_records',
+        )
+    date_debut = models.DateField(
+        u"date de début",
+        help_text=HELP_TEXT_DATE,
+        null=True,
+        blank=True,
+        db_index=True
+    )
+    date_fin = models.DateField(
+        u"date de fin",
+        help_text=HELP_TEXT_DATE,
+        null=True,
+        blank=True,
+        db_index=True
+    )
+
+    def __unicode__(self):
+        return self.classement.__unicode__()
+
+    class Meta:
+        verbose_name = u"Element d'historique de classement"
+        verbose_name_plural = u"Historique de classement"
+
+    @classmethod
+    def post_save_handler(cls,
+                          sender,
+                          instance,
+                          created,
+                          using,
+                          **kw):
+
+        # Attempt to pull previous classement from history.
+        previous_classement = None
+        has_changed = False
+
+        if not created:
+            has_changed = (
+                instance.classement.id !=
+                instance.before_save.classement.id
+                )
+            try:
+                previous_record = cls.objects.get(
+                    dossier=instance,
+                    classement=instance.before_save.classement,
+                    date_fin=None,
+                    )
+            except cls.DoesNotExist:
+                previous_record = None
+            else:
+                previous_classement = previous_record.classement
+
+
+        if created or (not created and not previous_classement):
+            cls.objects.create(
+                date_debut=instance.date_debut,
+                classement=instance.classement,
+                dossier=instance,
+                )
+        elif has_changed and previous_classement:
+            change_date = datetime.date.today()
+            previous_record.date_fin = change_date
+            previous_record.save()
+            cls.objects.create(
+                date_debut=change_date,
+                classement=instance.classement,
+                dossier=instance,
+                )
+            
+        # elif not created:
+        #     cmp_inst = self.before_save
+
+        #     # Classement has changed!
+        #     if (cmp_inst.classement.id !=
+        #         instance.classement.id):
+
+                
+                
+                
+            
+            
+        
+
+
 class DossierPiece_(models.Model):
     """
     Documents relatifs au Dossier (à l'occupation de ce poste par employé).
@@ -1747,19 +1838,6 @@ class ChangementPersonnel(models.Model):
 
 
     @classmethod
-    def pre_save_handler(cls,
-                         sender,
-                         instance,
-                         using,
-                         **kw):
-        # Store a copy of the model before save is called.
-        if instance.pk is not None:
-            instance.before_save = Dossier.objects.get(pk=instance.pk)
-        else:
-            instance.before_save = None
-
-
-    @classmethod
     def post_save_handler(cls,
                           sender,
                           instance,
@@ -1861,5 +1939,22 @@ class ChangementPersonnel(models.Model):
 reversion.register(ChangementPersonnel, format='xml')
 
 
+def dossier_pre_save_handler(sender,
+                     instance,
+                     using,
+                     **kw):
+    # Store a copy of the model before save is called.
+    if instance.pk is not None:
+        instance.before_save = Dossier.objects.get(pk=instance.pk)
+    else:
+        instance.before_save = None
+
+
+# Connect a pre_save handler that assigns a copy of the model as an
+# attribute in order to compare it in post_save.
+pre_save.connect(dossier_pre_save_handler, sender=Dossier)
+
 post_save.connect(ChangementPersonnel.post_save_handler, sender=Dossier)
-pre_save.connect(ChangementPersonnel.pre_save_handler, sender=Dossier)
+post_save.connect(RHDossierClassementRecord.post_save_handler, sender=Dossier)
+
+