Skip to content

Commit 3ceaa8d

Browse files
committed
Merge pull request django-haystack#682 from acdha/682-update_index-tz-support
update_index should use non-naive datetime when settings.USE_TZ=True
2 parents d399ea3 + 1fdfeaa commit 3ceaa8d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

haystack/management/commands/update_index.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
import datetime
1+
from datetime import timedelta
2+
from optparse import make_option
23
import os
34
import warnings
4-
from optparse import make_option
5+
56
from django import db
67
from django.conf import settings
78
from django.core.exceptions import ImproperlyConfigured
89
from django.core.management.base import LabelCommand
910
from django.db import reset_queries
1011
from django.utils.encoding import smart_str
12+
1113
from haystack import connections as haystack_connections
1214
from haystack.constants import DEFAULT_ALIAS
1315
from haystack.query import SearchQuerySet
1416

17+
try:
18+
from django.utils.timezone import now
19+
except ImportError:
20+
from datetime import datetime
21+
now = datetime.now
22+
1523

1624
DEFAULT_BATCH_SIZE = None
1725
DEFAULT_AGE = None
@@ -135,7 +143,7 @@ def handle(self, *items, **options):
135143
end_date = options.get('end_date')
136144

137145
if age is not None:
138-
self.start_date = datetime.datetime.now() - datetime.timedelta(hours=int(age))
146+
self.start_date = now() - timedelta(hours=int(age))
139147

140148
if start_date is not None:
141149
from dateutil.parser import parse as dateutil_parse

tests/solr_tests/tests/management_commands.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import datetime
2+
3+
from mock import patch
24
import pysolr
5+
6+
from django import VERSION as DJANGO_VERSION
37
from django.conf import settings
48
from django.core.exceptions import ImproperlyConfigured
59
from django.core.management import call_command
610
from django.test import TestCase
11+
from django.utils import unittest
12+
713
from haystack import connections
814
from haystack import indexes
915
from haystack.utils.loading import UnifiedIndex
16+
1017
from core.models import MockModel, MockTag
1118

1219

@@ -94,6 +101,20 @@ def test_age(self):
94101
call_command('update_index', age=3, verbosity=0)
95102
self.assertEqual(self.solr.search('*:*').hits, 1)
96103

104+
@unittest.skipIf(DJANGO_VERSION < (1, 4, 0), 'timezone support was added in Django 1.4')
105+
def test_age_with_time_zones(self):
106+
"""Haystack should use django.utils.timezone.now on Django 1.4+"""
107+
from django.utils.timezone import now as django_now
108+
from haystack.management.commands.update_index import now as haystack_now
109+
110+
self.assertIs(haystack_now, django_now,
111+
msg="update_index should use django.utils.timezone.now")
112+
113+
with patch("haystack.management.commands.update_index.now") as m:
114+
m.return_value = django_now()
115+
self.test_age()
116+
assert m.called
117+
97118
def test_dates(self):
98119
call_command('clear_index', interactive=False, verbosity=0)
99120
self.assertEqual(self.solr.search('*:*').hits, 0)

0 commit comments

Comments
 (0)