forked from feincms/feincms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangedate.py
More file actions
66 lines (49 loc) · 2.23 KB
/
Copy pathchangedate.py
File metadata and controls
66 lines (49 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# ------------------------------------------------------------------------
# coding=utf-8
# ------------------------------------------------------------------------
"""
Track the modification date for objects.
"""
from __future__ import absolute_import, unicode_literals
from email.utils import parsedate_tz, mktime_tz
from time import mktime
from django.db import models
from django.db.models.signals import pre_save
from django.utils import timezone
from django.utils.http import http_date
from django.utils.translation import ugettext_lazy as _
from feincms import extensions
# ------------------------------------------------------------------------
def pre_save_handler(sender, instance, **kwargs):
"""
Intercept attempts to save and insert the current date and time into
creation and modification date fields.
"""
now = timezone.now()
if instance.id is None:
instance.creation_date = now
instance.modification_date = now
# ------------------------------------------------------------------------
def dt_to_utc_timestamp(dt):
return int(mktime(dt.timetuple()))
class Extension(extensions.Extension):
def handle_model(self):
self.model.add_to_class('creation_date', models.DateTimeField(
_('creation date'), null=True, editable=False))
self.model.add_to_class('modification_date', models.DateTimeField(
_('modification date'), null=True, editable=False))
self.model.last_modified = lambda p: p.modification_date
pre_save.connect(pre_save_handler, sender=self.model)
# ------------------------------------------------------------------------
def last_modified_response_processor(page, request, response):
# Don't include Last-Modified if we don't want to be cached
if "no-cache" in response.get('Cache-Control', ''):
return
# If we already have a Last-Modified, take the later one
last_modified = dt_to_utc_timestamp(page.last_modified())
if response.has_header('Last-Modified'):
last_modified = max(
last_modified,
mktime_tz(parsedate_tz(response['Last-Modified'])))
response['Last-Modified'] = http_date(last_modified)
# ------------------------------------------------------------------------