Skip to content

Commit

Permalink
Use timezone-aware dates and times in rules (#197)
Browse files Browse the repository at this point in the history
* use timezone-aware dates and times re #196

* remove redundant newlines

* Fix flake8 linting errors in python 3.6
  • Loading branch information
aweakley authored and jberghoef committed Jan 24, 2019
1 parent 1e69d92 commit 956c1bf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/wagtail_personalisation/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ def refresh(self):
for segment in enabled_segments:
if segment.is_static and segment.static_users.filter(id=self.request.user.id).exists():
additional_segments.append(segment)
elif (segment.excluded_users.filter(id=self.request.user.id).exists() or
segment in excluded_segments):
elif any((
segment.excluded_users.filter(id=self.request.user.id).exists(),
segment in excluded_segments
)):
continue
elif not segment.is_static or not segment.is_full:
segment_rules = []
Expand Down
6 changes: 3 additions & 3 deletions src/wagtail_personalisation/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging

import re
from datetime import datetime
from importlib import import_module

import pycountry
Expand All @@ -13,6 +12,7 @@
from django.db import models
from django.template.defaultfilters import slugify
from django.test.client import RequestFactory
from django.utils import timezone
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from modelcluster.fields import ParentalKey
Expand Down Expand Up @@ -113,7 +113,7 @@ class Meta:
verbose_name = _('Time Rule')

def test_user(self, request=None):
return self.start_time <= datetime.now().time() <= self.end_time
return self.start_time <= timezone.now().time() <= self.end_time

def description(self):
return {
Expand Down Expand Up @@ -157,7 +157,7 @@ class Meta:

def test_user(self, request=None):
return [self.mon, self.tue, self.wed, self.thu,
self.fri, self.sat, self.sun][datetime.today().weekday()]
self.fri, self.sat, self.sun][timezone.now().date().weekday()]

def description(self):
days = (
Expand Down

0 comments on commit 956c1bf

Please sign in to comment.