Skip to content

Commit

Permalink
add new jinja2 filter specific for formatting fuzzy_dates (also for d…
Browse files Browse the repository at this point in the history
…ates before 1900)
  • Loading branch information
laevya committed Apr 26, 2018
1 parent 7d7a504 commit 85ce0b3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
44 changes: 44 additions & 0 deletions pyoes/utils.py
Expand Up @@ -37,3 +37,47 @@ def datetime_format_filter(value, format='%d-%m-%Y %H:%M'):
return date.strftime(format)
else:
return ''


def fuzzy_date_format_filter(value, format='%d-%m-%Y'):
"""
Jinja 2 filter to print a fuzzy date object in a template.
:param value: Eiter a :class:`datetime.datetime` or a string that can be \
parsed by :class:`dateutil.parser`. In case of ambiguity between the \
day and the month, the parser will assume the day precedes the month \
(ie. European way)
:param format: The format to print the date in.
:return: The formatted date, eg. `07-09-2014 20:00`.
"""
if value:
try:
return date_converter(value, format)
except AttributeError:
if value[0:4].find('-') == -1 and value[0:4].find('/') == -1:
date = parser.parse(value, yearfirst=True, dayfirst=False)
else:
date = parser.parse(value, dayfirst=True)
return date_converter(date, format)
else:
return ''


def date_converter(date, format):
"""
Dateconverter to convert date or datetime objects to strings in a specific format
Implemented because of the problems of datetime.strftime method with years before 1900
:param date:
:param format: The format to print the date in. (must be a predefined format)
:return: The formatted date, eg. `07-09-2014`
"""
if format == '%d-%m-%Y':
return '{0.day:02d}-{0.month:02d}-{0.year:4d}'.format(date)
elif format == '%m-%Y':
return '{0.month:02d}-{0.year:4d}'.format(date)
elif format == '%Y':
return '{0.year:4d}'.format(date)
else:
# Default
return '{0.day:02d}-{0.month:02d}-{0.year:4d}'.format(date)
57 changes: 55 additions & 2 deletions tests/test_utils.py
Expand Up @@ -3,8 +3,8 @@

from pyoes.utils import (
set_attr_filter,
datetime_format_filter
)
datetime_format_filter,
fuzzy_date_format_filter)


class TestSetAttr(unittest.TestCase):
Expand Down Expand Up @@ -61,3 +61,56 @@ def test_datetime_format_filter_slash(self):
def test_datetime_format_filter_none(self):
fd = datetime_format_filter(None)
self.assertEqual('', fd)


class TestFuzzyDateFormatFilter(unittest.TestCase):
def test_fuzzy_date_format_filter_default(self):
from datetime import datetime
d = datetime(2018, 4, 26, 20, 00)
fd = fuzzy_date_format_filter(d)
self.assertEqual('26-04-2018', fd)

def test_fuzzy_date_format_filter_before_1900(self):
from datetime import datetime
d = datetime(1782, 4, 26, 20, 00)
fd = fuzzy_date_format_filter(d)
self.assertEqual('26-04-1782', fd)

def test_fuzzy_date_format_filter_date(self):
from datetime import date
d = date(2018, 4, 26)
fd = fuzzy_date_format_filter(d)
self.assertEqual('26-04-2018', fd)

def test_fuzzy_date_format_filter_custom_format(self):
from datetime import datetime
d = datetime(2018, 4, 26, 20, 00)
fd = fuzzy_date_format_filter(d, '%m-%Y')
self.assertEqual('04-2018', fd)
d = datetime(2018, 4, 26, 20, 00)
fd = fuzzy_date_format_filter(d, '%Y')
self.assertEqual('2018', fd)

def test_fuzzy_date_format_filter_default_parsed(self):
fd = fuzzy_date_format_filter('26-04-2018 20:00')
self.assertEqual('26-04-2018', fd)

def test_fuzzy_date_format_filter_yearfirst(self):
fd = fuzzy_date_format_filter('2018-04-26')
self.assertEqual('26-04-2018', fd)

def test_fuzzy_date_format_filter_yearfirst_daylast(self):
fd = fuzzy_date_format_filter('2018-11-12')
self.assertEqual('12-11-2018', fd)

def test_fuzzy_date_format_filter_yearfirst_slash(self):
fd = fuzzy_date_format_filter('2018/11/12')
self.assertEqual('12-11-2018', fd)

def test_fuzzy_date_format_filter_slash(self):
fd = fuzzy_date_format_filter('12/11/12')
self.assertEqual('12-11-2012', fd)

def test_fuzzy_date_format_filter_none(self):
fd = fuzzy_date_format_filter(None)
self.assertEqual('', fd)

0 comments on commit 85ce0b3

Please sign in to comment.