(Python 2 compatibility for datetime.timestamp)

This commit is contained in:
Pi Delport 2018-08-22 18:51:43 +02:00
parent 4b4f26f54e
commit 1b7429c3e1
2 changed files with 12 additions and 4 deletions

View file

@ -7,6 +7,7 @@ from __future__ import absolute_import
import hashlib
import hmac
import json
import sys
import time
import re
@ -28,6 +29,14 @@ TRACKING_CODE = """
register = Library()
def _timestamp(when): # type: (datetime) -> float
"""
Python 2 compatibility for `datetime.timestamp()`.
"""
return (time.mktime(when.timetuple()) if sys.version_info < (3,) else
when.timestamp())
def _hashable_bytes(data): # type: (AnyStr) -> bytes
"""
Coerce strings to hashable bytes.
@ -100,8 +109,7 @@ class IntercomNode(Node):
params.setdefault('user_id', user.pk)
params['created_at'] = int(time.mktime(
user.date_joined.timetuple()))
params['created_at'] = int(_timestamp(user.date_joined))
else:
params['created_at'] = None

View file

@ -9,7 +9,7 @@ from django.http import HttpRequest
from django.template import Context
from django.test.utils import override_settings
from analytical.templatetags.intercom import IntercomNode, intercom_user_hash
from analytical.templatetags.intercom import IntercomNode, intercom_user_hash, _timestamp
from analytical.tests.utils import TagTestCase
from analytical.utils import AnalyticalException
@ -123,7 +123,7 @@ class IntercomTagTestCase(TagTestCase):
) # type: User
attrs = IntercomNode()._get_custom_attrs(Context({'user': user}))
self.assertEqual({
'created_at': int(user.date_joined.timestamp()),
'created_at': int(_timestamp(user.date_joined)),
'email': 'test@example.com',
'name': '',
'user_hash': intercom_user_hash(str(user.pk)),