Skip to content

Commit a360bf4

Browse files
author
Martin J. Laubach
committed
Correct handling of last-modified times.
The last-modified header was completely out of whack wrt time zones, hopefully that's now fixed. Also simplify the date comparison in the response processor a bit.
1 parent 7260f13 commit a360bf4

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

feincms/module/extensions/changedate.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55
Track the modification date for pages.
66
"""
77

8+
import os
9+
10+
from email.utils import parsedate_tz, mktime_tz
11+
812
from django.db import models
913
from django.db.models.signals import pre_save
1014
from django.utils.translation import ugettext_lazy as _
1115

16+
try:
17+
import pytz
18+
local_tz = pytz.timezone(os.environ['TZ'])
19+
except ImportError:
20+
class NoTimezone(object):
21+
def localize(_, dt):
22+
return dt
23+
local_tz = NoTimezone()
24+
25+
# ------------------------------------------------------------------------
1226
def pre_save_handler(sender, instance, **kwargs):
1327
"""
1428
Intercept attempts to save and insert the current date and time into
@@ -23,36 +37,33 @@ def pre_save_handler(sender, instance, **kwargs):
2337

2438
# ------------------------------------------------------------------------
2539
def dt_to_utc_timestamp(dt):
26-
from time import mktime, gmtime
27-
return mktime(gmtime(mktime(dt.utctimetuple())))
40+
from time import mktime
41+
return int(mktime(dt.timetuple()))
2842

2943
def register(cls, admin_cls):
3044
cls.add_to_class('creation_date', models.DateTimeField(_('creation date'), null=True, editable=False))
3145
cls.add_to_class('modification_date', models.DateTimeField(_('modification date'), null=True, editable=False))
3246

3347
if hasattr(cls, 'cache_key_components'):
34-
cls.cache_key_components.append(lambda page: page.modification_date and page.modification_date.strftime('%s'))
48+
cls.cache_key_components.append(lambda page: page.modification_date and str(dt_to_utc_timestamp(page.modification_date)))
3549

36-
if hasattr(cls, 'last_modified'):
37-
cls.last_modified = lambda p: dt_to_utc_dt(p.modification_date)
50+
cls.last_modified = lambda p: p.modification_date
3851

3952
pre_save.connect(pre_save_handler, sender=cls)
4053

4154
# ------------------------------------------------------------------------
42-
def last_modified_response_processor(self, request, response):
55+
def last_modified_response_processor(page, request, response):
4356
from django.utils.http import http_date
4457

4558
# Don't include Last-Modified if we don't want to be cached
4659
if "no-cache" in response.get('Cache-Control', ''):
4760
return
4861

4962
# If we already have a Last-Modified, take the later one
50-
from email.utils import parsedate_tz, mktime_tz
51-
52-
last_modified = [ dt_to_utc_timestamp(self.modification_date) ]
63+
last_modified = dt_to_utc_timestamp(page.last_modified())
5364
if response.has_header('Last-Modified'):
54-
last_modified.append(mktime_tz(parsedate_tz(response['Last-Modified'])))
65+
last_modified = max(last_modified, mktime_tz(parsedate_tz(response['Last-Modified'])))
5566

56-
response['Last-Modified'] = http_date(max(last_modified))
67+
response['Last-Modified'] = http_date(last_modified)
5768

5869
# ------------------------------------------------------------------------

0 commit comments

Comments
 (0)