Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
tzinfo=app.publ_config.timezone)
with pytest.raises(ValueError):
utils.parse_date("not a date")
with pytest.raises(ValueError):
utils.parse_date("12345678")
with pytest.raises(ValueError):
utils.parse_date("")
assert utils.parse_date('1978-06-14') == (make_date(1978, 6, 14), 'day', utils.DAY_FORMAT)
assert utils.parse_date('19780614') == (make_date(1978, 6, 14), 'day', utils.DAY_FORMAT)
assert utils.parse_date('1983-07') == (make_date(1983, 7), 'month', utils.MONTH_FORMAT)
assert utils.parse_date('198307') == (make_date(1983, 7), 'month', utils.MONTH_FORMAT)
assert utils.parse_date("1979") == (make_date(1979), 'year', utils.YEAR_FORMAT)
assert utils.parse_date('19810505_w') == (make_date(1981, 5, 4), 'week', utils.WEEK_FORMAT)
with pytest.raises(ValueError):
utils.parse_date("not a date")
with pytest.raises(ValueError):
utils.parse_date("12345678")
with pytest.raises(ValueError):
utils.parse_date("")
assert utils.parse_date('1978-06-14') == (make_date(1978, 6, 14), 'day', utils.DAY_FORMAT)
assert utils.parse_date('19780614') == (make_date(1978, 6, 14), 'day', utils.DAY_FORMAT)
assert utils.parse_date('1983-07') == (make_date(1983, 7), 'month', utils.MONTH_FORMAT)
assert utils.parse_date('198307') == (make_date(1983, 7), 'month', utils.MONTH_FORMAT)
assert utils.parse_date("1979") == (make_date(1979), 'year', utils.YEAR_FORMAT)
assert utils.parse_date('19810505_w') == (make_date(1981, 5, 4), 'week', utils.WEEK_FORMAT)
with app.app_context():
def make_date(year=None, month=None, day=None):
return arrow.Arrow(year=year or 1,
month=month or 1,
day=day or 1,
tzinfo=app.publ_config.timezone)
with pytest.raises(ValueError):
utils.parse_date("not a date")
with pytest.raises(ValueError):
utils.parse_date("12345678")
with pytest.raises(ValueError):
utils.parse_date("")
assert utils.parse_date('1978-06-14') == (make_date(1978, 6, 14), 'day', utils.DAY_FORMAT)
assert utils.parse_date('19780614') == (make_date(1978, 6, 14), 'day', utils.DAY_FORMAT)
assert utils.parse_date('1983-07') == (make_date(1983, 7), 'month', utils.MONTH_FORMAT)
assert utils.parse_date('198307') == (make_date(1983, 7), 'month', utils.MONTH_FORMAT)
assert utils.parse_date("1979") == (make_date(1979), 'year', utils.YEAR_FORMAT)
assert utils.parse_date('19810505_w') == (make_date(1981, 5, 4), 'week', utils.WEEK_FORMAT)
month=month or 1,
day=day or 1,
tzinfo=app.publ_config.timezone)
with pytest.raises(ValueError):
utils.parse_date("not a date")
with pytest.raises(ValueError):
utils.parse_date("12345678")
with pytest.raises(ValueError):
utils.parse_date("")
assert utils.parse_date('1978-06-14') == (make_date(1978, 6, 14), 'day', utils.DAY_FORMAT)
assert utils.parse_date('19780614') == (make_date(1978, 6, 14), 'day', utils.DAY_FORMAT)
assert utils.parse_date('1983-07') == (make_date(1983, 7), 'month', utils.MONTH_FORMAT)
assert utils.parse_date('198307') == (make_date(1983, 7), 'month', utils.MONTH_FORMAT)
assert utils.parse_date("1979") == (make_date(1979), 'year', utils.YEAR_FORMAT)
assert utils.parse_date('19810505_w') == (make_date(1981, 5, 4), 'week', utils.WEEK_FORMAT)
def test_parse_date():
""" tests for the date parser """
import arrow
from . import PublMock
app = PublMock()
with app.app_context():
def make_date(year=None, month=None, day=None):
return arrow.Arrow(year=year or 1,
month=month or 1,
day=day or 1,
tzinfo=app.publ_config.timezone)
with pytest.raises(ValueError):
utils.parse_date("not a date")
with pytest.raises(ValueError):
utils.parse_date("12345678")
with pytest.raises(ValueError):
utils.parse_date("")
assert utils.parse_date('1978-06-14') == (make_date(1978, 6, 14), 'day', utils.DAY_FORMAT)
assert utils.parse_date('19780614') == (make_date(1978, 6, 14), 'day', utils.DAY_FORMAT)
assert utils.parse_date('1983-07') == (make_date(1983, 7), 'month', utils.MONTH_FORMAT)
assert utils.parse_date('198307') == (make_date(1983, 7), 'month', utils.MONTH_FORMAT)
assert utils.parse_date("1979") == (make_date(1979), 'year', utils.YEAR_FORMAT)
assert utils.parse_date('19810505_w') == (make_date(1981, 5, 4), 'week', utils.WEEK_FORMAT)
def where_entry_date(query, datespec):
""" Where clause for entries which match a textual date spec
datespec -- The date spec to check for, in YYYY[[-]MM[[-]DD]] format
"""
date, interval, _ = utils.parse_date(datespec)
start_date, end_date = date.span(interval)
return query.filter(lambda e:
e.local_date >= start_date.naive and
e.local_date <= end_date.naive
)
def _get_date_pagination(self,
base: ViewSpec
) -> typing.Tuple[typing.Optional['View'], typing.Optional['View']]:
""" Compute the pagination for date-based views """
date, interval, date_format = utils.parse_date(self.spec['date'])
start_date, end_date = date.span(interval)
base_query = queries.build_query(base)
oldest_neighbor = base_query.filter(lambda e: e.local_date < start_date.datetime)\
.order_by(*queries.ORDER_BY['newest']).first()
newest_neighbor = base_query.filter(lambda e: e.local_date > end_date.datetime)\
.order_by(*queries.ORDER_BY['oldest']).first()
older_view: typing.Optional['View'] = None
newer_view: typing.Optional['View'] = None
if newest_neighbor:
newer_date = newest_neighbor.display_date
newer_view = View.load({**base,
'order': self._order_by,
'date': arrow.get(newer_date).format(date_format)})
def _view_name(**formats) -> str:
if not any(k for k in PAGINATION_SPECS if k in self.spec):
# We don't have anything that specifies a pagination constraint, so
# we don't have a name
return ''
if not self.oldest or not self.newest:
# We don't have any entries, so we don't have a name
return ''
if 'date' in self.spec:
_, span_type, span_format = utils.parse_date(self.spec['date'])
elif self.oldest.date.year != self.newest.date.year:
span_type = 'year'
span_format = utils.YEAR_FORMAT
elif self.oldest.date.month != self.newest.date.month:
span_type = 'month'
span_format = utils.MONTH_FORMAT
else:
span_type = 'day'
span_format = utils.DAY_FORMAT
date_format = formats.get(
span_type, SPAN_FORMATS.get(span_type, span_format))
oldest = self.oldest.date.format(date_format)
if self.oldest == self.newest:
return oldest
self._order_by = spec.get('order', 'newest')
self.spec = spec
if 'start' in spec and paginated:
if self._order_by == 'oldest':
self.spec['first'] = self.spec['start']
elif self._order_by == 'newest':
self.spec['last'] = self.spec['start']
self._entries = queries.build_query(
spec).order_by(*queries.ORDER_BY[self._order_by])
if self.spec.get('date') is not None:
_, self.type, _ = utils.parse_date(self.spec['date'])
elif 'count' in self.spec:
self.type = 'count'
else:
self.type = ''