Skip to content

Commit 503e518

Browse files
committed
✨ Add TIME_LEEWAY setting
1 parent 1c9b160 commit 503e518

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

notifications_api_common/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
NOTIFICATIONS_API_GET_DOMAIN = "notifications_api_common.utils.get_site_domain"
1313

14+
TIME_LEEWAY = 0
1415

1516
def get_setting(name: str) -> Any:
1617
this_module = sys.modules[__name__]

notifications_api_common/validators.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import logging
2+
from datetime import timedelta
23

4+
from .settings import get_setting
35
from django.core.exceptions import ValidationError
46
from django.utils import timezone
57
from django.utils.deconstruct import deconstructible
@@ -14,14 +16,16 @@ class UntilNowValidator:
1416
Validate a datetime to not be in the future.
1517
1618
This means that `now` is included.
19+
20+
Some leeway can be added with the TIME_LEEWAY setting.
1721
"""
1822

1923
message = _("Ensure this value is not in the future.")
2024
code = "future_not_allowed"
2125

2226
@property
2327
def limit_value(self):
24-
return timezone.now()
28+
return timezone.now() + timedelta(seconds=get_setting("TIME_LEEWAY"))
2529

2630
def __call__(self, value):
2731
if value > self.limit_value:

tests/test_validators.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from datetime import datetime
2+
3+
from django.core.exceptions import ValidationError
4+
5+
import pytest
6+
from freezegun import freeze_time
7+
8+
from notifications_api_common.validators import UntilNowValidator
9+
10+
11+
@freeze_time("2021-08-23T14:20:00")
12+
def test_invalid_date():
13+
validator = UntilNowValidator()
14+
with pytest.raises(ValidationError) as error:
15+
validator(datetime(2021, 8, 23, 14, 20, 4))
16+
assert "Ensure this value is not in the future." in str(error.value)
17+
18+
19+
@freeze_time("2021-08-23T14:20:00")
20+
def test_invalid_date_with_leeway(settings):
21+
settings.TIME_LEEWAY = 5
22+
validator = UntilNowValidator()
23+
validator(datetime(2021, 8, 23, 14, 20, 4))

0 commit comments

Comments
 (0)