def get_modified_since(request, last_modified): """ Checks if a Last-Modified timestamp is newer than the requested HTTP_IF_MODIFIED_SINCE from the browser. This can be used to bail early if no updates have been performed since the last access to the page. This can take a DateField, datetime, HTTP date-formatted string, or a function for the last_modified timestamp. If a function is passed, it will only be called if the HTTP_IF_MODIFIED_SINCE header is present. """ if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE', None) if if_modified_since is not None: if six.callable(last_modified): last_modified = last_modified() return (if_modified_since == http_date(last_modified)) return False
def set_last_modified(response, timestamp): """ Sets the Last-Modified header in a response based on a DateTimeField. """ response['Last-Modified'] = http_date(timestamp)
def test_http_date_with_unix_timestamp(self): """Testing http_date with unix timestamp""" unix_timestamp = '1466424000' self.assertEqual(dates.http_date(unix_timestamp), unix_timestamp)
def test_http_date_with_date_string(self): """Testing http_date with date string""" date = '20/06/2016' self.assertEqual(dates.http_date(date), date)
def test_http_date_with_datetime(self): """Testing http_date with datetime""" date_time = datetime.datetime(2016, 8, 26, 3, 3, 26, 123456) self.assertEqual(dates.http_date(date_time), 'Fri, 26 Aug 2016 03:03:26 GMT')
def test_http_date_with_unix_timestamp(self): """Testing http_date with unix timestamp""" unix_timestamp = '1466424000' self.assertEqual(http_date(unix_timestamp), unix_timestamp)
def test_http_date_with_date_string(self): """Testing http_date with date string""" date = '20/06/2016' self.assertEqual(http_date(date), date)
def test_http_date_with_datetime(self): """Testing http_date with datetime""" date_time = datetime(2016, 8, 26, 3, 3, 26, 123456) self.assertEqual(http_date(date_time), 'Fri, 26 Aug 2016 03:03:26 GMT')