Permalink
Browse files

Merge pull request #682 from acdha/682-update_index-tz-support

update_index should use non-naive datetime when settings.USE_TZ=True
  • Loading branch information...
acdha committed Dec 11, 2012
2 parents d399ea3 + 1fdfeaa commit 3ceaa8d30a345a82152b16bd7cd867880ca4d4db
Showing with 32 additions and 3 deletions.
  1. +11 −3 haystack/management/commands/update_index.py
  2. +21 −0 tests/solr_tests/tests/management_commands.py
@@ -1,17 +1,25 @@
-import datetime
+from datetime import timedelta
+from optparse import make_option
import os
import warnings
-from optparse import make_option
+
from django import db
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import LabelCommand
from django.db import reset_queries
from django.utils.encoding import smart_str
+
from haystack import connections as haystack_connections
from haystack.constants import DEFAULT_ALIAS
from haystack.query import SearchQuerySet
+try:
+ from django.utils.timezone import now
+except ImportError:
+ from datetime import datetime
+ now = datetime.now
+
DEFAULT_BATCH_SIZE = None
DEFAULT_AGE = None
@@ -135,7 +143,7 @@ def handle(self, *items, **options):
end_date = options.get('end_date')
if age is not None:
- self.start_date = datetime.datetime.now() - datetime.timedelta(hours=int(age))
+ self.start_date = now() - timedelta(hours=int(age))
if start_date is not None:
from dateutil.parser import parse as dateutil_parse
@@ -1,12 +1,19 @@
import datetime
+
+from mock import patch
import pysolr
+
+from django import VERSION as DJANGO_VERSION
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.management import call_command
from django.test import TestCase
+from django.utils import unittest
+
from haystack import connections
from haystack import indexes
from haystack.utils.loading import UnifiedIndex
+
from core.models import MockModel, MockTag
@@ -94,6 +101,20 @@ def test_age(self):
call_command('update_index', age=3, verbosity=0)
self.assertEqual(self.solr.search('*:*').hits, 1)
+ @unittest.skipIf(DJANGO_VERSION < (1, 4, 0), 'timezone support was added in Django 1.4')
+ def test_age_with_time_zones(self):
+ """Haystack should use django.utils.timezone.now on Django 1.4+"""
+ from django.utils.timezone import now as django_now
+ from haystack.management.commands.update_index import now as haystack_now
+
+ self.assertIs(haystack_now, django_now,
+ msg="update_index should use django.utils.timezone.now")
+
+ with patch("haystack.management.commands.update_index.now") as m:
+ m.return_value = django_now()
+ self.test_age()
+ assert m.called
+
def test_dates(self):
call_command('clear_index', interactive=False, verbosity=0)
self.assertEqual(self.solr.search('*:*').hits, 0)

0 comments on commit 3ceaa8d

Please sign in to comment.