Skip to content

Commit

Permalink
Fix timezone support
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky committed Aug 13, 2014
1 parent ff48e4e commit 72f001f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion djangocms_blog/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.2b5'
__version__ = '0.2c1'
10 changes: 5 additions & 5 deletions djangocms_blog/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from collections import Counter
except ImportError:
from .compat import Counter
import datetime

from django.db import models
from django.utils.timezone import now
from parler.managers import TranslationManager


Expand Down Expand Up @@ -84,7 +84,7 @@ def published(self, queryset=None):
queryset = self.published_future(queryset)
if self.start_date_field:
return queryset.filter(
**{"%s__lte" % self.start_date_field: datetime.datetime.now()})
**{"%s__lte" % self.start_date_field: now()})
else:
return queryset

Expand All @@ -93,7 +93,7 @@ def published_future(self, queryset=None):
queryset = self.get_queryset().all()
if self.end_date_field:
qfilter = (
models.Q(**{"%s__gte" % self.end_date_field: datetime.datetime.now()})
models.Q(**{"%s__gte" % self.end_date_field: now()})
| models.Q(**{"%s__isnull" % self.end_date_field: True})
)
queryset = queryset.filter(qfilter)
Expand All @@ -104,7 +104,7 @@ def archived(self, queryset=None):
queryset = self.get_queryset().all()
if self.end_date_field:
qfilter = (
models.Q(**{"%s__lte" % self.end_date_field: datetime.datetime.now()})
models.Q(**{"%s__lte" % self.end_date_field: now()})
| models.Q(**{"%s__isnull" % self.end_date_field: False})
)
queryset = queryset.filter(qfilter)
Expand All @@ -127,5 +127,5 @@ def get_months(self, queryset=None):
date_counter = Counter(dates)
dates = set(dates)
dates = sorted(dates, reverse=True)
return [{'date': datetime.date(year=year, month=month, day=1),
return [{'date': now().replace(year=year, month=month, day=1),
'count': date_counter[year, month]} for year, month in dates]
1 change: 0 additions & 1 deletion djangocms_blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from cms.models import PlaceholderField, CMSPlugin
from cmsplugin_filer_image.models import ThumbnailOption
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.db import models
from django.utils import timezone
Expand Down
4 changes: 2 additions & 2 deletions djangocms_blog/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import datetime
from django.utils.translation import get_language
from django.utils.timezone import now
from django.contrib.auth.models import User
from django.core.urlresolvers import resolve
from django.views.generic import ListView, DetailView
Expand Down Expand Up @@ -76,7 +76,7 @@ def get_context_data(self, **kwargs):
kwargs['month'] = int(self.kwargs.get('month')) if 'month' in self.kwargs else None
kwargs['year'] = int(self.kwargs.get('year')) if 'year' in self.kwargs else None
if kwargs['year']:
kwargs['archive_date'] = datetime.date(kwargs['year'], kwargs['month'] or 1, 1)
kwargs['archive_date'] = now().replace(kwargs['year'], kwargs['month'] or 1, 1)
return super(PostArchiveView, self).get_context_data(**kwargs)


Expand Down
12 changes: 6 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from cms.api import add_plugin
from cms.utils.copy_plugins import copy_plugins_to
from cms.utils.plugins import downcast_plugins
from datetime import date
from django.core.urlresolvers import reverse
from django.utils.timezone import now
import parler
from taggit.models import Tag

Expand Down Expand Up @@ -80,30 +80,30 @@ def test_manager(self):
# default queryset, published and unpublished posts
months = Post.objects.get_months()
for data in months:
self.assertEqual(data['date'], date(year=date.today().year, month=date.today().month, day=1))
self.assertEqual(data['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
self.assertEqual(data['count'], 2)

# custom queryset, only published
post1.publish = True
post1.save()
months = Post.objects.get_months(Post.objects.published())
for data in months:
self.assertEqual(data['date'], date(year=date.today().year, month=date.today().month, day=1))
self.assertEqual(data['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
self.assertEqual(data['count'], 1)

self.assertEqual(len(Post.objects.available()), 1)

# If post is published but publishing date is in the future
post2.date_published = date(year=date.today().year+1, month=date.today().month, day=1)
post2.date_published = now().replace(year=now().year+1, month=now().month, day=1)
post2.publish = True
post2.save()
self.assertEqual(len(Post.objects.available()), 2)
self.assertEqual(len(Post.objects.published()), 1)
self.assertEqual(len(Post.objects.archived()), 0)

# If post is published but end publishing date is in the past
post2.date_published = date(year=date.today().year-2, month=date.today().month, day=1)
post2.date_published_end = date(year=date.today().year-1, month=date.today().month, day=1)
post2.date_published = now().replace(year=now().year-2, month=now().month, day=1)
post2.date_published_end = now().replace(year=now().year-1, month=now().month, day=1)
post2.save()
self.assertEqual(len(Post.objects.available()), 2)
self.assertEqual(len(Post.objects.published()), 1)
Expand Down
8 changes: 5 additions & 3 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
from datetime import date
import re
from cms.api import add_plugin
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.utils.timezone import now
from taggit.models import Tag

from . import BaseTest
Expand Down Expand Up @@ -99,9 +99,11 @@ def test_blog_archive_plugin(self):
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
plugin_class = plugin.get_plugin_class_instance()
context = plugin_class.render(RequestContext(request, {}), plugin, ph)
self.assertEqual(context['dates'][0], {'date': date(year=date.today().year, month=date.today().month, day=1), 'count': 2})
self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
self.assertEqual(context['dates'][0]['count'], 2)

post2.publish = False
post2.save()
context = plugin_class.render(RequestContext(request, {}), plugin, ph)
self.assertEqual(context['dates'][0], {'date': date(year=date.today().year, month=date.today().month, day=1), 'count': 1})
self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
self.assertEqual(context['dates'][0]['count'], 1)
13 changes: 7 additions & 6 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
from datetime import date
from django.contrib.auth.models import AnonymousUser
from django.http import Http404
from django.utils.translation import activate
from django.utils.timezone import now
from djangocms_blog.feeds import LatestEntriesFeed, TagFeed
from djangocms_blog.sitemaps import BlogSitemap
from djangocms_blog.views import PostListView, PostDetailView, PostArchiveView, \
CategoryEntriesView, AuthorEntriesView, TaggedListView
from djangocms_blog.views import (PostListView, PostDetailView,
PostArchiveView, CategoryEntriesView,
AuthorEntriesView, TaggedListView)

from . import BaseTest

Expand Down Expand Up @@ -89,7 +90,7 @@ def test_post_archive_view(self):
activate('en')
view_obj = PostArchiveView()
view_obj.request = request
view_obj.kwargs = {'year': date.today().year, 'month': date.today().month}
view_obj.kwargs = {'year': now().year, 'month': now().month}

# One post only, anonymous request
qs = view_obj.get_queryset()
Expand All @@ -98,7 +99,7 @@ def test_post_archive_view(self):

view_obj.object_list = qs
context = view_obj.get_context_data(object_list=view_obj.object_list)
self.assertEqual(context['archive_date'], date(year=date.today().year, month=date.today().month, day=1))
self.assertEqual(context['archive_date'].date(), now().replace(year=now().year, month=now().month, day=1).date())

def test_category_entries_view(self):
page1, page2 = self.get_pages()
Expand Down Expand Up @@ -212,4 +213,4 @@ def test_sitemap(self):
sitemap = BlogSitemap()
self.assertEqual(sitemap.items().count(), 2)
for item in sitemap.items():
self.assertTrue(sitemap.lastmod(item).date(), date.today())
self.assertTrue(sitemap.lastmod(item).date(), now().today())

0 comments on commit 72f001f

Please sign in to comment.