Fix 5453
[auf_rh_dae.git] / project / rh / models.py
index 14c76f6..4944fbf 100644 (file)
@@ -1114,56 +1114,69 @@ class RHDossierClassementRecord(models.Model):
                           using,
                           **kw):
 
-        # Attempt to pull previous classement from history.
+        today = date.today()
+        previous_record = None
         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(
+        # Premièrement, pour les nouvelles instances:
+        if created:
+            if not self.classement:
+                return
+            else:
+                cls.objects.create(
+                    date_debut=instance.date_debut,
+                    classement=instance.classement,
                     dossier=instance,
-                    classement=instance.before_save.classement,
-                    date_fin=None,
                     )
-            except cls.DoesNotExist:
-                previous_record = None
-            else:
-                previous_classement = previous_record.classement
+                return
 
+        # Deuxièmement, pour les instances existantes:
 
-        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,
+        # Détermine si:
+        # 1. Est-ce que le classement a changé?
+        # 2. Est-ce qu'une historique de classement existe déjà
+        try:
+            previous_record = cls.objects.get(
                 dossier=instance,
+                classement=instance.before_save.classement,
+                date_fin=None,
                 )
-            
-        # elif not created:
-        #     cmp_inst = self.before_save
+        except cls.DoesNotExist:
+            if instance.before_save.classement:
+                # Il était censé avoir une historique de classement
+                # donc on le créé.
+                previous_record = cls.objects.create(
+                    date_debut=instance.before_save.date_debut,
+                    classement=instance.before_save.classement,
+                    dossier=instance,
+                    )
+                previous_classement = instance.before_save.classement
 
-        #     # Classement has changed!
-        #     if (cmp_inst.classement.id !=
-        #         instance.classement.id):
+        else:
+            previous_classement = previous_record.classement
 
+        has_changed = (
+            instance.classement !=
+            previous_classement
+            )
+
+        # Cas aucun changement:
+        if not has_changed:
+            return
+
+        else:
+            # Classement a changé
+            if previous_record:
+                previous_record.date_fin = today
+                previous_record.save()
                 
-                
-                
-            
-            
-        
+            if instance.classement:
+                cls.objects.create(
+                    date_debut=today,
+                    classement=instance.classement,
+                    dossier=instance,
+                    )
 
 
 class DossierPiece_(models.Model):