From e20918afdf50def969aca8cb864b33edaa2fa927 Mon Sep 17 00:00:00 2001
From: Pasi Eronen <pe@iki.fi>
Date: Wed, 2 Sep 2009 10:38:38 +0000
Subject: [PATCH] Show correct days-in-state information (hopefully)  -
 Legacy-Id: 1657

---
 changelog                              |  2 ++
 ietf/idrfc/idrfc_wrapper.py            | 15 ++++++++++++---
 ietf/idrfc/templatetags/ballot_icon.py |  2 +-
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/changelog b/changelog
index 84cd56f22..34329641b 100644
--- a/changelog
+++ b/changelog
@@ -1,5 +1,7 @@
 ietfdb (2.32)
 
+  * Show correct days-in-state information (hopefully). From Pasi.
+	
   * Cleaning of IPR disclosure submission form internals  
     to simplify Django 1.0 transition.  From Jelte and Pasi.	 
 	
diff --git a/ietf/idrfc/idrfc_wrapper.py b/ietf/idrfc/idrfc_wrapper.py
index 4f50cd9e2..eca599770 100644
--- a/ietf/idrfc/idrfc_wrapper.py
+++ b/ietf/idrfc/idrfc_wrapper.py
@@ -35,6 +35,7 @@ from ietf.idrfc.models import RfcIndex, RfcEditorQueue, DraftVersions
 import re
 from datetime import date, timedelta
 from django.utils import simplejson
+from django.db.models import Q
 import types
 
 BALLOT_ACTIVE_STATES = ['In Last Call',
@@ -306,7 +307,6 @@ class IetfProcessData:
     main_state = None
     sub_state = None
     state = None
-    state_date = None
     _ballot = None
     def __init__(self, idinternal):
         self._idinternal = idinternal
@@ -318,7 +318,6 @@ class IetfProcessData:
         else:
             self.sub_state = None
             self.state = self.main_state
-        self.state_date = i.event_date
     
     def has_iesg_ballot(self):
         try:
@@ -364,11 +363,21 @@ class IetfProcessData:
         else:
             return None
 
+    def state_date(self):
+        try:
+            return self._idinternal.documentcomment_set.filter(
+                Q(comment_text__istartswith="Draft Added by ")|
+                Q(comment_text__istartswith="State Changes to ")|
+                Q(comment_text__istartswith="Sub state has been changed to ")).order_by('-id')[0].date
+        except IndexError:
+            # should never happen -- return an obviously bogus date
+            return date(1990,1,1)
+
     def to_json_helper(self):
         result = {'main_state':self.main_state,
                   'sub_state':self.sub_state,
                   'state':self.state,
-                  'state_date':str(self.state_date),
+                  'state_date':str(self.state_date()),
                   'has_iesg_ballot':self.has_iesg_ballot(),
                   'has_active_iesg_ballot':self.has_active_iesg_ballot(),
                   'ad_name':self.ad_name(),
diff --git a/ietf/idrfc/templatetags/ballot_icon.py b/ietf/idrfc/templatetags/ballot_icon.py
index 8915a4c24..4a3a3ac20 100644
--- a/ietf/idrfc/templatetags/ballot_icon.py
+++ b/ietf/idrfc/templatetags/ballot_icon.py
@@ -156,7 +156,7 @@ def state_age_colored(doc):
     sub_state = doc.ietf_process.sub_state
     if main_state in ["Dead","AD is watching","RFC Published"]:
         return ""
-    days = timesince_days(doc.ietf_process.state_date)
+    days = timesince_days(doc.ietf_process.state_date())
     goal = 0
     if sub_state == "Revised ID Needed":
         goal = 30