]> git.ozlabs.org Git - patchwork/commitdiff
models: Fix invalid dates in patch mbox
authorJeremy Kerr <jk@ozlabs.org>
Mon, 10 Jun 2013 02:16:24 +0000 (10:16 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 10 Jun 2013 02:16:24 +0000 (10:16 +0800)
Currently, the Date header in mbox views is incorrect;
datetime.datetime.utctimetuple assumes that the date is in the local
timezone, but we keep all dates as UTC.

This change replaces utctimetuple with a manual calculation of the
seconds-since-epoch timestamp, which email.utils.formatdate requires. We
add a testcase for this too.

Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
apps/patchwork/models.py
apps/patchwork/tests/mboxviews.py

index a9e70ced4c141cdc5b3dfc97115d665707ef4617..250ad018bb87806e674c4ba63ddcea109627587e 100644 (file)
@@ -279,10 +279,12 @@ class Patch(models.Model):
         if self.content:
             body += '\n' + self.content
 
         if self.content:
             body += '\n' + self.content
 
+        utc_timestamp = (self.date -
+                datetime.datetime.utcfromtimestamp(0)).total_seconds()
+
         mail = PatchMbox(body)
         mail['Subject'] = self.name
         mail = PatchMbox(body)
         mail['Subject'] = self.name
-        mail['Date'] = email.utils.formatdate(
-                        time.mktime(self.date.utctimetuple()))
+        mail['Date'] = email.utils.formatdate(utc_timestamp)
         mail['From'] = email.utils.formataddr((
                         str(Header(self.submitter.name, mail.patch_charset)),
                         self.submitter.email))
         mail['From'] = email.utils.formataddr((
                         str(Header(self.submitter.name, mail.patch_charset)),
                         self.submitter.email))
index 07513c2afedf2487971ca2f9f29334ca12c7f9d2..e619e7b35b2014410f80615980fe9a1fc7edef2e 100644 (file)
@@ -20,6 +20,8 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 import unittest
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 import unittest
+import email
+import dateutil.parser, dateutil.tz
 from django.test import TestCase
 from django.test.client import Client
 from patchwork.models import Patch, Comment, Person
 from django.test import TestCase
 from django.test.client import Client
 from patchwork.models import Patch, Comment, Person
@@ -132,3 +134,25 @@ class MboxBrokenFromHeaderTest(TestCase):
 
         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
         self.assertContains(response, from_email)
 
         response = self.client.get('/patch/%d/mbox/' % self.patch.id)
         self.assertContains(response, from_email)
+
+class MboxDateHeaderTest(TestCase):
+    """ Test that the date provided in the patch mail view is correct """
+
+    def setUp(self):
+        defaults.project.save()
+        self.person = defaults.patch_author_person
+        self.person.save()
+
+        self.patch = Patch(project = defaults.project,
+                           msgid = 'p1', name = 'testpatch',
+                           submitter = self.person, content = '')
+        self.patch.save()
+
+    def testDateHeader(self):
+        response = self.client.get('/patch/%d/mbox/' % self.patch.id)
+        mail = email.message_from_string(response.content)
+        mail_date = dateutil.parser.parse(mail['Date'])
+        # patch dates are all in UTC
+        patch_date = self.patch.date.replace(tzinfo=dateutil.tz.tzutc(),
+                                            microsecond=0)
+        self.assertEqual(mail_date, patch_date)