This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
6.7.2 | ||
6.7.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from datetime import datetime | ||
|
||
from django.test import TestCase | ||
|
||
from mock import patch | ||
|
||
from molo.surveys.widgets import NaturalDateInput | ||
|
||
|
||
class TestNaturalDateInputWidget(TestCase): | ||
def setUp(self): | ||
self.widget = NaturalDateInput() | ||
|
||
def test_widget_returns_value_if_falsey(self): | ||
data = {'name': ''} | ||
|
||
self.assertEqual( | ||
self.widget.value_from_datadict(data, None, 'name'), | ||
'', | ||
) | ||
|
||
def test_uses_dateparser_to_parse_dates(self): | ||
data = {'name': '1 January 2018'} | ||
|
||
self.assertEqual( | ||
self.widget.value_from_datadict(data, None, 'name'), | ||
datetime(2018, 1, 1), | ||
) | ||
|
||
def test_uses_dateparser_to_parse_datetimes(self): | ||
data = {'name': '1 January 2018 5pm'} | ||
|
||
self.assertEqual( | ||
self.widget.value_from_datadict(data, None, 'name'), | ||
datetime(2018, 1, 1, 17, 0), | ||
) | ||
|
||
@patch('molo.surveys.widgets.NaturalDateInput._parse_date') | ||
def test_caches_expensive_result_of_dateparser(self, parse_date_mock): | ||
parse_date_mock.return_value = datetime(2018, 1, 1) | ||
data = {'name': '1 January 2018'} | ||
|
||
self.widget.value_from_datadict(data, None, 'name') | ||
self.widget.value_from_datadict(data, None, 'name') | ||
|
||
self.assertEqual(parse_date_mock.call_count, 1) | ||
|
||
@patch('molo.surveys.widgets.NaturalDateInput._parse_date') | ||
def test_return_original_if_dateparser_returns_none(self, parse_date_mock): | ||
parse_date_mock.return_value = None | ||
data = {'name': "rubbish value which can't be parsed as a date"} | ||
|
||
self.widget.value_from_datadict(data, None, 'name') | ||
|
||
self.assertEqual( | ||
self.widget.value_from_datadict(data, None, 'name'), | ||
"rubbish value which can't be parsed as a date", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from dateparser import parse as date_parse | ||
|
||
from django.core.cache import cache | ||
from django.forms import DateInput | ||
|
||
from hashlib import md5 | ||
|
||
|
||
class NaturalDateInput(DateInput): | ||
def _parse_date(self, date_string): | ||
return date_parse(date_string) | ||
|
||
def value_from_datadict(self, data, files, name): | ||
date = data.get(name) | ||
|
||
if not date: | ||
return date | ||
|
||
hasher = md5() | ||
hasher.update(date.encode('utf-8')) | ||
cache_key = 'date_parse_{0}'.format(hasher.hexdigest()) | ||
|
||
parsed_date = cache.get(cache_key) | ||
|
||
if not parsed_date: | ||
parsed_date = self._parse_date(date) | ||
cache.set(cache_key, parsed_date, None) | ||
|
||
if parsed_date is None: | ||
return date | ||
else: | ||
return parsed_date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters